tckraomuqnt
tckraomuqnt

Reputation: 492

KeyPressEvent with Qwidget is not working?

If I press any key, nothing will happen. How to implement it? And I notice if I pressed some key, (at first time/Very Beggning/first character), it take some more time to display that pressed Character in textbox.How to resolve it?

from PyQt5 import QtWidgets,QtCore,QtGui

class MyKey(QtWidgets.QWidget):
    def __init__(self):
        super(). __init__()
        self.setWindowTitle(" My Key Board")
        self.ui()

    def ui(self):
        self.tb = QtWidgets.QLineEdit()
        self.vb = QtWidgets.QVBoxLayout()
        self.vb.addWidget(self.tb)
        self.setLayout(self.vb)

    def keyPressEvent(self, e):
        print("key pressed")
        print(e.key())
        if type(e) == QtGui.QKeyEvent:
            if e.key() == QtCore.Qt.Key_A:
                print("ypu pressed 'A'")


if __name__ == "__main__":
    import sys
    myapp = QtWidgets.QApplication(sys.argv)
    mywindow = MyKey()
    mywindow.show()
    sys.exit(myapp.exec_())

Upvotes: 1

Views: 1304

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

In Qt the KeyPressEvent is only sent to the widget that has the focus and if it consumes it then the event is not sent to the parent widgets otherwise the parent widget is also notified.

In this case, the QLineEdit is the widget that has the focus and consumes the events that have text, numbers and some special keys, so the parent widgets will not be notified in those cases. There are other keys that are not consumed by QLineEdit such as F2, Escape, etc that are received by the parent widget.

If you want to listen to a special key or combination of keys then you must use a QShortcut but because the OP wants to listen to all the events then a possible solution is to install an eventfilter to the QWindow associated with the window that if it receives the keyboard event when the window has the focus no matter which widget has the focus.

from PyQt5 import QtWidgets, QtCore, QtGui


class MyKey(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle(" My Key Board")
        self.ui()

    def ui(self):
        self.tb = QtWidgets.QLineEdit()

        vb = QtWidgets.QVBoxLayout(self)
        vb.addWidget(self.tb)

    def handle_key_press(self, key, text):
        if key == QtCore.Qt.Key_A:
            print("ypu pressed 'A'")


class KeyHelper(QtCore.QObject):
    key_pressed = QtCore.pyqtSignal(int, str)

    def __init__(self, window):
        super().__init__(window)
        self._window = window
        self.window.installEventFilter(self)

    @property
    def window(self):
        return self._window

    def eventFilter(self, obj, event):
        if obj is self._window and event.type() == QtCore.QEvent.KeyPress:
            self.key_pressed.emit(event.key(), event.text())
        return super().eventFilter(obj, event)


if __name__ == "__main__":
    import sys

    myapp = QtWidgets.QApplication(sys.argv)

    mywindow = MyKey()
    mywindow.show()

    helper = KeyHelper(mywindow.windowHandle())
    helper.key_pressed.connect(mywindow.handle_key_press)

    sys.exit(myapp.exec_())

Upvotes: 3

Related Questions