0
votes

I have QtKeyEvent* event from keyPressEvent func.

I want to get keyboard key name from this event. How example: I press "ALT + SHIFT + 1" and I want to get two modifiers from event->modifiers and "1" from event->key() (or event->nativeVirtualKey()).

I don't have problem with ALT and SHIFT, but I can't to get key name (string) "1" from event.

I tried to use QKeySequence(key).toString() and it's work for "1", "2"... But when I tried to press "F1"..., it look "F1" -> "p", "F2" -> "q"...

How to get QString keyname = "1" if I press "SHIFT + 1" and keyname = "F1" if I press "F1"?

2
key.text() does the job?Thomas Ayoub
No, does not. key.text() shows "!" if SHIFT is activefhdnsYa

2 Answers

0
votes

From the Qt Doc:

QString QKeyEvent::text() const

Returns the Unicode text that this key generated. The text returned can be an empty string in cases where modifier keys, such as Shift, Control, Alt, and Meta, are being pressed or released. In such cases key() will contain a valid value.

You then need to handle some cases manually.

0
votes

QKeySequence(event->key()).toString() prints out F1-F12 just fine.

Shift+number are recognized by Qt as !, $, % and so on due to the keyboard layout settings in the operating system. On the different layout you will get different keys. I'm afraid Qt doesn't have an API to read the keys on a lower level.

Check out how WebOS is doing key mappings for different layouts.