Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Questions about QKeyEvent's native scan code



  • Hi,

    I am currently trying to move away from the limited Qt::Key to native scan codes, because that way I can perfectly track all the pressed keys in a set, without having to care about modifiers and sequences (that I don't need in my use case anyway).

    The only thing I'm having trouble with is mapping the existing Qt::Keys to a native scan code outside of keyPressEvent.

    I tried creating a QKeyEvent(QEvent::Type::KeyPress, key, Qt::KeyboardModifier::NoModifier) and calling nativeScanCode() on it, but this always returns 0, while calling the same function on a QKeyEvent passed to a keyPressEvent returns the correct value.

    To explain:
    I am saving a key config of a game to a file, using QKeySequence(keyEvent->key()).toString(QKeySequence::NativeText) in order to make it readable for the user, and then load the Qt::Key it with QKeySequence(name)[0]. I now simply need to convert these strings or keys to a native scan code.

    Alternatively I could save the native scan code, but then I'd have to convert that to a readable string in the gui.



  • The Qt Key Event is going to map to the native windowing system. So, your mapping is going to be dependent upon the keysym definitions of that windowing system. Windows, MAC, and Unix/X11 may have different mechanisms for receving keyboard input. Don't plan on your hack to be portable.


Log in to reply