2

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()
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Tigran84
  • 193
  • 3
  • 16

2 Answers2

3

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()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

it is possible when we use key() method which return int value of keys,then we just convert int to char using unichr() function

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

    def event(self, event):
        self.myText = ""
        event_key = ""
        if event.type() == QEvent.KeyPress:
            if event.text() != "" :
                event_key = str(unichr(event.key())).lower()
            if event.modifiers()==  Qt.ControlModifier  :
                print event.text()
                self.myText = "Ctrl+" + event_key
            self.emit(SIGNAL("press"))
            return True
        return QLineEdit.event(self, event)
Tigran84
  • 193
  • 3
  • 16