Is there a way to send a key press to a widget without having to move the mouse over it?
-
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 :)
-
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 :)
-
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)
-
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)
@lachdanan Did you try giving focus to the widget?
-
-
Hi,
What kind of widget is that ?
What are you trying to do with it ? -
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 :)
@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.
-
@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.
@Pablo-J-Rogina the QTest module is meant to be used only for testing not in production code.
-
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.
-
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.
-
Before going further:
- What Python API ?
- Where does that widget you want to send event to come from ?
-
Before going further:
- What Python API ?
- Where does that widget you want to send event to come from ?
@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.
-
The action is inside the menu.
So you try send a keypress to a context menu (QAction) inside a TreeView ?
-
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.
-
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.
@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 -
@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@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.
-
@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.
@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. -
@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.
-
@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.
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
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. - I call dumpObjectTree method but it doesn't print anything, I guess it only works in IDE environments?