python-2.7eventspyqt4keyboard-shortcutsqlineedit

in pyqt how to print "Ctrl+key" in QLineEdit when pressed Ctrl + anyKey


here my kode where I generated event for QLineEdit to put pressed key combination in textline it Ok for Shift key and Alt key but not for Ctrl key why ?

#!/usr/bin/python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def main():
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())
class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        self.la = QLabel("Press tab in this box:")
        self.le = MyLineEdit()


        layout = QVBoxLayout()
        layout.addWidget(self.la)
        layout.addWidget(self.le)
        self.setLayout(layout)


        self.connect(self.le, SIGNAL("press"),
                 self.update)

    def update(self):
        oldText = str(self.le.text())
        self.le.setText(self.le.myText)

class MyLineEdit(QLineEdit):
    def __init__(self, *args):
        QLineEdit.__init__(self, *args)

        self.myText = ""
    def event(self, event):
        if event.type() == QEvent.KeyPress:
            if  event.modifiers() & Qt.ControlModifier  :

in textline it is Ok for Shift key and Alt key I can put by variable self.myText value in textline, but not for Ctrl key why ?

                print event.text()
                self.myText = "Ctrl+"+ event.text()
                self.emit(SIGNAL("press"))
            return True
        return QLineEdit.event(self, event)

if __name__ == "__main__":
    main()

Solution

  • The reason why the original example doesn't work is because many of the Ctrl+key combinations produce control characters - e.g. Ctrl+J produces a newline.

    The correct way to capture keyboard combinations is using QKeySequence, which will work for all keys, including function keys, arrow keys, page up/down keys, etc. You can also get translated shortcuts by using QKeySequence.PortableText.

    Here's a demo based on the original example:

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    def main():
        app = QApplication(sys.argv)
        w = MyWindow()
        w.show()
        sys.exit(app.exec_())
    
    class MyWindow(QWidget):
        def __init__(self, *args):
            QWidget.__init__(self, *args)
    
            self.la = QLabel("Press tab in this box:")
            self.le = MyLineEdit()
    
            layout = QVBoxLayout()
            layout.addWidget(self.la)
            layout.addWidget(self.le)
            self.setLayout(layout)
    
            self.le.keyPressed.connect(self.update)
    
        def update(self, text):
            self.le.setText(text)
    
    MOD_MASK = (Qt.CTRL | Qt.ALT | Qt.SHIFT | Qt.META)
    
    class MyLineEdit(QLineEdit):
        keyPressed = pyqtSignal(str)
    
        def keyPressEvent(self, event):
            keyname = ''
            key = event.key()
            modifiers = int(event.modifiers())
            if (modifiers and modifiers & MOD_MASK == modifiers and
                key > 0 and key != Qt.Key_Shift and key != Qt.Key_Alt and
                key != Qt.Key_Control and key != Qt.Key_Meta):
    
                keyname = QKeySequence(modifiers + key).toString()
    
                print('event.text(): %r' % event.text())
                print('event.key(): %d, %#x, %s' % (key, key, keyname))
    
            self.keyPressed.emit(keyname)
    
    if __name__ == "__main__":
        main()