PyQt Connect to KeyPressEvent
Create a custom signal, and emit it from your reimplemented event handler:
class MyWidget(QtGui.QWidget):
keyPressed = QtCore.pyqtSignal(int)
def keyPressEvent(self, event):
super(MyWidget, self).keyPressEvent(event)
self.keyPressed.emit(event.key())
...
def on_key(key):
# test for a specific key
if key == QtCore.Qt.Key_Return:
print('return key pressed')
else:
print('key pressed: %i' % key)
self.widget.keyPressed.connect(on_key)
(NB: calling the base-class implementation is required in order to keep the existing handling of events).
The way I've done this in the past is (it is a work around), where this is in the sender and the receiver declares/connects to the signal.
def keyPressEvent(self, event):
if type(event) == QtGui.QKeyEvent:
if event.key() == QtCore.Qt.Key_Space:
self.emit(QtCore.SIGNAL('MYSIGNAL'))