View Full Version : Number pad
Mark Levin
2004.01.05, 08:16 PM
What's the best way to distinguish between the number keys on the keypad and the number keys above the letters? Right now, I'm using Cocoa events, and the NSNumericPadKeyMask modifier flag doesn't allow the two sets of number keys to be used simultaneously
OneSadCookie
2004.01.05, 08:27 PM
Use the key code. You can find a list in various places on the web, or just use NSLog to figure 'em out yourself...
Apple says that the key codes are hardware-dependent, in practice though, it's fairly safe -- just be aware that in different locales, the keys have slightly different key caps (eg. Q and A switched on german keyboards).
If you use the HID manager, you get similar properties from the scancodes, but slightly stronger guarantees of hardware independence.
imported_kelvin
2004.01.06, 01:48 AM
Actually from the Cocoa docs, apple says "Its value is hardware-independent." What I would recommend is, when you configure controls with the NSEvents, grab the keyCode and use that to control the game. That way you can keep the setup human-readable while still getting the accuracy of the keycodes.
NSEvent docs (file:///Developer/Documentation/Cocoa/Reference/ApplicationKit/ObjC_classic/Classes/NSEvent.html#//apple_ref/doc/uid/20000016/keyCode)
NCarter
2004.01.06, 02:53 AM
I do it like this:
switch(ascii_character)
{
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '.': case '+':
case '-': case '*': case '/': case '=':
if(raw_key_code >= 0x41)
{
// It's on the numeric keypad
}
else
{
// It's on the main keyboard
}
break;
default:
// It can't be a keypad key anyway, unless it's the
// Enter or Clear key
break;
}
That's for Carbon - I don't know if the keycodes are reported differently for Cocoa. I'd imagine it's the same.
This code only tries to distinguish whether numbers and punctuation are on the keypad or main keyboard. The Enter and Clear keys have their own distinct keycodes (0x4C and 0x47 respectively), so you can detect those separately in any case.
While I'm posting, you might find the following list useful - it's my localisable strings file for keycap names. (Sorry about the incredibly long post!)
"0x31" = "Space";
"0x24" = "Return";
"0x30" = "Tab";
"0x33" = "Backspace";
"0x35" = "Escape";
"0x7A" = "F1";
"0x78" = "F2";
"0x63" = "F3";
"0x76" = "F4";
"0x60" = "F5";
"0x61" = "F6";
"0x62" = "F7";
"0x64" = "F8";
"0x65" = "F9";
"0x6D" = "F10";
"0x67" = "F11";
"0x6F" = "F12";
"0x69" = "F13";
"0x6B" = "F14"; /* F14 and F15 seem to be unusable on Mac OS X */
"0x71" = "F15";
/* Anybody know the keycode for F16? */
"0x7B" = "Left Arrow";
"0x7D" = "Down Arrow";
"0x7C" = "Right Arrow";
"0x7E" = "Up Arrow";
"0x72" = "Help";
"0x75" = "Delete";
"0x73" = "Home";
"0x77" = "End";
"0x74" = "Page Up";
"0x79" = "Page Down";
"0x52" = "Keypad 0";
"0x41" = "Keypad .";
"0x4C" = "Keypad Enter";
"0x53" = "Keypad 1";
"0x54" = "Keypad 2";
"0x55" = "Keypad 3";
"0x56" = "Keypad 4";
"0x57" = "Keypad 5";
"0x58" = "Keypad 6";
"0x45" = "Keypad +";
"0x59" = "Keypad 7";
"0x5B" = "Keypad 8";
"0x5C" = "Keypad 9";
"0x4E" = "Keypad -";
"0x47" = "Keypad Clear";
"0x51" = "Keypad =";
"0x4B" = "Keypad /";
"0x43" = "Keypad *";
"0x0A" = "Mystery Key"; /* Below the escape key, keycap varies a lot */
"0x36" = "Control";
"0x3A" = "Option";
"0x37" = "Command";
"0x38" = "Shift";
"0x39" = "Caps Lock";
Fenris
2004.01.06, 04:36 AM
*Plugging Key Code Capper*
I wrote this little thingy a while ago, and I quite like it for quickly discerning key codes, although it is rather obliterated by NCarter's above table. :)
http://www.rusted.se/keycodecapper.sit
NCarter
2004.01.06, 04:10 PM
*Plugging Key Code Capper*
I wrote this little thingy a while ago, and I quite like it for quickly discerning key codes, although it is rather obliterated by NCarter's above table. :)
Thanks, I meant to ask you about this... I could have saved myself a lot of time if I had thought about it sooner! :cry:
Hey, it reports my left and right modifier keys as having separate key codes! I thought that didn't apply to recent keyboards!
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.