keypressevent does not detect the enter key pyqt

2

A simple question I am trying to detect the enter key of my keyboard by pressing it but it does not work. this is the code I use:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import QtCore
from PyQt5 import Qt


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)


    def keyPressEvent(self,event):
        if event.key() == QtCore.Qt.Key_Enter:
            print("pressed")
        event.accept()



app = QApplication([])
p = Principal()
p.show()
app.exec_()
    
asked by Mystic_Force 21.03.2018 в 01:51
source

1 answer

1

QtCore.Qt.Key_Enter refers to the INTRO / ENTER of the numeric keypad As a general rule, you should use instead QtCore.Qt.Key_Return :

def keyPressEvent(self, event):
    if event.key() == QtCore.Qt.Key_Return:
        ...
    
answered by 21.03.2018 / 02:01
source