Keyboard scancode to virtual keycode

21 views
Skip to first unread message

John

unread,
Apr 8, 2024, 1:04:24 PMApr 8
to Ukelele Users
Hi,

Is there any information out there regarding how keyboard scan codes are mapped to virtual key codes? As far as I understand it, keyboards will emit a scan code that will be translated into virtual key codes. These virtual key codes will be used along with the hardware id of the keyboard and the user's defined keyboard (keyboard layout) to generate output. However, its not clear what the initial mapping from scan code to virtual key code is.

Gé van Gasteren

unread,
Apr 8, 2024, 2:17:13 PMApr 8
to ukelel...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Ukelele Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ukelele-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ukelele-users/84f1b7ac-f2e3-43a0-8def-7c49871ec18cn%40googlegroups.com.

John

unread,
Apr 8, 2024, 5:49:33 PMApr 8
to Ukelele Users
Scan codes are what is sent by the keyboard. I've seen references in OSX documentation about virtual key codes being an intermediate representation for a key event. 

> In macOS, the hardware scan codes generated by keyboards are mapped to a set of virtual key codes that are hardware-independent. Pressing a given key always generates the same virtual key code on any supported keyboard.

> As keys are pressed, the system uses the virtual key codes to create low-level keyboard events. For information on how to simulate a keyboard event, see the function init(keyboardEventSource:virtualKey:keyDown:).

> The property’s value is hardware-independent.

I'm not sure if the Events.h below supports this notion or not. The 3 keyboard types, ANSI, ISO, and JIS, are likely to be sending scan codes based on some standard (or maybe not!).



/*
 *  Summary:
 *    Virtual keycodes
 *  
 *  Discussion:
 *    These constants are the virtual keycodes defined originally in
 *    Inside Mac Volume V, pg. V-191. They identify physical keys on a
 *    keyboard. Those constants with "ANSI" in the name are labeled
 *    according to the key position on an ANSI-standard US keyboard.
 *    For example, kVK_ANSI_A indicates the virtual keycode for the key
 *    with the letter 'A' in the US keyboard layout. Other keyboard
 *    layouts may have the 'A' key label on a different physical key;
 *    in this case, pressing 'A' will generate a different virtual
 *    keycode.
 */

There is Technical Note TN2450 (https://developer.apple.com/library/archive/technotes/tn2450/_index.html), which has a table containing "a list of keyboard usages and their usage IDs for use in key remapping. This list is from the USB HID Usage Tables Specification, Section 10 Keyboard /Keypad Page"

John

unread,
Apr 9, 2024, 3:25:17 AMApr 9
to Ukelele Users
From the "Inside Macintosh" PDF, it looks like this is/used to be the Key-Map Resource (Type 'KMAP')

"The key-map resource (resource type 'KMAP') is used for converting the raw key codes
produced by a keyboard’s microprocessor into hardware-independent virtual key codes.
There is one key-map resource per physical keyboard on a Macintosh; it belongs to the
Operating System, not to any script system."
Reply all
Reply to author
Forward
0 new messages