Search code examples
pythonqtpython-2.7pyqtqtextedit

Detecting Key Sequences


I am using pyqt's Qshortcut in trying to detect a key combination to trigger some action i.e when the user types -> into a QtextEdit widget, I would like to print "changing mode". I have seen other key sequence examples that involve CTRL+E or some other CTRL or shift key combination,

    self.shcut1 = QtGui.QShortcut(self)
    self.shcut1.setKey("CTRL+E")
    self.connect(self.shcut1, QtCore.SIGNAL("activated()"), self.close)

But I really want to trap ->(hyphen followed by a greater than sign). Any suggestions on how to do this


Solution

  • QShortCut only accepts combinations of QtCore.Qt::KeyboardModifiers. Try using an event filter:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtGui, QtCore
    
    class MyWindow(QtGui.QTextEdit):
        modeChanged = QtCore.pyqtSignal(bool)
        _seenMinus  = False
        def __init__(self, parent=None):
            super(MyWindow, self).__init__(parent)
    
            self.installEventFilter(self)
    
            self.modeChanged.connect(self.on_modeChanged)
    
        def on_modeChanged(self):
            print "Changing Mode."
    
        def eventFilter(self, obj, event):
            if event.type() == QtCore.QEvent.KeyPress:
                if event.key() == QtCore.Qt.Key_Minus:
                    self._seenMinus = True
    
                elif event.key() == QtCore.Qt.Key_Greater \
                and  event.modifiers() == QtCore.Qt.ShiftModifier \
                and  self._seenMinus:
                    self.modeChanged.emit(True)
                    self.setStyleSheet("""
                        background-color: lightgray;
                    """)
    
                elif event.modifiers() != QtCore.Qt.ShiftModifier:
                    if self._seenMinus == True:
                        self.modeChanged.emit(False)
                        self._seenMinus = False
                        self.setStyleSheet("")
    
    
            return super(MyWindow, self).eventFilter(obj, event)
    
    if __name__ == "__main__":
        import sys
    
        app = QtGui.QApplication(sys.argv)
        app.setApplicationName('MyWindow')
    
        main = MyWindow()
        main.show()
    
        sys.exit(app.exec_())