Re: keyCode translation for various keyboards
Re: keyCode translation for various keyboards
- Subject: Re: keyCode translation for various keyboards
- From: John Joyce <email@hidden>
- Date: Sat, 6 Feb 2010 13:47:50 -0600
On Feb 3, 2010, at 8:23 AM, email@hidden wrote:
> Dave Carpeneto wrote:
>> Hello list folk - I'm trying to print out strings representing various
>> keyDown events I receive. Using OS 10.6.2.
> ...
>> So far the two approaches I've seen are as follows:
> ...
>> Approach #2: Get what you can via UCKeyTranslate, and then 'fill in
>> the blanks' with a lookup table for non-alphanumeric stuff (example:
>> http://inquisitivecocoa.com/2009/04/05/key-code-translator , which
>> looks like it builds off a patched version of
>> http://rogueamoeba.com/sources/HotKeysLib3.zip )
>>
>> Prior threads by people in the know (
>> http://www.cocoabuilder.com/archive/cocoa/101991-key-codes-and-characters-in-nsevents.html
>> ) further state that developer-created lookups are a fact of life in
>> this area, but I'm reely hoping they are not ...
> OK, given that even the up-to-date code snippets I can find on the
> interwebs ( http://code.google.com/p/shortcutrecorder ) do the lookup
> thing I'm guessing that there's no better way to go about it. Which is a
> shame, as I'm still freaked out about the possibilities that
> international keyboards may present. But at least I'm pretty confident
> this is what everyone else is doing, so "as bad as everyone else" is
> good enough I guess ;-)
>
> On the off chance that someone else bangs their head against this
> situation here's the mapping table I've come up with, which gives pretty
> unicode symbols where possible:
>
> // keycode mappings from http://ritter.ist.psu.edu/projects/RUI/macosx/rui.c
> // unicode symbols from
> https://wincent.com/wiki/Unicode_representations_of_modifier_keys
> // more unicode @
> http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/KEYBOARD.TXT
>
> keyCodeToStringDict = [[NSDictionary alloc] initWithObjectsAndKeys:
>
> @"\u23CE", [NSNumber numberWithInteger: 36], // Ret
> @"\u21E5", [NSNumber numberWithInteger: 48], // Tab
> @"\u2423", [NSNumber numberWithInteger: 49], // Space
> @"\u232B", [NSNumber numberWithInteger: 51], // <-Del
> @"\u2324", [NSNumber numberWithInteger: 52], // Enter
> @"\u238B", [NSNumber numberWithInteger: 53], // Esc
> @"\u2327", [NSNumber numberWithInteger: 71], // Clear
> @"\u2324", [NSNumber numberWithInteger: 76], // Enter
>
> @"\uF860F1", [NSNumber numberWithInteger:122], // F1
> @"\uF860F2", [NSNumber numberWithInteger:120], // F2
> @"\uF860F3", [NSNumber numberWithInteger: 99], // F3
> @"\uF860F4", [NSNumber numberWithInteger:118], // F4
> @"\uF860F5", [NSNumber numberWithInteger: 96], // F5
> @"\uF860F6", [NSNumber numberWithInteger: 97], // F6
> @"\uF860F7", [NSNumber numberWithInteger: 98], // F7
> @"\uF860F8", [NSNumber numberWithInteger:100], // F8
> @"\uF860F9", [NSNumber numberWithInteger:101], // F9
> @"\uF861F10",[NSNumber numberWithInteger:109], // F10
> @"\uF861F11",[NSNumber numberWithInteger:103], // F11
> @"\uF861F12",[NSNumber numberWithInteger:111], // F12
> @"\uF861F13",[NSNumber numberWithInteger:105], // F13
> @"\uF861F14",[NSNumber numberWithInteger:107], // F14
> @"\uF861F15",[NSNumber numberWithInteger:113], // F15
> @"\uF861F16",[NSNumber numberWithInteger:106], // F16
> @"\uF861F17",[NSNumber numberWithInteger: 64], // F17
> @"\uF861F18",[NSNumber numberWithInteger: 79], // F18
> @"\uF861F19",[NSNumber numberWithInteger: 80], // F19
>
> @"?\u20DD", [NSNumber numberWithInteger:114], // Help
> @"\u21F1", [NSNumber numberWithInteger:115], // Home
> @"\u21DE", [NSNumber numberWithInteger:116], // PgUp
> @"\u2326", [NSNumber numberWithInteger:117], // Del->
> @"\u21F2", [NSNumber numberWithInteger:119], // End
> @"\u21DF", [NSNumber numberWithInteger:121], // PgDn
>
> @"\u2191", [NSNumber numberWithInteger:126], // Up
> @"\u2193", [NSNumber numberWithInteger:125], // Down
> @"\u2190", [NSNumber numberWithInteger:123], // Left
> @"\u2192", [NSNumber numberWithInteger:124], // Right
> nil];
>>
>> Questions:
>>
>> 1) Is there a programmatic method to get strings like this (I'm
>> guessing not)
>>
>> 2) Is a F1 key the same on all keyboards (if so then my worries
>> about the lookup table are a non-issue)
>>
>> 3) Are there any keys typical in other places that don't exist on a
>> 'standard' North American keyboard ? (if so heaven help me)
Yes. There are.
Japanese Keyboards will have a couple of keys for specific input switching.
You will find frequently that the result of a key press plus a modifier key or modifier keys will produce totally different results in many locales.
The question arises, what are you trying to accomplish??
Most things these days would look at what the USB device sends. All mac keyboards, even built in ones are on a USB bus.
They do also emit "Mac Key Codes" but the layout of keyboards are different accross different models even just in the USA.
example: black macbook F keys and their system control functionality.
External keyboards usually do not have Fn key.
In general, you should avoid any assumption that keyboards will behave in any consistent manner.
Locale, locale, locale.
Models are also different...
In Cocoa apps it is probably enough to look at what is passed in the responder chain.
Best to take the most hi-level abstract but available approach.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden