e.J*_*mes 45 keyboard events cocoa
我正在寻找可以与Cocoa NSEvent类一起使用的可用密钥代码的完整列表.的NSEvent类有一个keyCode属性,该属性被定义为unsigned short.以下代码放置在适当的UI对象中时,将在按下键代码时回显它们:
- (void)keyDown:(NSEvent *)theEvent
{
NSLog(@"%d", [theEvent keyCode]);
}
Run Code Online (Sandbox Code Playgroud)
从这段代码中,我可以很容易地看到哪些代码与某些键匹配,但我想找到一个列出所有代码的官方文档.我希望Apple有一个头文件,看起来像这样:
enum {
...
NSKeyCodeLeftArrow = 123,
NSKeyCodeRightArrow = 124,
...
};
Run Code Online (Sandbox Code Playgroud)
但如果有的话,我还没有找到它.
Pet*_*sey 30
Here you go. It's a map of all the virtual key-codes on the US extended keyboard layout, from the old Inside Macintosh: Text. Most of the key codes are still currently, although I suspect that the very newest Apple keyboards—those with media keys—may have changed a few of the function keys.
Note: ISO and non-extended keyboards may have different key codes for some keys. If you have such a keyboard, try Peter Maurer's Key Codes application. His site is down, so here's my copy.
小智 24
据我所知,没有枚举或密钥代码列表.但是,要获得类似的行为,您可以在keyDown中调用interpretKeyEvents:它将调用适当的操作方法,所有这些方法都记录在NSResponder中(例如moveLeft:,insertTab:等)
Ang*_*bes 12
要在XCode 4中包含HIToolbox/Events.h(如berrange的答案中所述),你只需要链接Binaries with Libraries并添加Carbon框架(包括HIToolbox),然后在你所在的文件中导入主Carbon头.检查keyCodes.
#import <Carbon/Carbon.h>
Run Code Online (Sandbox Code Playgroud)
我花了一秒钟才发现我无法直接导入HIToolbox/Events.h所以我想我会发布这个以防它帮助某人.
对于 Swift,导入Carbon.HIToolbox.Events然后直接使用常量:
import Carbon.HIToolbox.Events
let keyCode = kVK_ANSI_A
Run Code Online (Sandbox Code Playgroud)
有关所有代码的列表,请转到 的定义Carbon.HIToolbox.Events:
小智 6
根据这个论坛主题,常量可以在HIToolbox/Events.h中找到
http://forums.macrumors.com/showthread.php?t=780577
他们方便地将整套常量复制+粘贴到论坛中.否则标题可在此处获得:
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
Run Code Online (Sandbox Code Playgroud)