Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue sending space with QKeyEvents
Forum Updated to NodeBB v4.3 + New Features

Issue sending space with QKeyEvents

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.5k 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.
  • B Offline
    B Offline
    benja
    wrote on last edited by
    #1

    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

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

      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.

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

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vgf89
        wrote on last edited by vgf89
        #3

        @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?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

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

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            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

            C++ is a perfectly valid school of magic.

            B 1 Reply Last reply
            0
            • fcarneyF fcarney

              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

              B Offline
              B Offline
              benja
              wrote on last edited by
              #6

              @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!

              1 Reply Last reply
              0
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #7

                @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.

                C++ is a perfectly valid school of magic.

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

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

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

                  1 Reply Last reply
                  1

                  • Login

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