How to program function keys as key equivalents

From the NSMenuItem Class Description

If you want to specify the Backspace key as the key equivalent for a menu item, use one character string with NSBackspaceCharacter (defined in NSText.h as 0x08), and for Forward Delete use NSDeleteCharacter (defined in NSText.h as 0x7F).

Not sure if I understand "use a single character string with ..." from the ref class.

// This works as expected

NSString *s = [NSString stringWithFormat:@"%c",NSDeleteCharacter]; [myMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; [myMenuItem setKeyEquivalent:s]; 

enter image description here

// This does not work as expected

 NSString *s = [NSString stringWithFormat:@"%c",NSF2FunctionKey]; [myMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; [myMenuItem setKeyEquivalent:s]; 

enter image description here

+4
source share
3 answers

Example for Swift 2.0:

 let key = String(utf16CodeUnits: [unichar(NSBackspaceCharacter)], count: 1) as String menuItem.keyEquivalentModifierMask = Int(NSEventModifierFlags.CommandKeyMask.rawValue) menuItem.keyEquivalent = key 
+4
source

I thought for myself.

  unichar c = NSF2FunctionKey; NSString *f2 = [NSString stringWithCharacters:&c length:1]; [mi setKeyEquivalent:f2]; [mi setKeyEquivalentModifierMask:NSCommandKeyMask]; 

enter image description here

+1
source

In Swift 3:

 let f2Character: Character = Character(UnicodeScalar(NSF2FunctionKey)!) myMenuItem.keyEquivalent: String(f2Character) myMenuItem.keyEquivalentModifierMask = [] 
+1
source

Source: https://habr.com/ru/post/1498431/


All Articles