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

Issue sending space with QKeyEvents



  • I am trying to send keyboard keys with a push button press. Any other key I simulate works fine except the space key. I am on Qt 5.7.1. If I send the key using a physical keyboard in my textbox it works fine but my push button sends the word 'Space' rather than an actual space. Code:

        // key_val = the Qt Key enum ie Qt::Key_Space, Qt::Key_A, Qt::Key_Right etc
        QKeyEvent key_press =  QKeyEvent(QKeyEvent::KeyPress, key_val, Qt::NoModifier, QKeySequence(key_val).toString());
        QKeyEvent key_release = QKeyEvent(QKeyEvent::KeyPress, key_val, Qt::NoModifier);
    
        QCoreApplication::sendEvent(receiver, &key_press);
        QCoreApplication::sendEvent(receiver, &key_release);
    

    The above code works with every other key I have tried for key_val except for Qt::Key_Space (even Left, Right, Esc). The workaround I currently have is this:

       if(key_val == Qt::Key_Space)
        {
          key_press =  QKeyEvent(QKeyEvent::KeyPress, key_val, Qt::NoModifier, " ");
        }
    

    As you can see above, using the string " " instead of QKeySequence(key_val).toString() works properly.

    Thanks


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    That's because you are expecting the toString() method to return a char which is wrong. The method is clearly documented as return a representation of a sequence.



  • @SGaist said in Issue sending space with QKeyEvents:

    Hi and welcome to devnet,

    That's because you are expecting the toString() method to return a char which is wrong. The method is clearly documented as return a representation of a sequence.

    But the behavior of QKeyEvent (or the way the event is handled) seems inconsistent. If it works for Delete, ESC, Left, Right, etc (and those events do have text strings in them just like Space, not just single literal characters), then why would Space be treated any different?


  • Lifetime Qt Champion

    Hi
    Thats odd. on linux i send space as
    QKeyEvent(QKeyEvent::KeyPress, key_val, Qt::NoModifier, QChar(32));
    and it works.



  • Would this work:

    QChar(key_val)
    

    Space is defined as 0x20 in the enum which 32 decimal:
    https://doc.qt.io/archives/qt-4.8/qt.html#Key-enum



  • @fcarney said in Issue sending space with QKeyEvents:

    Would this work:

    QChar(key_val)
    

    Space is defined as 0x20 in the enum which 32 decimal:
    https://doc.qt.io/archives/qt-4.8/qt.html#Key-enum

    It looks like that works. I wonder why it works as a char and not a string?

    Thanks!



  • @benja said in Issue sending space with QKeyEvents:

    It looks like that works. I wonder why it works as a char and not a string?

    Because QKeySequence is returning a representation of the key. Not the unicode returned when the key is actually pressed. I am not exactly sure of the purpose of QKeySequence.


  • Lifetime Qt Champion

    @fcarney QKeySequence is usually used to create the sequence of key to associate with a QShortcut.


Log in to reply