I have a vispy scene embedded in a pyqt frame on a pyqt app to display some plots. Now, i need a keypressevent on the whole app for some unrelated thing (a home-made implementation of vim command mode)
My app structure is like this:
app = QApplication(sys.argv)
win = QMainWindow()
vp
is a Vispy scene with a scatter plot.
canv_lay = QGridLayout()
canv_frame = QFrame()
canv_frame.setFrameStyle(1)
canv_frame.setLayout(canv_lay)
canv_lay.addWidget(vp.canvas.native)
sidebar_frame
is an unrelated frame:
mid_lay = QHBoxLayout()
mid_lay.addWidget(sidebar_frame)
mid_lay.addWidget(canv_frame)
main = QWidget()
main_lay = QGridLayout()
main_lay.addLayout(mid_lay,0,0,8,1)
main_lay.addLayout(cns_lay,10,0,1,1)
main.setLayout(main_lay)
win.setCentralWidget(main)
And the key listener, ex_command is the function to catch the key presses:
win.keyPressEvent = ex_command
Everything works perfect with the listener, but when i click the vispy plot, it stops working until i click another frame in the app and then it resumes catching the key presses. My obvious guess is that's because Vispy is catching the key events itself cause they are movement/zoom/etc keys in vispy api.
Now, my question, can i override, somehow, vispy catching some specific key so it gets fetched by my listener instead of Vispy? I've tried adding the listeners to the canvas_frame, to the canvas_layout, etc, but it's the same, vispy catches all the key presses after focusing it.
Per @musicamante's advice, i put an event filter on a subclass of `QMainWindow` and now the keypress event is being read anywhere.
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
app.installEventFilter(self) #keyboard control
def eventFilter(self, obj, ev):
if (ev.type() == QtCore.QEvent.Type.KeyPress):
<<Stuff>>
return super(MainWindow, self).eventFilter(obj, ev)