Search code examples
qtkeyboardqt4qkeyevent

Get key position of QKeyEvent (multiplatform)


In order to make position-based keyboard shortcuts, that work for all keyboard layouts, I need to identify a physical keyboard key, without the keyboard layout on top of it. Ideally, I would like to retrieve the scancode of the QKeyEvent, because that is not keyboard layout-dependent.

For example, if I would want to map Shift+Q to volume-down and Shift+W to volume-up (two keys which are next to each other on a US keyboard layout) I could just map these to Shift+0x10 and Shift+0x11, according to this scancode set. This way, it will also work for French keyboard layouts, where the Q and the A are switched. And it will even work for DVORAK keyboard layouts or other exotical keyboard layouts, because the keyboard layout will be just ignored.

Now, the question is: How can I retrieve QKeyEvent's scancode, or any other parameter telling about the physical key's position, ignoring the keyboard layout, that will work on all platforms? I want it to work on Windows, Linux and OSX.

QKeyEvent::nativeScanCode() gets the job done, but it doesn't work for OSX :\


Solution

  • You already have the most multiplatform way of doing it. Except that Qt hasn't implemented it for OS X yet, at least as of Qt 5.7.

    Feel free to patch your copy of Qt to include a HID event tap to get the scancodes, see e.g. this question.