Re: Key codes and characters in NSEvents
Re: Key codes and characters in NSEvents
- Subject: Re: Key codes and characters in NSEvents
- From: Allan Odgaard <email@hidden>
- Date: Fri, 19 Mar 2004 00:56:09 +0100
On 18. Mar 2004, at 21:06, Darrin Cardani wrote:
the key information by calling [ theEvent charactersIgnoringModifiers
]. The problem is that the character for non-printing keys is not what
I want to display. For example, left arrow comes back as an Asian
character.
Yes, you'll need to map the key codes to unicode character points
manually for those keys which are normally not inserted into the text
stream.
Below is the table I use to show the keys -- the "pb_enter" is because
IB and PB differs in which glyph they use for enter (I did this before
System Prefs also got the ability to customize keys, so I do not know
which glyph that one uses), and the same is also the case with the
arrow keys (where IB uses some other arrows).
If you also want a glyph for help you can use "unichar str[] = { '?',
0x20DD };" which puts ? into an enclosing circle combining glyph.
You might also want to spell out space and non-breaking-space.
static const struct { char const* Name; unichar Code; } KeyGlyphs[] =
{
{ "pb_enter", 0x2324 },
{ "left", 0x2190 },
{ "up", 0x2191 },
{ "right", 0x2192 },
{ "down", 0x2193 },
{ "ib_left", 0x21E0 },
{ "ib_up", 0x21E1 },
{ "ib_right", 0x21E2 },
{ "ib_down", 0x21E3 },
{ "home", 0x2196 },
{ "end", 0x2198 },
{ "return", 0x21A9 },
{ "pageup", 0x21DE },
{ "pagedown", 0x21DF },
{ "tab", 0x21E5 },
{ "backtab", 0x21E4 },
{ "shift", 0x21E7 },
{ "control", 0x2303 },
{ "enter", 0x2305 },
{ "command", 0x2318 },
{ "modifier", 0x2325 },
{ "backspace", 0x232B },
{ "delete", 0x2326 },
{ "escape", 0x238B },
{ "numlock", 0x2327 },
{ "help", 0x225F }
};
If I use [ theEvent keyCode ], I can get the key code. But how do I
convert the key code into a character I can display to the user?
Here is a table to map the key code into a name (which you can then use
with the table above). For key codes not in the table, the
'charactersIgnor....' should work.
static const struct { unsigned short Code; char const* Name; }
Keys[] =
{
{ NSUpArrowFunctionKey, "up" },
{ NSDownArrowFunctionKey, "down" },
{ NSLeftArrowFunctionKey, "left" },
{ NSRightArrowFunctionKey, "right" },
{ NSDeleteFunctionKey, "delete" },
{ NSHomeFunctionKey, "home" },
{ NSEndFunctionKey, "end" },
{ NSPageUpFunctionKey, "pageup" },
{ NSPageDownFunctionKey, "pagedown" },
{ NSClearLineFunctionKey, "numlock", },
{ NSHelpFunctionKey, "help", },
{ '\t', "tab" },
{ '\r', "return" },
{ '\003', "enter" },
{ '\031', "backtab" },
{ '\033', "escape" },
{ '\177', "backspace" },
};
I'm also having a problem getting command key combinations. If the
user is pressing the command key, my window controller never gets the
event. Is there a way to also get those events?
Yes, you'll need to override the sendEvent: method in your application
class and have that send you the keys directly, as they will otherwise
be swallowed by menu items or similar.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.