T
T
Timur Bogdalov2021-08-21 12:57:55
Python
Timur Bogdalov, 2021-08-21 12:57:55

String representation of shift, ctrl, alt etc keys in pyqt5?

QtGui.QKeySequence(Qt.Key_Shift).toString()

In keyPressEvent, I catch the pressed keys and display their string representation using QKeySequence(code).toString(). Errors occur when this key is pressed shift, control, alt, and so on. Is it possible to somehow represent these keys in text form using their codes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bazik29, 2021-08-21
@Bazik29

As an option, match the key code and the string yourself:

def key_code_str(event):
    key_str = {
        Qt.Key_Shift: "Shift",
        Qt.Key_Control: "Ctrl",
        Qt.Key_Alt: "Alt",
        Qt.Key_Meta: "Meta",
    }
    code = event.key()
    print(key_str.get(code, QKeySequence(code).toString()))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question