2

In my application, I need to map a shortcut to the key to the left of the "1" key on the keyboard. On a standard US keyboard, this would be the backtick character (" ` "), which is key code number 50. Unfortunately, international keyboards (the French keyboard, for example) has a different key to the left of the 1 key (the forward slash key "/"), so hard coding that key code would result in unexpected results for users who are not using a US keyboard.

Is there any way to convert a US key code into a key code for international keyboards at runtime, or a way to programatically determine the key code based on the position of the key on the keyboard?

Conrad Meyer
  • 2,851
  • 21
  • 24
indragie
  • 18,002
  • 16
  • 95
  • 164
  • Correct me if I'm wrong, but this sounds like an Objective-C question, not a C question. Re-tagged as such (and fixed the spelling of 'obejctive-c'). – Conrad Meyer Mar 10 '11 at 01:52
  • I tagged it as both because the Carbon text input APIs are written in C, but there might be a solution in Objective-C as well. – indragie Mar 10 '11 at 23:52
  • Is Carbon supported anymore? http://en.wikipedia.org/wiki/Carbon_(API) makes it sound like the answer is 'no'. – Conrad Meyer Mar 11 '11 at 05:50
  • 2
    There are still certain parts of the Carbon APIs that have no equivalent in Cocoa and are still supported by Apple (the keyboard events APIs, for example). – indragie Mar 11 '11 at 15:10
  • Get my code here: [http://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode/14529841#14529841][1] [1]: http://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode/14529841#14529841 – TONy.W Jan 25 '13 at 20:34

1 Answers1

3

The character on the key to the left of "1" is different on different keyboard layouts, but the virtual key code should be the same. If you look at HIToolbox/Events.h, you can see the constant kVK_ANSI_Grave, which represents the key you're talking about; above the list of constants, there's a comment that suggests that at the virtual key code level, equality means that the physical key is the same, though the scan code might be different and the emitted letter might be different.

In other words: the keyboard driver maps from scan codes to virtual key codes, and the keyboard layout (which you can change in System Preferences) maps from virtual key codes to characters.

This is all potentially wrong; I don't have a non-US keyboard with which to verify these assertions.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • That brings me to the question, are the key codes that the Carbon Keyboard Events API uses based on virtual codes mentioned above? Or are they hard coded to that specific character? – indragie Mar 14 '11 at 05:01
  • I believe `CGKeyCode` is, indeed, the same virtual key code used virtually everywhere in Mac OS X. The fact that both that list and your observation use decimal 50 to represent the grave key is a positive sign. – John Calsbeek Mar 14 '11 at 05:06
  • The problem is, I'm using that key code in a shipping application and I've had several bug reports that it's actually just mapping it to the ` character and not the position of the key on the keyboard. – indragie Mar 14 '11 at 14:04
  • The trouble is, scan codes don't guarantee the physical position either. You might just have to make it customizable. – John Calsbeek Mar 15 '11 at 01:53
  • You're right, the `Events.h` header contains a list of key codes that are independent of the keyboard layout, but unfortunately, the grave key is not one of them. – indragie Mar 15 '11 at 23:51