iDevGames Forums
[NSEvent keyCode] to actual keys? - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: [NSEvent keyCode] to actual keys? (/thread-3063.html)

[NSEvent keyCode] to actual keys? - teknein - Aug 30, 2007 06:54 AM

Ok, so pretty much what I'm looking for is a reference table or something from the values returned by NSEvent's -keyCode method to the keys that they reference.

On the apple website they have a NSEvent Class Reference and in that they have this to say about the keyCode method:

- (unsigned short)keyCode

Return Value
The virtual key code. The returned value is hardware-independent. The value returned is the same as the value returned in the kEventParamKeyCode when using Carbon Events.

... and this is where I get lost.... where do I find these "virtual key code"s?

[NSEvent keyCode] to actual keys? - unknown - Aug 30, 2007 06:59 AM

Just take the first char out of - (NSString *)characters instead.

[NSEvent keyCode] to actual keys? - Fenris - Aug 30, 2007 07:32 AM

For the modifier keys, you do what unknown said, and examine the modifier flags.

// These are for modifier keys (Ctrl, Shift, Command). Command is reported as kInputKeyMeta.
        case NSFlagsChanged:
            static unsigned int oldFlags = 0;
            unsigned int flags = [event modifierFlags];    // Grab the modifier bitmask
            // Then just check if the given modifier changed state, and if so record it along with its current state.
            if ((flags & NSShiftKeyMask) != (oldFlags & NSShiftKeyMask))
                key.key = kInputKeyShift;
                key.up = !(flags & NSShiftKeyMask);
            else if ((flags & NSAlphaShiftKeyMask) != (oldFlags & NSAlphaShiftKeyMask))    // Caps lock
                key.key = kInputKeyCapsLock;
                key.up = !(flags & NSAlphaShiftKeyMask);
            else if ((flags & NSCommandKeyMask) != (oldFlags & NSCommandKeyMask))
                key.key = kInputKeyMeta;
                key.up = !(flags & NSCommandKeyMask);
            else if ((flags & NSAlternateKeyMask) != (oldFlags & NSAlternateKeyMask))    // Alternate is Alt/Opt
                key.key = kInputKeyAlt;
                key.up = !(flags & NSAlternateKeyMask);
            else if ((flags & NSControlKeyMask) != (oldFlags & NSControlKeyMask))
                key.key = kInputKeyCtrl;
                key.up = !(flags & NSControlKeyMask);
            else if ((flags & NSNumericPadKeyMask) != (oldFlags & NSNumericPadKeyMask))        // Num lock
                key.key = kInputKeyNumLock;
                key.up = !(flags & NSNumericPadKeyMask);
            oldFlags = flags;
        } break;
        // Regular keydowns
        case NSKeyDown:
            if ([self checkForSystemMnemonic: event]) break;    // Allow the OS to have a go at possible Cmd-Q, Cmd-S etc combos
        case NSKeyUp:
            if ([event isARepeat]) break;    // We don't like held keys
            unichar unicharacter;
            NSString *characters;
            characters = [event characters];    // Grab the raw character
            if ([characters length] == 0) break;                // If there wasn't one (for instance, if we're typing ë), skip it.
            unicharacter = [characters characterAtIndex:0];        // Grab the unicode char for the key
            key.key = [self mapCharToKeysym: unicharacter];        // Figure out what key symbol that unichar represented
            key.unicodeChar = unicharacter;                        // Record the unichar
            key.up = ([event type] == NSKeyUp) ? true : false;    // And the state.
        } break;

...where macCharToKeysym is a custom method to map unichars to my own keyboard enums - substitute your own.

[NSEvent keyCode] to actual keys? - ThemsAllTook - Aug 30, 2007 07:34 AM

I was never able to find such a list, but it's easy enough to make your own. Just write a simple program that prints out the -keyCode for each keypress, and press every key on your keyboard.

[NSEvent keyCode] to actual keys? - Nevada - Aug 30, 2007 10:34 AM This is a rather complete list of keycodes for the mac and is what I use... corresponding keycodes are commented on the left in hex so you'll have to format it yourself unless you use his other two keycode headers in the 'Common' source folder...

[NSEvent keyCode] to actual keys? - teknein - Aug 30, 2007 12:13 PM

Nevada Wrote: This is a rather complete list of keycodes for the mac and is what I use...

woot, awesome! that is exactly what I was lookin for. I knew it was out there somewhere, I just didn't know where to look. thanks again Smile

[NSEvent keyCode] to actual keys? - OneSadCookie - Aug 30, 2007 01:14 PM

Note that these relate to physical keyboard locations, not the key caps that are printed. For example, a French keyboard has the Q and A keys flipped, and GS_KEYBOARD_A is the Q key. Obviously, this is appropriate for certain kinds of games (FPS) and inappropriate for others (RTS). Be sure to use the keycode when that's what you're after, and the characters when they're what you're after.

My list is apparently incomplete, too. If anyone has keys I'm missing, I'd like to hear of them. In particular, the new iMac keyboard is likely to have a bunch that have never been used before, and a JIS keyboard is likely to have at least one that I don't include.

[NSEvent keyCode] to actual keys? - Nevada - Aug 30, 2007 01:34 PM

I actually like the fact that they relate to the physical locations because I code in DVORAK and it's a pain to have to switch back to QWERTY to play the game....

[NSEvent keyCode] to actual keys? - teknein - Sep 5, 2007 07:27 AM

actually, I was looking to use it for mapping keys(locations of the keys work fine too) to events that I have pre setup.
example: lets say I have a camera object, it register a couple events with the event handler(lets say [someEventHandler addEvent:@"strife-left" forObj:self]) without any inital key setup.

then have a keymap that saves which keys you set(lets say we have it so you click on the event name in a tableview, then save the keyCode for the next event matching NSKeyDown)

finally, the event handler receives events while the game is running and asks the keymap for the name of the events associated with the keyCode( [keymap eventNameForKeyCode:[theEvent keyCode]])

thats the rough idea of what I'm lookin to do, although Im having a hard time trying to find the time to actually implement it, but one day in the next couple years im sure i'll have something to actually show everyone.