[NSEvent keyCode] to actual keys?

Posts: 5
Joined: 2007.08
Post: #1
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?
Quote this message in a reply
Posts: 1,403
Joined: 2005.07
Post: #2
Just take the first char out of - (NSString *)characters instead.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Posts: 834
Joined: 2002.09
Post: #3
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.
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #4
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.
Quote this message in a reply
Posts: 100
Joined: 2006.05
Post: #5
http://onesadcookie.com/trac/browser/Gam...KeyCodes.c. 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...
Quote this message in a reply
Posts: 5
Joined: 2007.08
Post: #6
Nevada Wrote:http://onesadcookie.com/trac/browser/Gam...KeyCodes.c. 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
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #7
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.
Quote this message in a reply
Posts: 100
Joined: 2006.05
Post: #8
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....
Quote this message in a reply
Posts: 5
Joined: 2007.08
Post: #9
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Spaces on Snow Leopard and ctrl/arrow keys GolfHacker 19 27,671 Dec 4, 2012 03:18 PM
Last Post: bmantzey
  Multiple Keys down in SDL silver9172 2 7,546 May 30, 2009 06:16 PM
Last Post: scgames
  Starting to program an actual game ferum 10 11,997 Sep 28, 2007 12:27 AM
Last Post: Oibalf
  Keys ......again Coin 7 9,289 Jan 29, 2005 09:38 PM
Last Post: belthaczar
  Keys I Can Use In Games Nick 10 8,558 Jul 22, 2004 11:49 PM
Last Post: AnotherJake