Search code examples
cocoaeventskeyboard

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


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 keyCode property, which is defined as unsigned short. The following code, when placed in an appropriate UI object, will echo the key codes as they are pressed:

- (void)keyDown:(NSEvent *)theEvent
{
    NSLog(@"%d", [theEvent keyCode]);
}

From this code, I can easily see which codes match certain keys, but I would like to find an official document somewhere that lists all of them. I expected Apple to have a header file somewhere that looked like this:

enum {
    ...
    NSKeyCodeLeftArrow = 123,
    NSKeyCodeRightArrow = 124,
    ...
};

But if there is one, I have yet to find it.


Solution

  • 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.)