PyQt6 QWidget.keyPressEvent() behaviour changed?

Marko Luther marko.luther at gmx.net
Tue Nov 16 12:34:01 GMT 2021


Dear all,

with below example code I observe a difference between PyQt6 and PyQt5 in the result of the QKeyEvent delivered by the keyPressEvent method of QWidget.

While in PyQt5 the event.key() value did not change on pressing a modifier key together with another key, while in PyQt6 it does. Thus a simple comparison like event.key() == Qt.Key.Key_H does not work to recognise if the H key is pressed independent of modifier keys.

Is this intentional? How would one recognise the press of the H-key independent of any modifier?

Thanks for any insight,
Marko


--
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QKeyEvent

class MyWidget(QWidget):

    def keyPressEvent(self,event: QKeyEvent):
      k = event.key()
      txt = event.text()
      alt_modifier = (event.modifiers() == Qt.KeyboardModifier.AltModifier)
      print(k, txt, alt_modifier, k==Qt.Key.Key_H)

app = QApplication(sys.argv)
window = MyWidget()
window.show()
app.exec()


## PyQt6:

# press H key:
# => 72 h False True

# press and hold OPTION/ALT key
# => 16777251  True False

# press keep OPTION/ALT key pressed and press additionally H key:
# => 170 ª True False


## PyQt5

# press H key:
# => 72 h False True

# press and hold OPTION/ALT key
# => 16777251  True False

# press keep OPTION/ALT key pressed and press additionally H key:
# => 72 ª True True


More information about the PyQt mailing list