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

Find scan code from key event



  • Hello,

    Is there a way to find which scanCode caused the generation of a key event?

    I am working on a companion app for a keyboard with programmable keys and I need to provide a way for the user "record" key presses. So that I can program the keyboard to resend the same keys...
    But the keyboard sends scan codes, not "text", so I need to do a reverse conversion...

    For example, when the "A" key is pressed, it means that I need to send a code 0x04 (USB scan code for A key)... But a "simple" 1:1 association does not work due to keyboard locales (on a French keyboard, for example, the key "A" and "Q" keys are inverted, so sending a 0x04 would result in a "Q", not an A!)

    Hence my question, how can I do a reverse lookup from a keyEvent to the originating scan codes?

    Thanks,
    Cyrille



  • Hello Again,

    Alternatively, is there a way to get the raw input data (not key events)? this would achieve the same result.

    Cyrille


  • Lifetime Qt Champion

    Hi,

    Isn't that what QKeyEvent provides you ?



  • @Cyrille-de-Brebisson Hi,

    You could probably store the key sequence (QKeyEvent::key()) as well as "modifiers" (QKeyEvent::modifiers()) and then replay them using QCoreApplication::postEvent

    Here is an example (not tested) inspired from https://stackoverflow.com/questions/2035310/how-can-i-simulate-user-interaction-key-press-event-in-qt:

    for(const QPair<int, Qt::KeyboardModifiers> &keyData : sequence){
        QKeyEvent *pressEvent = new QKeyEvent ( QEvent::KeyPress, keyData.first, keyData.second);
        QKeyEvent *releaseEvent = new QKeyEvent ( QEvent::KeyRelease, keyData.first, keyData.second);
        QCoreApplication::postEvent (receiver, pressEvent);
    
        // Let make it more "human" :). You can adjust this or remove it according 
        // to your needs. You may also store  the time between keys when 
        // you record your sequence to reproduce the macro with same timings. 
        // That's up to you
        QThread::msleep(250); 
        QCoreApplication::postEvent (receiver, releaseEvent);
    }
    


  • Hello,

    A couple of questions...
    how do I get the QKeyEvent in qml?
    QKeyEvent has a scanCode, but this does not apear to be a USB scan code as far as I can tell...
    The scanCode does not seem to work on MacOS, at least according to the documentation...

    Cyrille


Log in to reply