Prevent Qt application from reacting to Tab and Alt keys
-
Thanks for the welcomes :)
Note that I'm using pyqt bindings.
And no, I did not mean Alt + Tab. I don't know how to describe the behavior I want other than to say I don't want special functionality for the Alt or Tab keys.
This is what I have tried so far, and it has not prevented the Qt application from performing special behavior (specifically testing by pressing Alt and looking to see if the menu bar stole focus or not):
class View(QtWidgets.QGraphicsView): def __init__(self, scene): super(View, self).__init__(scene) self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag) def event(self, event): if event.type() == QtCore.QEvent.KeyPress or event.type() == QtCore.QEvent.KeyRelease: if event.key() == QtCore.Qt.Key_Alt: return True return QtWidgets.QGraphicsView.event(self, event) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QGraphicsView.keyPressEvent(self, event) def keyReleaseEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QGraphicsView.keyReleaseEvent(self, event)
class Scene(QtWidgets.QGraphicsScene): def __init__(self, *args, **kwargs): super(Scene, self).__init__(*args, **kwargs) def event(self, event): if event.type() == QtCore.QEvent.KeyPress or event.type() == QtCore.QEvent.KeyRelease: if event.key() == QtCore.Qt.Key_Alt: return True return QtWidgets.QGraphicsScene.event(self, event) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QGraphicsScene.keyPressEvent(self, event) def keyReleaseEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QGraphicsScene.keyReleaseEvent(self, event)
class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() def event(self, event): if event.type() == QtCore.QEvent.KeyPress or event.type() == QtCore.QEvent.KeyRelease: if event.key() == QtCore.Qt.Key_Alt: return True return QtWidgets.QMainWindow.event(self, event) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QMainWindow.keyPressEvent(self, event) def keyReleaseEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QMainWindow.keyReleaseEvent(self, event)
class Application(QtWidgets.QApplication): def __init__(self, args): super().__init__(args) def event(self, event): if event.type() == QtCore.QEvent.KeyPress or event.type() == QtCore.QEvent.KeyRelease: if event.key() == QtCore.Qt.Key_Alt: return True return QtWidgets.QApplication.event(self, event) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QApplication.keyPressEvent(self, event) def keyReleaseEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: return QtWidgets.QApplication.keyReleaseEvent(self, event)
Overrides for keyPressEvent(), keyReleaseEvent(), and event() methods have no issues for any generic key like 'P' or 'Q' but Alt and Tab are being captured somewhere behind the scenes, and I'm trying to override that special behavior to prevent it from occurring at all.
-
Again: Alt is a modifier not a key per se. It won't generate a key press event.
You will be able to verify it's pressed when there's another "normal" key that is press e.g. "Alt+F" that will open the File menu if there was an accelerator associated with it. -
Again: Alt is a modifier not a key per se. It won't generate a key press event.
You will be able to verify it's pressed when there's another "normal" key that is press e.g. "Alt+F" that will open the File menu if there was an accelerator associated with it.I'm not sure what you mean when you say Alt will not generate a key press event. It very much does generate a key press event and a key release event, but they do not happen as one would expect, and I'm guessing it's due to how the focus is changed.
You can test this by adding an event hook for both key press and key release where they simply print 'Pressed' and 'Released' respectively, and then try pressing Alt. You will see that pressing Alt generates 'Pressed' but releasing does not generate 'Released' and pressing Alt again does not generate the 'Pressed' message again, but releasing it does generate the 'Released' message.
If you are asserting that the Qt framework makes the assumption that the user never wants to override the behavior of the Alt key, then what you're saying makes sense, but otherwise I don't see why you're asserting that Alt is only a modifier and not a key in and of itself.
-
hi
Its send as a modifier
void myClass::keyPressEvent(QKeyEvent *e)
{
if ((e->key()==Qt::Key_Return) && (e->modifiers()==Qt::AltModifier))
doSomething();
}So its not really listed as a key.
-
hi
Its send as a modifier
void myClass::keyPressEvent(QKeyEvent *e)
{
if ((e->key()==Qt::Key_Return) && (e->modifiers()==Qt::AltModifier))
doSomething();
}So its not really listed as a key.
Okay, so is there any way to override that behavior? If not, then it would indeed seem that the Qt framework assumes the user would never want to override the behavior of the Alt key, which is quite odd and unfortunate. In any low level application it's very easy to treat all keys on the keyboard as equal, but of course it's preferable to use Qt over writing custom Windows message pumps.
-
What platform are you running your code on ?
You made me doubt so I've double checked. On OS X at least, there's indeed key press and release events generated for the modifiers (AFAIK, something new)
-
Can you check with a basic C++ project if you have the same results ? Just to be sure PyQt is innocent.
#include <QApplication> #include <QWidget> #include <QtDebug> class MyWidget : public QWidget { public: explicit MyWidget(QWidget *parent = nullptr) : QWidget(parent) {} void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE { qDebug() << "Pressed" << (Qt::Key) event->key(); } void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE { qDebug() << "Released" << (Qt::Key) event->key(); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget mw; mw.show(); return app.exec(); }
-
hi
Test on win 10, Qt 5.5, empty mainwindowvoid keyReleaseEvent(QKeyEvent* e) {
qDebug() << "release" << (e->key() == Qt::Key_Alt);
if ( e->modifiers() == Qt::AltModifier)
qDebug() << "ALT up";
}void keyPressEvent(QKeyEvent* e) {
if ( e->modifiers() == Qt::AltModifier)
qDebug() << "ALT down" << (e->key() == Qt::Key_Alt);
}Give me "ALT up/down" with key Qt::Key_Alt.
So seem it does send it also as key. (on windows)UPDATE:
It gives me ALT as modifier on down , but only as key when released. ( make sense) -
Can you check with a basic C++ project if you have the same results ? Just to be sure PyQt is innocent.
#include <QApplication> #include <QWidget> #include <QtDebug> class MyWidget : public QWidget { public: explicit MyWidget(QWidget *parent = nullptr) : QWidget(parent) {} void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE { qDebug() << "Pressed" << (Qt::Key) event->key(); } void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE { qDebug() << "Released" << (Qt::Key) event->key(); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget mw; mw.show(); return app.exec(); }
@SGaist
The key events are also available on Linux (Debian, 4.x kernel with Qt 5.5).
I always thought that modifiers are sent only as such, not as regular keys, but I guess this had been changed at some point ...@pcsagan,
Seeing that @mrjj also receives presses/releases correctly, I'd guess there's something specific to the python bindings, but you should run the example code to make sure. -
Hey guys, I ran this test as requested and it works the way the C++ implementation works, and which also happens to be the functionality I desired.
from PyQt5 import QtCore, QtWidgets import sys class Widget(QtWidgets.QWidget): def __init__(self, parent=None): super(Widget, self).__init__(parent) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: print('Alt pressed!') def keyReleaseEvent(self, event): if event.key() == QtCore.Qt.Key_Alt: print('Alt released!') if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) widget = Widget() widget.show() sys.exit(app.exec_())
What's interesting is that this behavior changes when you create a menu in the menubar via:
self.fileMenu = self.menuBar().addMenu('&File') self.newConsoleAct = QtWidgets.QAction(QtGui.QIcon('assets/icons/console.png'), '&New Console', self, shortcut='Ctrl+N', statusTip='Create a new client console', triggered=self.newConsole) self.fileMenu.addAction(self.newConsoleAct)
The code above will capture the focus and set it to the menu bar, and I don't know how to prevent this from happening. What's even more interesting is that when you add a QtWidgets.QMdiArea the behavior changes to what I originally described where the 'in between' key press/release events are somehow consumed, and you need to press and release Alt twice to see both pressed and release events trigger, and it still sets focus to the menu bar.
I assume that if you guys were to create a test application that had a menu bar and MDI area you'd see the same change in behavior of the keyPressEvent() and keyReleaseEvent(). I'm hoping I eventually discover some kind of policy flag that disables this behavior, but in the mean time I think I'll just use non-modifier keys for my scene's custom controls. I appreciate the help from you guys regardless. Thanks!
-
&File
< there you have an accelerator thus the the Key_Alt + Key_F combo must be detected.
Same goes for newConsoleAct where you have both an accelerator and a shortcut which must also be detected. -
&File
< there you have an accelerator thus the the Key_Alt + Key_F combo must be detected.
Same goes for newConsoleAct where you have both an accelerator and a shortcut which must also be detected. -
If you want to disable that, why not avoid accelerators ?
-
The thing is I actually really like the default behavior of the Qt framework, and it's remarkable at providing easy access to convenient and common functionality straight out of the box. I don't want to avoid accelerators because I want to provide keyboard shortcuts for the various things users can do in my application.
I was hoping that in my QGraphicsScene I could have the Ctrl key toggle between ScrollHandDrag and RubberBandDrag drag modes while still retaining Ctrl+<Key> shortcut functionality in the application (I simply wasn't going to assign the Alt key to any accelerators). I also wanted to toggle displaying textual information above items in the scene while the user has Alt pressed down (like Diablo 2 when items are on the ground, you only see the item text while Alt is held in a pressed state).
So in my application with a menu bar, MDI central widget, and graphics scene MDI child window, I effectively want to be able to press Ctrl + drag the mouse to select many items, then hold Alt to verify they're the ones I wanted selected by reading the text above them, then press Ctrl+R to rotate all selected items by 90 degrees.
When I said "that's the functionality I want to disable" I was referring to the mechanism that automatically captures the Alt key and reacts to it. I want to be in complete control of how the application responds to the Alt key. To be fair, and as I stated earlier, I can just use 'Q' and 'A' and 'Z' to achieve the functionality I want, but it's less intuitive than using the modifier keys to change the behavior of the scene (i.e. change selection mode and display item text).
-
The thing is I actually really like the default behavior of the Qt framework, and it's remarkable at providing easy access to convenient and common functionality straight out of the box. I don't want to avoid accelerators because I want to provide keyboard shortcuts for the various things users can do in my application.
I was hoping that in my QGraphicsScene I could have the Ctrl key toggle between ScrollHandDrag and RubberBandDrag drag modes while still retaining Ctrl+<Key> shortcut functionality in the application (I simply wasn't going to assign the Alt key to any accelerators). I also wanted to toggle displaying textual information above items in the scene while the user has Alt pressed down (like Diablo 2 when items are on the ground, you only see the item text while Alt is held in a pressed state).
So in my application with a menu bar, MDI central widget, and graphics scene MDI child window, I effectively want to be able to press Ctrl + drag the mouse to select many items, then hold Alt to verify they're the ones I wanted selected by reading the text above them, then press Ctrl+R to rotate all selected items by 90 degrees.
When I said "that's the functionality I want to disable" I was referring to the mechanism that automatically captures the Alt key and reacts to it. I want to be in complete control of how the application responds to the Alt key. To be fair, and as I stated earlier, I can just use 'Q' and 'A' and 'Z' to achieve the functionality I want, but it's less intuitive than using the modifier keys to change the behavior of the scene (i.e. change selection mode and display item text).
@pcsagan
Maybe you could use void qt_set_sequence_auto_mnemonic(bool enable) to disable the menu bar from intercepting the alt key presses?PS. I forgot you were working with python. I don't know a way to call that function from pyton as it most probably is not available in the bindings ...