Search code examples
cocoaiokeycode

Where are all the Cocoa keycodes?


I'm trying to create a game with Cocoa, and I need to get user input. There doesn't seem to be an available list on all the keycodes. Am I supposed to be using keycodes? I found certain values such as NSUpArrowFunctionKey, but not for other keys. The much older question on this has answers that are all outdated, using Carbon, with broken links.


Solution

  • The virtual keycodes are declared in Events.h (/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h)

    Discussion:

    These constants are the virtual keycodes defined originally in Inside Mac Volume V, pg. V-191. They identify physical keys on a keyboard. Those constants with "ANSI" in the name are labeled according to the key position on an ANSI-standard US keyboard. For example, kVK_ANSI_A indicates the virtual keycode for the key with the letter 'A' in the US keyboard layout. Other keyboard layouts may have the 'A' key label on a different physical key; in this case, pressing 'A' will generate a different virtual keycode.