2013-08-22 18 views
4

從NSMenuItem類參考如何設置功能鍵等效鍵編程

如果要指定Backspace鍵作爲等效鍵菜單項,請使用一個字符串,NSBackspaceCharacter(在NSText定義.h作爲0x08)和正向刪除鍵,使用NSDeleteCharacter(在NSText.h中定義爲0x7F)。

不知道我明白「使用單個字符串......」從類ref。

//可正常工作

NSString *s = [NSString stringWithFormat:@"%c",NSDeleteCharacter]; 

    [myMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; 

    [myMenuItem setKeyEquivalent:s]; 

enter image description here

//這並不按預期工作

NSString *s = [NSString stringWithFormat:@"%c",NSF2FunctionKey]; 

    [myMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; 

    [myMenuItem setKeyEquivalent:s]; 

enter image description here

回答

1

想通了自己。

unichar c = NSF2FunctionKey; 

    NSString *f2 = [NSString stringWithCharacters:&c length:1]; 

    [mi setKeyEquivalent:f2]; 
    [mi setKeyEquivalentModifierMask:NSCommandKeyMask]; 

enter image description here

4

爲夫特2.0實施例:

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

夫特3:

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

有點兒討厭如何我不能只把一個'Int'成一個「字符」直接... –