Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:59:26+00:00 2026-06-16T07:59:26+00:00

I would like to convert a string representing a key on the keyboard to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:59:27+00:00Added an answer on June 16, 2026 at 7:59 am

    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 must split the string representation using “+” sign and then process separately each substring. The following is the complete function:

    QVector<int> EmoKey::splitKeys(const QString &comb)
    {
        QVector<int> keys;
        const auto keyList = comb.split('+');
        for (const auto &key: keyList) {
            if (0 == key.compare("Alt", Qt::CaseInsensitive)) {
                keys << Qt::Key_Alt;
            } else if (0 == key.compare("Ctrl", Qt::CaseInsensitive)) {
                keys << Qt::Key_Control;
            } else if (0 == key.compare("Shift", Qt::CaseInsensitive)) {
                keys << Qt::Key_Shift;
            } else if (0 == key.compare("Meta", Qt::CaseInsensitive)) {
                keys << Qt::Key_Meta;
            } else {
                const QKeySequence keySeq(key);
                if (1 == keySeq.count()) {
                    keys << keySeq[0];
                }
            }
        }
        return keys;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to convert a hex string to a binary string. For example,
I would like to convert a string input representing numbers to the respective numeric
I would like to convert this string foo_utf = u'nästy chäräctörs with å and
I have double values which I would like to convert to String values with
I would like to convert the following string into an array/nested array and iterate
I would like to convert a Dictionary to a string array (or list) The
I would like to ask how could I convert a string which inserted by
I have a string that is UTC and would like to convert it to
I have a date that I would like to convert to String format. /Date(1281067200000)/
I would like to convert a string containing a valid Erlang expression to its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.