Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Find scan code from key event
Qt 6.11 is out! See what's new in the release blog

Find scan code from key event

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 2.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Cyrille de Brebisson
      wrote on last edited by
      #2

      Hello Again,

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

      Cyrille

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Isn't that what QKeyEvent provides you ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Gojir4G 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Isn't that what QKeyEvent provides you ?

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by Gojir4
          #4

          @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);
          }
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cyrille de Brebisson
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved