Convert string representation of keycode to Qt::Key (or any int) and back

别来无恙 提交于 2020-01-23 05:45:07

问题


I would like to convert a string representing a key on the keyboard to a keycode enum like Qt::Key (or anything else). Example conversions would be:

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

As you see the above includes not just alpha numeric keys but modifiers and special keys. I'm not attached to the Qt keycode enum, but it seems that Qt has this parsing functionality in QKeySequence's fromString static function (see this direct link):

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

You might as why I need this conversion. Well, I have a data file generated by GhostMouse. It's a log of what I type. Here's an example of me typing " 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 strings representing keys in this data file to a unique int.


回答1:


You were already on the right track looking at QKeySequence, as this 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 acknowledge a Ctrl key by itself as a key. So, to support modifier keys, you can add a non-modifier key to the sequence and then subtract it from the code. The following is the complete 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 (e.g. 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;
}



回答2:


You can restore most of key codes, for example, QKeySequence::fromString("SPACE")[0] returns 32. It doesn't work for Shift, Ctrl, etc, so you should process some strings on your own.




回答3:


In one line, try it:

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

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

Output (the last one is the windows 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+*"

"??"


来源:https://stackoverflow.com/questions/14034209/convert-string-representation-of-keycode-to-qtkey-or-any-int-and-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!