Reputation: 179
I am trying to capture all of key pressed in my program
def keyPressEvent(self, event):
event = event.key()
if (event == QtCore.Qt.Key_Q):
print ('Q!.')
That function works fine when i am trying to capture keys in my window. (in this case Q_Key)
But if the key was pressed in a Text Widget (for example in a: QListView, QlistWidget, QLineEdit, and so many more ) it don't work. The function prints nothing. I am doing something wrong... What can i do to fix it?
Upvotes: 3
Views: 2207
Reputation: 120798
You will need to install an event-filter on the application object to get all key-press events:
class Window(QMainWindow):
def __init__(self):
...
QApplication.instance().installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.KeyPress:
print('KeyPress: %s [%r]' % (event.key(), source))
return super().eventFilter(source, event)
Upvotes: 5