Convert string representation of key code to Qt :: Key (or any int) and vice versa

I would like to convert a string representing a key on a keyboard into an enumeration of key code, for example Qt :: Key (or something else). Conversion Examples:

  • "Ctrl" to Qt::Key_Control
  • "Up" to Qt::Key_Up
  • "a" to Qt::Key_A
  • "5" to Qt::Key_5

As you can see, this includes not only alphanumeric keys, but also modifiers and special keys. I am not tied to listing the Qt key code, but it seems that Qt has this parsing function in the QKeySequence fromString static function (see this direct link ):

 QKeySequence fromString(const QString & str, SequenceFormat format); 

You could somehow change this transformation. Well, I have a data file created by GhostMouse . This is a journal of what I print. Here is an example of how I type " It " :

 {SPACE down} {Delay 0.08} {SPACE up} {Delay 2.25} {SHIFT down} {Delay 0.11} {i down} {Delay 0.02} {SHIFT up} {Delay 0.03} {i up} {Delay 0.05} {t down} {Delay 0.08} {t up} {Delay 0.05} {SPACE down} {Delay 0.12} {SPACE up} 

So, I need a way to convert the string "SPACE" and all the other lines representing the keys in this data file into a unique int .

+4
source share
3 answers

You were already on the right track by looking at QKeySequence , as it can be used to convert between string and key codes:

 QKeySequence seq = QKeySequence("SPACE"); qDebug() << seq.count(); // 1 // If the sequence contained more than one key, you // could loop over them. But there is only one here. uint keyCode = seq[0]; bool isSpace = keyCode==Qt::Key_Space; qDebug() << keyCode << isSpace; // 32 true QString keyStr = seq.toString().toUpper(); qDebug() << keyStr; // "SPACE" 

added by OP

The above does not support modifier keys such as Ctrl, Alt, Shift, etc. Unfortunately, QKeySequence does not recognize the Ctrl key itself as a key. Thus, in order to support modifier keys, you can add the key to the modifier in the sequence, and then subtract it from the code. The following is the full function:

 uint toKey(QString const & str) { QKeySequence seq(str); uint keyCode; // We should only working with a single key here if(seq.count() == 1) keyCode = seq[0]; else { // Should be here only if a modifier key (eg Ctrl, Alt) is pressed. assert(seq.count() == 0); // Add a non-modifier key "A" to the picture because QKeySequence // seems to need that to acknowledge the modifier. We know that A has // a keyCode of 65 (or 0x41 in hex) seq = QKeySequence(str + "+A"); assert(seq.count() == 1); assert(seq[0] > 65); keyCode = seq[0] - 65; } return keyCode; } 
+5
source

You can recover most key codes, for example, QKeySequence::fromString("SPACE")[0] returns 32. This does not work for Shift, Ctrl, etc., so you have to process some lines yourself.

+1
source

In one line try:

 qDebug() << QKeySequence(event->modifiers()+event->key()).toString() << '\n'; 

First, I call the QKeySequence constructor, and then convert it to a string using toString ().

Output (the last is the window key):

 "Alt+??" "Alt+A" "Alt+A" "Alt+A" "A" "S" "F1" "F2" "Home" "Ins" "Num+8" "Num+5" "Num+4" "Num+." "Num++" "Num+-" "Num+/" "Num+*" "??" 
+1
source

All Articles