I found the following in the Qt Docs ...
Quote:
QString QKeyEvent::text() const
Returns the Unicode text that this key generated.
Return values when modifier keys such as Shift, Control, Alt, and Meta are pressed differ among platforms and could return an empty string.
Note: key() will always return a valid value, independent of modifier keys.
|
and
Quote:
int QKeyEvent::key() const
Returns the code of the key that was pressed or released.
See Qt::Key for the list of keyboard codes. These codes are independent of the underlying window system. Note that this function does not distinguish between capital and non-capital letters, use the text() function (returning the Unicode text the key generated) for this purpose.
A value of either 0 or Qt::Key_unknown means that the event is not the result of a known key; for example, it may be the result of a compose sequence, a keyboard macro, or due to key event compression.
|
So this is pretty much a platform specific nightmare. And it appears to be based on other things as well.
On macOS here are some test cases:
- entered small h:
key() returns 'H', text() returns 'h', no modifiers
result: H
- enter Shift + H:
key() returns 'H', text() returns 'H', shift modifier
result: Shift+H
- entered Ctrl+Shift+1:
key() returns '1', text() returns '', ctrl and shift modifiers
result: Ctrl+Shift+1
- entered Meta+Shift+1:
key() returns '!', text returns '', shift and meta modifiers
result: Meta+Shift+! (this is wrong I think!)
- entered Alt+Shift+1:
key() returns '!', text() returns '?', alt and shift modifiers
result: Alt+!
So it appears on macOS the text() value returned is either the unicode character formed, or a null string, or a "?" depending on what other modifiers are being used at the time.
The bottom line is that the existing code seems to work on Linux as expected, seems to work on macOS - most of the time but text() is basically not as advertised. And on Windows it does not seem to work for at least some modifiers on at least some Windows platforms with some key combinations.
I would love to see the output of key() and text() and modifiers() and the result stored for all of the test cases shown above on Linux and Windows so that I can begin to understand what it is going on and what commonalities exist so we can rewrite this.
The official Qt docs are clearly worthless in this regard.
BTW: To make things even worse on macOS
- using the apple/command key results in the Ctrl modifier being set
- using the option key results in the Alt modifier being set
- using the control key results in the Meta modifier being set
Thanks,
KevinH