Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Scancodes Windows Linux
Qt 6.11 is out! See what's new in the release blog

Scancodes Windows Linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Evgeny SiberiaE Offline
    Evgeny SiberiaE Offline
    Evgeny Siberia
    wrote on last edited by Evgeny Siberia
    #1

    Hi. I need to receive the current button clicked. Conditions: Linux\Win + any keyboard layout.
    My code doesn't correct in Win:

    enum class Keys {
    
        F1      = 67,
        F2      = 68,
        F3      = 69,
        F4      = 70,
        F5      = 71,
        F6      = 72,
        F7      = 73,
        F8      = 74,
        F9      = 75,
        F10     = 76,
        F11     = 95,
        F12     = 96,
        CAPS    = 66,
        LSHIFT  = 50,
        RSHIFT  = 62,
        LCTRL   = 37,
        RCTRL   = 105,
        LALT    = 64,
        RALT    = 108,
        ENTER   = 36,
        BACKSPACE = 22,
        Q   = 24,
        W   = 25,
        E   = 26,
        R   = 27,
        T   = 28,
        Y   = 29,
        U   = 30,
        I   = 31,
        O   = 32,
        P   = 33,
        A   = 38,
        S   = 39,
        D   = 40,
        F   = 41,
        G   = 42,
        H   = 43,
        J   = 44,
        K   = 45,
        L   = 46,
        Z   = 52,
        X   = 53,
        C   = 54,
        V   = 55,
        B   = 56,
        N   = 57,
        M   = 58,
        LSqBracket      = 34,
        RSqBracket      = 35,
        VerticalLine    = 51,
        COLON           = 47,
        QuotationMark   = 48,
        LAngleBracket   = 59,
        RAngleBracket   = 60,
        Question        = 61,
        SlantLine       = 106,
        Asterick        = 63,
        Minus           = 82,
        Plus            = 86,
        NLEnter         = 104,
        NLDel           = 91,
        Zero        = 19,
        One         = 10,
        Two         = 11,
        Three       = 12,
        Four        = 13,
        Five        = 14,
        Six         = 15,
        Seven       = 16,
        Eight       = 17,
        Nine        = 18,
        NLZero      = 90,
        NLOne       = 87,
        NLTwo       = 88,
        NLThree     = 89,
        NLFour      = 83,
        NLFive      = 84,
        NLSix       = 85,
        NLSeven     = 79,
        NLEight     = 80,
        NLNine      = 81
    };
    

    Values in Windows and Linux are different. This code only for Linux. If use QKeyEvent::key() for a different keyboard layout (for example - Russian layout) , then any key() below will be ignored (except Key_Escape):

    void Form::keyPressEvent(QKeyEvent *e)
    {
        if(e->key() == Qt::Key_C){
            buttonClicked("C");
            return;
        }
    }
    

    How do solve this?

    if you see mistakes in English tell me about it

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Evgeny-Siberia said in Scancodes Windows Linux:

      How do solve this?

      By taking a look into the documentation you will find enough functions which gives you a scan code instead the key.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Evgeny SiberiaE 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Evgeny-Siberia said in Scancodes Windows Linux:

        How do solve this?

        By taking a look into the documentation you will find enough functions which gives you a scan code instead the key.

        Evgeny SiberiaE Offline
        Evgeny SiberiaE Offline
        Evgeny Siberia
        wrote on last edited by
        #3

        @Christian-Ehrlicher I know it. But values in enum class Keys are QKeyEvent::nativeScanCode() const. It didn't work in Windows.

        if you see mistakes in English tell me about it

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Evgeny-Siberia said in Scancodes Windows Linux:

          But values in enum class Keys are QKeyEvent::nativeScanCode()

          What does this mean? nativeScanCode() returns an uint32, not an enum...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          Evgeny SiberiaE 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @Evgeny-Siberia said in Scancodes Windows Linux:

            But values in enum class Keys are QKeyEvent::nativeScanCode()

            What does this mean? nativeScanCode() returns an uint32, not an enum...

            Evgeny SiberiaE Offline
            Evgeny SiberiaE Offline
            Evgeny Siberia
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            void Form::keyPressEvent(QKeyEvent *e) {
                qDebug() << QString("Key=%1 Scan=%2").arg(e->text()).arg(e->nativeScanCode());
            }
            

            and we'll receive these nums, which I wrote in enum.

            if you see mistakes in English tell me about it

            1 Reply Last reply
            0
            • Evgeny SiberiaE Offline
              Evgeny SiberiaE Offline
              Evgeny Siberia
              wrote on last edited by Evgeny Siberia
              #6

              I rewrote it for Win and Linux, but I'm sure there is a simpler solution.

              #ifndef NATIVESCANCODES_H
              #define NATIVESCANCODES_H
              
              enum class Keys {
              #if defined(Q_OS_LINUX)
                  F1      = 67,
                  F2      = 68,
                  F3      = 69,
                  F4      = 70,
                  F5      = 71,
                  F6      = 72,
                  F7      = 73,
                  F8      = 74,
                  F9      = 75,
                  F10     = 76,
                  F11     = 95,
                  F12     = 96,
                  CAPS    = 66,
                  LSHIFT  = 50,
                  RSHIFT  = 62,
                  LCTRL   = 37,
                  RCTRL   = 105,
                  LALT    = 64,
                  RALT    = 108,
                  ENTER   = 36,
                  BACKSPACE = 22,
                  Q   = 24,
                  W   = 25,
                  E   = 26,
                  R   = 27,
                  T   = 28,
                  Y   = 29,
                  U   = 30,
                  I   = 31,
                  O   = 32,
                  P   = 33,
                  A   = 38,
                  S   = 39,
                  D   = 40,
                  F   = 41,
                  G   = 42,
                  H   = 43,
                  J   = 44,
                  K   = 45,
                  L   = 46,
                  Z   = 52,
                  X   = 53,
                  C   = 54,
                  V   = 55,
                  B   = 56,
                  N   = 57,
                  M   = 58,
                  LSqBracket      = 34,
                  RSqBracket      = 35,
                  VerticalLine    = 51,
                  COLON           = 47,
                  QuotationMark   = 48,
                  LAngleBracket   = 59,
                  RAngleBracket   = 60,
                  Question        = 61,
                  SlantLine       = 106,
                  Asterick        = 63,
                  Minus           = 82,
                  Plus            = 86,
                  NLEnter         = 104,
                  NLDel           = 91,
                  Zero        = 19,
                  One         = 10,
                  Two         = 11,
                  Three       = 12,
                  Four        = 13,
                  Five        = 14,
                  Six         = 15,
                  Seven       = 16,
                  Eight       = 17,
                  Nine        = 18,
                  NLZero      = 90,
                  NLOne       = 87,
                  NLTwo       = 88,
                  NLThree     = 89,
                  NLFour      = 83,
                  NLFive      = 84,
                  NLSix       = 85,
                  NLSeven     = 79,
                  NLEight     = 80,
                  NLNine      = 81
              #elif defined(Q_OS_WIN)
                  F1      = 59,
                  F2      = 60,
                  F3      = 61,
                  F4      = 62,
                  F5      = 63,
                  F6      = 64,
                  F7      = 65,
                  F8      = 66,
                  F9      = 67,
                  F10     = 68,
                  F11     = 87,
                  F12     = 88,
                  CAPS    = 58,
                  LSHIFT  = 42,
                  RSHIFT  = 54,
                  LCTRL   = 29,
                  RCTRL   = 285,
                  LALT    = 56,
                  RALT    = 312,
                  ENTER   = 28,
                  BACKSPACE = 14,
                  Q   = 16,
                  W   = 17,
                  E   = 18,
                  R   = 19,
                  T   = 20,
                  Y   = 21,
                  U   = 22,
                  I   = 23,
                  O   = 24,
                  P   = 25,
                  A   = 30,
                  S   = 31,
                  D   = 32,
                  F   = 33,
                  G   = 34,
                  H   = 35,
                  J   = 36,
                  K   = 37,
                  L   = 38,
                  Z   = 44,
                  X   = 45,
                  C   = 46,
                  V   = 47,
                  B   = 48,
                  N   = 49,
                  M   = 50,
                  LSqBracket      = 26,
                  RSqBracket      = 27,
                  VerticalLine    = 43,
                  COLON           = 39,
                  QuotationMark   = 40,
                  LAngleBracket   = 51,
                  RAngleBracket   = 52,
                  Question        = 53,
                  SlantLine       = 309,
                  Asterick        = 55,
                  Minus           = 74,
                  Plus            = 78,
                  NLEnter         = 284,
                  NLDel           = 83,
                  Zero        = 11,
                  One         = 2,
                  Two         = 3,
                  Three       = 4,
                  Four        = 5,
                  Five        = 6,
                  Six         = 7,
                  Seven       = 8,
                  Eight       = 9,
                  Nine        = 10,
                  NLZero      = 82,
                  NLOne       = 79,
                  NLTwo       = 80,
                  NLThree     = 81,
                  NLFour      = 75,
                  NLFive      = 76,
                  NLSix       = 77,
                  NLSeven     = 71,
                  NLEight     = 72,
                  NLNine      = 73
              #endif
              
              };
              
              #endif // NATIVESCANCODES_H
              
              

              if you see mistakes in English tell me about it

              artwawA 1 Reply Last reply
              0
              • Evgeny SiberiaE Evgeny Siberia

                I rewrote it for Win and Linux, but I'm sure there is a simpler solution.

                #ifndef NATIVESCANCODES_H
                #define NATIVESCANCODES_H
                
                enum class Keys {
                #if defined(Q_OS_LINUX)
                    F1      = 67,
                    F2      = 68,
                    F3      = 69,
                    F4      = 70,
                    F5      = 71,
                    F6      = 72,
                    F7      = 73,
                    F8      = 74,
                    F9      = 75,
                    F10     = 76,
                    F11     = 95,
                    F12     = 96,
                    CAPS    = 66,
                    LSHIFT  = 50,
                    RSHIFT  = 62,
                    LCTRL   = 37,
                    RCTRL   = 105,
                    LALT    = 64,
                    RALT    = 108,
                    ENTER   = 36,
                    BACKSPACE = 22,
                    Q   = 24,
                    W   = 25,
                    E   = 26,
                    R   = 27,
                    T   = 28,
                    Y   = 29,
                    U   = 30,
                    I   = 31,
                    O   = 32,
                    P   = 33,
                    A   = 38,
                    S   = 39,
                    D   = 40,
                    F   = 41,
                    G   = 42,
                    H   = 43,
                    J   = 44,
                    K   = 45,
                    L   = 46,
                    Z   = 52,
                    X   = 53,
                    C   = 54,
                    V   = 55,
                    B   = 56,
                    N   = 57,
                    M   = 58,
                    LSqBracket      = 34,
                    RSqBracket      = 35,
                    VerticalLine    = 51,
                    COLON           = 47,
                    QuotationMark   = 48,
                    LAngleBracket   = 59,
                    RAngleBracket   = 60,
                    Question        = 61,
                    SlantLine       = 106,
                    Asterick        = 63,
                    Minus           = 82,
                    Plus            = 86,
                    NLEnter         = 104,
                    NLDel           = 91,
                    Zero        = 19,
                    One         = 10,
                    Two         = 11,
                    Three       = 12,
                    Four        = 13,
                    Five        = 14,
                    Six         = 15,
                    Seven       = 16,
                    Eight       = 17,
                    Nine        = 18,
                    NLZero      = 90,
                    NLOne       = 87,
                    NLTwo       = 88,
                    NLThree     = 89,
                    NLFour      = 83,
                    NLFive      = 84,
                    NLSix       = 85,
                    NLSeven     = 79,
                    NLEight     = 80,
                    NLNine      = 81
                #elif defined(Q_OS_WIN)
                    F1      = 59,
                    F2      = 60,
                    F3      = 61,
                    F4      = 62,
                    F5      = 63,
                    F6      = 64,
                    F7      = 65,
                    F8      = 66,
                    F9      = 67,
                    F10     = 68,
                    F11     = 87,
                    F12     = 88,
                    CAPS    = 58,
                    LSHIFT  = 42,
                    RSHIFT  = 54,
                    LCTRL   = 29,
                    RCTRL   = 285,
                    LALT    = 56,
                    RALT    = 312,
                    ENTER   = 28,
                    BACKSPACE = 14,
                    Q   = 16,
                    W   = 17,
                    E   = 18,
                    R   = 19,
                    T   = 20,
                    Y   = 21,
                    U   = 22,
                    I   = 23,
                    O   = 24,
                    P   = 25,
                    A   = 30,
                    S   = 31,
                    D   = 32,
                    F   = 33,
                    G   = 34,
                    H   = 35,
                    J   = 36,
                    K   = 37,
                    L   = 38,
                    Z   = 44,
                    X   = 45,
                    C   = 46,
                    V   = 47,
                    B   = 48,
                    N   = 49,
                    M   = 50,
                    LSqBracket      = 26,
                    RSqBracket      = 27,
                    VerticalLine    = 43,
                    COLON           = 39,
                    QuotationMark   = 40,
                    LAngleBracket   = 51,
                    RAngleBracket   = 52,
                    Question        = 53,
                    SlantLine       = 309,
                    Asterick        = 55,
                    Minus           = 74,
                    Plus            = 78,
                    NLEnter         = 284,
                    NLDel           = 83,
                    Zero        = 11,
                    One         = 2,
                    Two         = 3,
                    Three       = 4,
                    Four        = 5,
                    Five        = 6,
                    Six         = 7,
                    Seven       = 8,
                    Eight       = 9,
                    Nine        = 10,
                    NLZero      = 82,
                    NLOne       = 79,
                    NLTwo       = 80,
                    NLThree     = 81,
                    NLFour      = 75,
                    NLFive      = 76,
                    NLSix       = 77,
                    NLSeven     = 71,
                    NLEight     = 72,
                    NLNine      = 73
                #endif
                
                };
                
                #endif // NATIVESCANCODES_H
                
                
                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

                @Evgeny-Siberia why enum?

                const QMap<uint32,QString> keyNames = {
                {scancode1:"1"},
                {scancode2:"2"},
                ...
                }
                

                For more information please re-read.

                Kind Regards,
                Artur

                Evgeny SiberiaE 1 Reply Last reply
                0
                • artwawA artwaw

                  @Evgeny-Siberia why enum?

                  const QMap<uint32,QString> keyNames = {
                  {scancode1:"1"},
                  {scancode2:"2"},
                  ...
                  }
                  
                  Evgeny SiberiaE Offline
                  Evgeny SiberiaE Offline
                  Evgeny Siberia
                  wrote on last edited by Evgeny Siberia
                  #8

                  @artwaw
                  "why enum?" - I thought about this earlier but decided that enum is easy to read. I will use it in a project.

                  switch (e->nativeScanCode()) {
                      case static_cast<quint32>(Keys::F1):
                  ...
                  

                  Here we can see that it was F1 that was pressed. For more convenient use.

                  if you see mistakes in English tell me about it

                  artwawA 1 Reply Last reply
                  0
                  • Evgeny SiberiaE Evgeny Siberia

                    @artwaw
                    "why enum?" - I thought about this earlier but decided that enum is easy to read. I will use it in a project.

                    switch (e->nativeScanCode()) {
                        case static_cast<quint32>(Keys::F1):
                    ...
                    

                    Here we can see that it was F1 that was pressed. For more convenient use.

                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on last edited by artwaw
                    #9

                    @Evgeny-Siberia I fail to see the improvement of rewriting Qt::Keys - especially that those are already platform independent. You are, of course, most welcome to do as you wish.

                    EDIT:
                    I mean, there already is Qt::key_F1, replacing it with key::whatever... But it's your code.

                    For more information please re-read.

                    Kind Regards,
                    Artur

                    Evgeny SiberiaE 1 Reply Last reply
                    0
                    • artwawA artwaw

                      @Evgeny-Siberia I fail to see the improvement of rewriting Qt::Keys - especially that those are already platform independent. You are, of course, most welcome to do as you wish.

                      EDIT:
                      I mean, there already is Qt::key_F1, replacing it with key::whatever... But it's your code.

                      Evgeny SiberiaE Offline
                      Evgeny SiberiaE Offline
                      Evgeny Siberia
                      wrote on last edited by
                      #10

                      @artwaw thx, I will change it may be.

                      if you see mistakes in English tell me about it

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved