In PyQt I could define a keyPress and a keyRelease event, however, the keyPress event is triggered continously as long as the key is down, whereas I'd prefer an event that is triggered once when the key is first pressed down.
Is there such a built-in event?
If not, what is the easiest and correct (i.e. robust to pressing multiple keys at the same time etc.) way to create one?
One possible is to use the isAutoRepeat()
method of QKeyEvent
:
from PyQt5.QtWidgets import *
class Widget(QWidget):
def keyPressEvent(self, event):
if not event.isAutoRepeat():
print(event.text())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())