[PyQt] Capturing keypress events

Sergio Daniel Gomez sergiogomez at tostado.com.ar
Thu Mar 12 11:02:27 GMT 2009


Mal Wanstall escribió:
> I've got a simple little app that pre-loads a list of user defined 
> sounds and I want the sounds to be triggered on a particular key press 
> (also defined by the user). I'm fine with the QSound part of it but am 
> having trouble working out how to get the sounds to play when a 
> particular key was pressed. Any pointers are much appreciated.
> Thanks, -Mal
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
To capture key events you need overwrite the keyPressEvent.
I have a Ui made with Qt4 Designer and do that

class myWin(QtGui.QMainWindow):
     def __init__(self, parent=None):
         QtGui.QWidget.__init__(self, parent)
         self.ui = Ui_myMain()
         self.ui.setupUi(self)

     def keyPressEvent(self, event):
         if type(event) == QtGui.QKeyEvent:
             #here accept the event and do something
             print event.key()
             event.accept()
         else:
             event.ignore()


if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     mainW = myWin()
     mainW.show()
     sys.exit(app.exec_())

I'm not expert, perhaps there is a better way to do.

regards.

  Sergio D. Gómez
Tostado (SF - AR)


More information about the PyQt mailing list