Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSEvent -keyCode decoder ring?



That's because there are no constants for the majority of keys. Their value depends on their location on the keyboard. For example, the letter a on one keyboard layout may be the same place as the letter q on another, but they will both have the same keycode.

For the letters and punctuation, you need to use something like UCKeyTranslate() to translate them into strings.

For the other keys...

typedef struct {
    CFStringRef keyString;
    UInt16 keyCode;
    UniChar unicode;
    UInt32 glyph;
} GlyphTable;

const GlyphTable kGlyphTable[] =
{
{ NULL , 0x030 , 0x21E5 , kMenuTabRightGlyph},
{ NULL , 0x030 , 0x21E4 , kMenuTabLeftGlyph},
{ NULL , 0x04C , 0x2305 , kMenuEnterGlyph},
{ NULL , 0x000 , 0x2423 , kMenuSpaceGlyph},
{ NULL , 0x033 , 0x2326 , kMenuDeleteRightGlyph},
{ NULL , 0x075 , 0x232B , kMenuDeleteLeftGlyph},
{ NULL , 0x024 , 0x21A9 , kMenuReturnGlyph},
{ NULL , 0x024 , 0x21AA , kMenuReturnR2LGlyph},
{ NULL , 0x024 , 0x21B5 , kMenuNonmarkingReturnGlyph},
{ NULL , 0x000 , 0x21E3 , kMenuDownwardArrowDashedGlyph},
{ NULL , 0x000 , 0x21E0 , kMenuLeftArrowDashedGlyph},
{ NULL , 0x000 , 0x21E1 , kMenuUpArrowDashedGlyph},
{ NULL , 0x000 , 0x21E2 , kMenuRightArrowDashedGlyph},
{ NULL , 0x035 , 0x238B , kMenuEscapeGlyph},
{ NULL , 0x047 , 0x2327 , kMenuClearGlyph},
{ NULL , 0x074 , 0x21DE , kMenuPageUpGlyph},
{ NULL , 0x079 , 0x21DF , kMenuPageDownGlyph},
{ NULL , 0x07B , 0x2190 , kMenuLeftArrowGlyph},
{ NULL , 0x07C , 0x2192 , kMenuRightArrowGlyph},
{ NULL , 0x07E , 0x2191 , kMenuUpArrowGlyph},
{ NULL , 0x07D , 0x2193 , kMenuDownArrowGlyph},
{ NULL , 0x073 , 0x2196 , kMenuNorthwestArrowGlyph},
{ NULL , 0x077 , 0x2198 , kMenuSoutheastArrowGlyph},
{ CFSTR("<<Help Key>>") , 0x072 , 0x0 , kMenuHelpGlyph},
{ NULL , 0x000 , 0x233D , kMenuPowerGlyph},
{ CFSTR("F1") , 0x07a , 0x0 , kMenuF1Glyph},
{ CFSTR("F2") , 0x078 , 0x0 , kMenuF2Glyph},
{ CFSTR("F3") , 0x063 , 0x0 , kMenuF3Glyph},
{ CFSTR("F4") , 0x076 , 0x0 , kMenuF4Glyph},
{ CFSTR("F5") , 0x060 , 0x0 , kMenuF5Glyph},
{ CFSTR("F6") , 0x061 , 0x0 , kMenuF6Glyph},
{ CFSTR("F7") , 0x062 , 0x0 , kMenuF7Glyph},
{ CFSTR("F8") , 0x064 , 0x0 , kMenuF8Glyph},
{ CFSTR("F9") , 0x065 , 0x0 , kMenuF9Glyph},
{ CFSTR("F10") , 0x06D , 0x0 , kMenuF10Glyph},
{ CFSTR("F11") , 0x067 , 0x0 , kMenuF11Glyph},
{ CFSTR("F12") , 0x06F , 0x0 , kMenuF12Glyph},
{ CFSTR("F13") , 0x069 , 0x0 , kMenuF13Glyph},
{ CFSTR("F14") , 0x03B , 0x0 , kMenuF14Glyph},
{ CFSTR("F15") , 0x071 , 0x0 , kMenuF15Glyph},
{ NULL , 0x000 , 0x23CF , kMenuEjectGlyph},
};



CFStringRef CopyStringForKeyCode(UInt16 keyCode)
{
unsigned int i;
if (keyCode==0)
return NULL; // We don't want to scan the glyph table with a zero keycode. The key with the zero keycode is 'a' on the QWERTY layout. Use UCKeyTranslate() for other keys not in this table.


for (i=0;i<sizeof(kGlyphTable) / (sizeof(GlyphTable));i++)
{
if (kGlyphTable[i].keyCode == keyCode)
{
if (kGlyphTable[i].unicode)
{
CFStringRef string=CFStringCreateWithCharacters(NULL, &(kGlyphTable[i].unicode), 1);
return string;
}
else if (kGlyphTable[i].keyString)
{
return CFRetain(kGlyphTable[i].keyString);
}
break;
}
}
return NULL;
}



Ack, at 7/13/06, Tom Harrington said:

Is there any sort of decoder information for the numbers returned by
NSEvent's -keyCode method?  The docs just note that the "...value
returned is the same as the value returned in the kEventParamKeyCode
when using Carbon Events."  But that doesn't seem to be documented or
decoded anywhere that I can find.

I can get codes for specific keys empirically by pressing them and
seeing what keyCode results, but I would have expected constants
somewhere.

--


Sincerely, Rosyna Keller Technical Support/Holy Knight/Always needs a hug

Unsanity: Unsane Tools for Insanely Great People

It's either this, or imagining Phil Schiller in a thong.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >NSEvent -keyCode decoder ring? (From: "Tom Harrington" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.