Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why keyPress Event in PyQt does not work for key Enter?

Why, when I press Enter, does the keyPressEvent method not do what I need? It just moves the cursor to a new line.

class TextArea(QTextEdit):
    def __init__(self, parent):
        super().__init__(parent=parent)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.show()

    def SLOT_SendMsg(self):
        return lambda: self.get_and_send()

    def get_and_send(self):
        text = self.toPlainText()
        self.clear()
        get_connect(text)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Enter: 
            self.get_and_send()
        else:
            super().keyPressEvent(event)
like image 528
Ilya Glushchenko Avatar asked Mar 07 '13 04:03

Ilya Glushchenko


1 Answers

Qt.Key_Enter is the Enter located on the keypad:

Qt::Key_Return  0x01000004   
Qt::Key_Enter   0x01000005  Typically located on the keypad.

Use:

def keyPressEvent(self, qKeyEvent):
    print(qKeyEvent.key())
    if qKeyEvent.key() == QtCore.Qt.Key_Return: 
        print('Enter pressed')
    else:
        super().keyPressEvent(qKeyEvent)
like image 160
warvariuc Avatar answered Nov 14 '22 10:11

warvariuc