Get string value from modified key press
-
Hi,
I'm working on a key event related function. I use keyPressEvent to read in the keyboard inputs. The key() and string() values work fine on single press key events, but there is a problem.
When I press a key combination (control + c, for example), I receive two events. One has key = Qt::Key_Control, which is good. But the other has key = Qt::Key_C and string = QString(). Is there a way to convert the Qt::Key_* values to QString, QChar, or something similar?
A simple search in the docs yielded nothing, but I really wish I could find something here before diving into mapping all keys by myself (and there's the shift key modifier to consider...Aaaarrrrhh).
Thanks in advance.
-
The int key code values from Qt::Key_Space (0x20) until Qt::Key_AsciiTilde (0x7f) map directly to the respective ASCII codes of the characters. So it should be safe to do this:
@
int key = event->key();
QString keyString;
if(key >= Qt::Key_Space && key <= Qt::Key_AsciiTilde) {
// handle ASCII char like keys
keyString = QString( QChar(key) );
} else {
// handle the other keys here...
}
@ -
But text() is not available when modifiers present. :-( Well, I guess I'll just manually mapped all non-alphanumeric keys on my keyboard. But how about keyboard layouts? £ instead of $ above 4 on british keyboard, ¥ instead of \ on Japanese keyboards, etc. Is there a way for me to detect this on run-time?
-
I am building something similar to a terminal simulator that sends things like ctrl + ! to the server. The format the server takes (for the above example) is a CTRL, and then an ASCII '!', so I need to get the result of the keyboard output to send (through a TCP socket).
-
For alphabets the cases don't matter, so for ctrl + c both CTRL + 'C' and CTRL + 'c' are fine. I don't participate in the server side programming, so I'm not sure what they intend to do with non-ASCII keys. (Most likely they don't care -- the system we are building is not for public use, and we use mostly US-layout keyboards. My plan now is to ignore the problem and leave it for the testing team to worry about. :-p)