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. Is there a way to send a key press to a widget without having to move the mouse over it?
Forum Updated to NodeBB v4.3 + New Features

Is there a way to send a key press to a widget without having to move the mouse over it?

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 8 Posters 6.9k Views 4 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.
  • L lachdanan

    Hi,

    The app I am using has many qt widgets, but unfortunately they didn't expose all of the functionality in code, so I need to invoke the hotkey to perform specific actions, such as clear a tree view in a qt widget.

    The issue is, the whole app is designed to receive the key events based where the mouse is. So if the mouse is over widget 1, then only widget one will receive all the inputs.

    Is there a way to bypass this and send the key straight to the widget like some sort of lower level access?

    Otherwise even when I move the cursor before sending a key event, the inputs are sent to where my mouse cursor is currently.

    Thanks a lot in advance :)

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @lachdanan https://doc.qt.io/qt-5/qcoreapplication.html#sendEvent

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

    1 Reply Last reply
    3
    • L Offline
      L Offline
      lachdanan
      wrote on last edited by lachdanan
      #3

      Thanks I tried that before but no luck:

      perfMonWidget = QtWidgets.QApplication.widgetAt(355, 467)
      keypress = QtGui.QKeyEvent(QtGui.QKeyEvent.KeyPress, QtCore.Qt.Key_L, QtCore.Qt.NoModifier, "L")
      
      oldpos = QtGui.QCursor().pos()
      QtGui.QCursor().setPos(355, 467)
      for i in range(10):
          QtGui.QGuiApplication.sendEvent(perfMonWidget, keypress)
          QtWidgets.QApplication.sendEvent(perfMonWidget, keypress)
      O 1 Reply Last reply
      0
      • L lachdanan

        Thanks I tried that before but no luck:

        perfMonWidget = QtWidgets.QApplication.widgetAt(355, 467)
        keypress = QtGui.QKeyEvent(QtGui.QKeyEvent.KeyPress, QtCore.Qt.Key_L, QtCore.Qt.NoModifier, "L")
        
        oldpos = QtGui.QCursor().pos()
        QtGui.QCursor().setPos(355, 467)
        for i in range(10):
            QtGui.QGuiApplication.sendEvent(perfMonWidget, keypress)
            QtWidgets.QApplication.sendEvent(perfMonWidget, keypress)
        O Offline
        O Offline
        ofmrew
        wrote on last edited by
        #4

        @lachdanan Did you try giving focus to the widget?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lachdanan
          wrote on last edited by
          #5

          Yes I called focusWidget before on it but still the same. I also moved my mouse over the widget before executing the code, but I am not getting any key event sent, because it doesn't trigger the hotkey action. If I press myself, the widget reacts.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lachdanan
            wrote on last edited by
            #6

            Strangely I tested this with another widget in that app, that one receives the event when I send mouse event and keyboard event but the one I am interested somehow doesn't do anything.

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

              Hi,

              What kind of widget is that ?
              What are you trying to do with it ?

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

              L 1 Reply Last reply
              0
              • L lachdanan

                Hi,

                The app I am using has many qt widgets, but unfortunately they didn't expose all of the functionality in code, so I need to invoke the hotkey to perform specific actions, such as clear a tree view in a qt widget.

                The issue is, the whole app is designed to receive the key events based where the mouse is. So if the mouse is over widget 1, then only widget one will receive all the inputs.

                Is there a way to bypass this and send the key straight to the widget like some sort of lower level access?

                Otherwise even when I move the cursor before sending a key event, the inputs are sent to where my mouse cursor is currently.

                Thanks a lot in advance :)

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #8

                @lachdanan said in Is there a way to send a key press to a widget without having to move the mouse over it?:

                send the key straight to the widget

                What if you use QTest::keyClicks? From documentation:

                Simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test before each key click.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                SGaistS 1 Reply Last reply
                0
                • Pablo J. RoginaP Pablo J. Rogina

                  @lachdanan said in Is there a way to send a key press to a widget without having to move the mouse over it?:

                  send the key straight to the widget

                  What if you use QTest::keyClicks? From documentation:

                  Simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test before each key click.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Pablo-J-Rogina the QTest module is meant to be used only for testing not in production code.

                  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
                  • SGaistS SGaist

                    Hi,

                    What kind of widget is that ?
                    What are you trying to do with it ?

                    L Offline
                    L Offline
                    lachdanan
                    wrote on last edited by lachdanan
                    #10

                    @SGaist

                    It's the widget on screen left. I need to send the keypress to delete a profile, otherwise there is no delete function exposed for me to do it using the provided python API.

                    The hard coded behaviour is that if there are no profiles loaded into this widget, and you load one, it will show it. But if you load a profile when there are other profiles in the dropdown list, it won't load them, it will only add them to the list. But because I want to always show my most current profile data, I need to clear everything and load my profile.

                    That's why I need to invoke this delete profile key press multiple times to make sure the widget is empty.

                    alt text

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

                      Hi
                      It looks like a QToolButton
                      How do you get the pointer to it ?

                      sendEvent normally just works so i think there is something else up.

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

                        Before going further:

                        • What Python API ?
                        • Where does that widget you want to send event to come from ?

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

                        L 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Before going further:

                          • What Python API ?
                          • Where does that widget you want to send event to come from ?
                          L Offline
                          L Offline
                          lachdanan
                          wrote on last edited by
                          #13

                          @mrjj actually it's not a button. The entire tree view is the widget I need to send the key press. The action is inside the menu.

                          @SGaist said in Is there a way to send a key press to a widget without having to move the mouse over it?:

                          Before going further:

                          • What Python API ?
                          • Where does that widget you want to send event to come from ?

                          The python API of the app I posted the screen shot of. It exposes many functionality:

                          https://www.sidefx.com/docs/houdini/hom/index.html

                          The widget is part of the whole app.

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

                            The action is inside the menu.

                            So you try send a keypress to a context menu (QAction) inside a TreeView ?

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lachdanan
                              wrote on last edited by
                              #15

                              No I tried to send the keypress to the main widget, and if I manually press the same key over the widget anywhere, it will perform that action in the menu. So I don't have to click the menu itself or send to it. The whole panel can/should accept the key event as I can do it manually but in code nothing happens.

                              Also there is still the issue of having to have the mouse over the widget. The whole app's hotkeys work based on what panel has the focus.

                              mrjjM 1 Reply Last reply
                              0
                              • L lachdanan

                                No I tried to send the keypress to the main widget, and if I manually press the same key over the widget anywhere, it will perform that action in the menu. So I don't have to click the menu itself or send to it. The whole panel can/should accept the key event as I can do it manually but in code nothing happens.

                                Also there is still the issue of having to have the mouse over the widget. The whole app's hotkeys work based on what panel has the focus.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @lachdanan
                                Hi
                                Ok so its more like a QShortcut you try to trigger.
                                One thing comes to mind if you in Qt uses eventfilters on a View, you set it on its viewport as thats the widget you actually click on/interact with.

                                Could it be something like that ? I mean if you get the actual view widget it might not care as such for the key.

                                Do you have acces to most of Qt' api or just a selection ?
                                Like https://doc.qt.io/qt-5/qmetaobject.html

                                L 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @lachdanan
                                  Hi
                                  Ok so its more like a QShortcut you try to trigger.
                                  One thing comes to mind if you in Qt uses eventfilters on a View, you set it on its viewport as thats the widget you actually click on/interact with.

                                  Could it be something like that ? I mean if you get the actual view widget it might not care as such for the key.

                                  Do you have acces to most of Qt' api or just a selection ?
                                  Like https://doc.qt.io/qt-5/qmetaobject.html

                                  L Offline
                                  L Offline
                                  lachdanan
                                  wrote on last edited by
                                  #17

                                  @mrjj
                                  By viewport do you mean the panel on the screen left or screen center? Because there is an actual 3d viewport and that one has its own set of hotkeys. If you send it to that, it will do some other action for the same key.

                                  Basically I have access to all of qt and pyside2 AFAIK, except maybe things that are not exposed like key scan codes. Otherwise I can get/call anything on qwidget qapplication etc. It allows full qt programming.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • L lachdanan

                                    @mrjj
                                    By viewport do you mean the panel on the screen left or screen center? Because there is an actual 3d viewport and that one has its own set of hotkeys. If you send it to that, it will do some other action for the same key.

                                    Basically I have access to all of qt and pyside2 AFAIK, except maybe things that are not exposed like key scan codes. Otherwise I can get/call anything on qwidget qapplication etc. It allows full qt programming.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @lachdanan
                                    Hi
                                    Actually i mean the viewport object that all Qt view has. (from QScrollArea)
                                    (ui->treeView->viewport();)
                                    Not a 3d viewport :)

                                    Ok so it does work for other keys / Widgets, but that key and that widget will not?

                                    oh thats neat.
                                    You can try call dumpObjectTree() on the widget you think it is and see how its structured. Might give us
                                    hints on what is going on.

                                    1 Reply Last reply
                                    1
                                    • L Offline
                                      L Offline
                                      lachdanan
                                      wrote on last edited by
                                      #19

                                      @mrjj said in Is there a way to send a key press to a widget without having to move the mouse over it?:

                                      dumpObjectTree

                                      Thanks, I have to check that viewport method, I didn't see that before.

                                      Basically I tried another widget I normally work on and that accepted the key. But this widget, no key I sent, were accepted.

                                      I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?

                                      But I acquire the widget currently using widgetAt method and after that I recursively gathered all of its children which are these:

                                      <PySide2.QtWidgets.QWidget object at 0x000000006CDA9DC8>
                                      [<PySide2.QtWidgets.QWidget object at 0x000000006CDA9DC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B3FC508>, <PySide2.QtWidgets.QWidget object at 0x000000006CDA9E88>, <PySide2.QtWidgets.QWidget object at 0x000000009B5E6A88>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42B948>, <PySide2.QtWidgets.QWidget object at 0x000000009B42B7C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42B108>, <PySide2.QtWidgets.QWidget object at 0x000000009B42B1C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42BFC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B2A2D88>, <PySide2.QtWidgets.QWidget object at 0x000000009B316CC8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316D08>, <PySide2.QtWidgets.QWidget object at 0x000000009B40FDC8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316BC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B316C08>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316A08>, <PySide2.QtWidgets.QWidget object at 0x000000009B3169C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B3174C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B3170C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317848>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B317388>, <PySide2.QtWidgets.QWidget object at 0x000000009B3173C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317A48>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B3175C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317148>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B317D88>]

                                      Not sure if that helps. Unfortunately in this app, this is how you have to blindly find the widget you are looking for.

                                      mrjjM 1 Reply Last reply
                                      0
                                      • L lachdanan

                                        @mrjj said in Is there a way to send a key press to a widget without having to move the mouse over it?:

                                        dumpObjectTree

                                        Thanks, I have to check that viewport method, I didn't see that before.

                                        Basically I tried another widget I normally work on and that accepted the key. But this widget, no key I sent, were accepted.

                                        I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?

                                        But I acquire the widget currently using widgetAt method and after that I recursively gathered all of its children which are these:

                                        <PySide2.QtWidgets.QWidget object at 0x000000006CDA9DC8>
                                        [<PySide2.QtWidgets.QWidget object at 0x000000006CDA9DC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B3FC508>, <PySide2.QtWidgets.QWidget object at 0x000000006CDA9E88>, <PySide2.QtWidgets.QWidget object at 0x000000009B5E6A88>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42B948>, <PySide2.QtWidgets.QWidget object at 0x000000009B42B7C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42B108>, <PySide2.QtWidgets.QWidget object at 0x000000009B42B1C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B42BFC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B2A2D88>, <PySide2.QtWidgets.QWidget object at 0x000000009B316CC8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316D08>, <PySide2.QtWidgets.QWidget object at 0x000000009B40FDC8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316BC8>, <PySide2.QtWidgets.QWidget object at 0x000000009B316C08>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B316A08>, <PySide2.QtWidgets.QWidget object at 0x000000009B3169C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B3174C8>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B3170C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317848>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B317388>, <PySide2.QtWidgets.QWidget object at 0x000000009B3173C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317A48>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B3175C8>, <PySide2.QtWidgets.QWidget object at 0x000000009B317148>, <PySide2.QtWidgets.QVBoxLayout object at 0x000000009B317D88>]

                                        Not sure if that helps. Unfortunately in this app, this is how you have to blindly find the widget you are looking for.

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #20

                                        Hi

                                        But this widget, no key I sent, was accepted.

                                        Ok so that often would mean that its not the right widget we send to.
                                        It should work the same (event wise) regardless of the widget but
                                        if nothing happens it smell like it's not hooked up as we think or similar.

                                        • I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?
                                          Oh, i assumed it uses the qDebug method as it comes the same place but you could be right.

                                        Hmm,. it only shows QVBoxLayout names.
                                        Do/can you call widgetprt->metaObject()->className()
                                        so we maybe could see the it better ?

                                        Also just to be sure. We are talking about this one
                                        alt text

                                        One thing I might try if all else fails is to install an event filter (if that works in python)
                                        on the ListView (or what it is )
                                        and see what is sent to it when you press the key.

                                        L 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          Hi

                                          But this widget, no key I sent, was accepted.

                                          Ok so that often would mean that its not the right widget we send to.
                                          It should work the same (event wise) regardless of the widget but
                                          if nothing happens it smell like it's not hooked up as we think or similar.

                                          • I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?
                                            Oh, i assumed it uses the qDebug method as it comes the same place but you could be right.

                                          Hmm,. it only shows QVBoxLayout names.
                                          Do/can you call widgetprt->metaObject()->className()
                                          so we maybe could see the it better ?

                                          Also just to be sure. We are talking about this one
                                          alt text

                                          One thing I might try if all else fails is to install an event filter (if that works in python)
                                          on the ListView (or what it is )
                                          and see what is sent to it when you press the key.

                                          L Offline
                                          L Offline
                                          lachdanan
                                          wrote on last edited by lachdanan
                                          #21

                                          @mrjj Thanks, when I call metaObject().className() I get these:

                                          QWidget
                                          QWidget
                                          QWidget
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QWidget
                                          QVBoxLayout
                                          QWidget
                                          QVBoxLayout

                                          Yes that's the panel we are talking about.

                                          Ok I will look into installing an event filter. I am not sure if that's supported so will have to check.

                                          I also just tried sending the same key events to all the child widgets but same result.

                                          mrjjM 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