3
votes

I am trying to set up two-key shortcuts without modifiers in Pyside/PyQt4.

I have tried various combinations of QEvents and QKeySequences, but to no avail.

What I would like to do is something like this:

def keyPressEvent(self, event): if (event.key() == QtCore.Qt.Key_O) and (event.key() == QtCore.Qt.Key_P ): print('shortcut op accepted')

or

fileMenu.addAction(QtGui.QAction("Open Project", self, shortcut=QtGui.QKeySequence("P" and "O"),triggered=self.openProject))

where the users presses, O, then P and then the action occurs.

Does anyone know firstly if this is possible and if so how to do it?

1
The and operator for strings cannot be the right thing here because "P" and "O" is just "O".Trilarion

1 Answers

3
votes

For me "O, P" as arguments for the QKeySequence do the job.

Example:

from PySide import QtGui

def beep():
    print('beep')

app = QtGui.QApplication([])

toolbar = QtGui.QToolBar()
toolbar.show()

action = QtGui.QAction("Action", toolbar, shortcut=QtGui.QKeySequence("O, P"), triggered=beep)
toolbar.addAction(action)

app.exec_()