Where can I find a list of key codes for use with Cocoa's NSEvent class?

前端 未结 5 1430
无人及你
无人及你 2020-12-24 11:14

I\'m looking for a comprehensive list of the available key codes that can be used with Cocoa\'s NSEvent class. The NSEvent class has a keyCod

相关标签:
5条回答
  • 2020-12-24 11:18

    As far as I know, there is no enum or list of key codes. However, to get similar behavior, you can call interpretKeyEvents: in keyDown: which will call appropriate action methods, all of which are documented in NSResponder (e.g. moveLeft:, insertTab:, etc.)

    0 讨论(0)
  • 2020-12-24 11:26

    To include HIToolbox/Events.h (as mentioned in berrange's answer) in XCode 4 you just need to go to Link Binaries with Libraries and add the Carbon framework (which includes HIToolbox) and then import the main Carbon header in the file where you are checking the keyCodes.

    #import <Carbon/Carbon.h>
    

    It took me a second to figure out that I couldn't import HIToolbox/Events.h directly so I thought I'd post this in case it helps someone.

    0 讨论(0)
  • 2020-12-24 11:32

    According to this forum topic, the constants are available in HIToolbox/Events.h

    http://forums.macrumors.com/showthread.php?t=780577

    They have conveniently copy+pasted the entire set of constants into the forum too. Otherwise the header is available here:

    /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
    
    0 讨论(0)
  • 2020-12-24 11:35

    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.

    0 讨论(0)
  • 2020-12-24 11:37

    For Swift, import Carbon.HIToolbox.Events and then use the constants directly:

    import Carbon.HIToolbox.Events
    
    let keyCode = kVK_ANSI_A
    

    For a list of all codes, go to the definition of Carbon.HIToolbox.Events:

    0 讨论(0)
提交回复
热议问题