1

Is there a cocoa / carbon equivalent to the VkKeyScan and ToUnicode functions present on windows for translating between virtual scan codes and a unicode string? I would also like to perform this conversion vice versa (going from a char to a scan code).

cdyer
  • 1,220
  • 1
  • 12
  • 21

2 Answers2

1

I'm not sure there is, sadly. CGEvent and NSEvent (which aren't toll-free bridged, but NSEvent can convert back and forth) are the standard containers for events including key presses and as a result can convert from device-dependent scan codes to unicode string sequences but there seems to be no way to go in the other direction — from a unicode character to the required sequence of device-dependent key presses.

There's a brief comment underneath CGEventCreateKeyboardEvent that:

All keystrokes needed to generate a character must be entered, including modifier keys. For example, to produce a 'Z', the SHIFT key must be down, the 'z' key must go down, and then the SHIFT and 'z' key must be released:

But the sequence shown then has hardcoded and unexplained constants for the shift and z keys.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

UCKeyTranslate().

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
  • 2
    Doesn't that only convert from a virtual key code to a unicode string? – cdyer Sep 02 '11 at 21:10
  • 1
    As far as I can tell, this distinction is a Windows-specific piece of bad terminology, “virtual scan codes” being unvirtualized key codes. I don’t know where you’d _get_ “virtual scan codes” in this sense in Mac OS X. Possibly you’re dealing with HID usages, in which case the `kHID2VKC` table in my [KeyNaming](https://github.com/Ahruman/KeyNaming/blob/master/KeyNaming.c) library may be of interest. – Jens Ayton Sep 03 '11 at 08:31
  • Anyone coming here like I did, KeyNaming library is now [here](https://github.com/JensAyton/KeyNaming). – Hari Mahadevan Feb 25 '19 at 03:32