1
votes

I want to detect where a pressed key physically is on a Mac keyboard. Looking at the keycode tells me the char etc. but not the location, which varies with language/keyboard layout. I'd need something like row indexes for the key.

I'm able to look at the keyboard layout language using this code:

TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
NSLog(@"localized name: %@", TISGetInputSourceProperty(source, kTISPropertyLocalizedName));

From which I could map location through building keycode location dictionaries for each language, but that could be a lot of work to cover most keyboards.

Any ideas for a shortcut?

1

1 Answers

3
votes

You are incorrect about the key code. On OS X, the virtual key code corresponds to the key position. It does not indicate which character would be generated by the keyboard layout.

For example, key code 0 is kVK_ANSI_A. What this means is not that it is the key which produces the "A" character. It means it's the key which is in the position that the "A" key is on an ANSI standard keyboard. It's the leftmost key in the middle row of letter keys.

When key code 0 is translated through the French keyboard layout with no modifiers, it produces the "q" character.