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. How to map Qt push button to keyboard key presses.
Forum Updated to NodeBB v4.3 + New Features

How to map Qt push button to keyboard key presses.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.8k Views 2 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.
  • T Offline
    T Offline
    trojan
    wrote on 28 Oct 2021, 18:03 last edited by
    #1

    Hi,

    I am having a widget with multiple push buttons with characters as name. I want my push button to be active(pressed) when I press a character in keyboard. ie, when I press 'a' in keyboard 'Push_button_A' should get pressed.

    Can anyone suggest me a working solution for it.

    Thankyou and regards.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Oct 2021, 18:06 last edited by mrjj
      #2

      Hi
      Welcome to the forums.

      Well use a std::map<QString, QPushButton *>
      Add all the buttons and its Letter to it.
      (you can use FindChildren if you placed them using UI)

      Set all buttons to Nofocus so they don't eat keys

      Override MainWindow KeypressEvent

      in keypressEvent function
      Look up the button, via the map, and set it to down / or up depending on its state

      Something like that :)

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Oct 2021, 18:07 last edited by
        #3

        Hi and welcome to devnet,

        Do you mean that pressing and releasing of the keyboard should be reflected on the button or that the press shall be the same as a mouse button click ?

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

        T 1 Reply Last reply 29 Oct 2021, 04:13
        0
        • S SGaist
          28 Oct 2021, 18:07

          Hi and welcome to devnet,

          Do you mean that pressing and releasing of the keyboard should be reflected on the button or that the press shall be the same as a mouse button click ?

          T Offline
          T Offline
          trojan
          wrote on 29 Oct 2021, 04:13 last edited by
          #4

          @SGaist Hi, The press of a keyboard key should reflect the same as a mouse button click on the bush button. And the the program written inside the slot of the push button should execute.

          Regards and Thankyou.

          J 1 Reply Last reply 29 Oct 2021, 04:23
          0
          • T trojan
            29 Oct 2021, 04:13

            @SGaist Hi, The press of a keyboard key should reflect the same as a mouse button click on the bush button. And the the program written inside the slot of the push button should execute.

            Regards and Thankyou.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 29 Oct 2021, 04:23 last edited by
            #5

            @trojan To catch the key press event subclass QWidget and override https://doc.qt.io/qt-5/qwidget.html#keyPressEvent
            In your keyPressEvent you then can then send the click event to the button:

            QApplication::sendEvent(button, &event);
            

            Or you can simply call the code you're calling when the button is pressed.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            1/5

            28 Oct 2021, 18:03

            • Login

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