Scancodes Windows Linux
-
wrote on 27 May 2021, 15:15 last edited by Evgeny Siberia
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?
-
@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-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.
wrote on 27 May 2021, 16:56 last edited by@Christian-Ehrlicher I know it. But values in enum class Keys are QKeyEvent::nativeScanCode() const. It didn't work in Windows.
-
@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-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...
wrote on 27 May 2021, 18:18 last edited byvoid 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.
-
wrote on 27 May 2021, 18:20 last edited by 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
-
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
wrote on 27 May 2021, 18:46 last edited by@Evgeny-Siberia why enum?
const QMap<uint32,QString> keyNames = { {scancode1:"1"}, {scancode2:"2"}, ... }
-
@Evgeny-Siberia why enum?
const QMap<uint32,QString> keyNames = { {scancode1:"1"}, {scancode2:"2"}, ... }
wrote on 27 May 2021, 19:01 last edited by 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.
-
@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.
wrote on 27 May 2021, 19:03 last edited by 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 isQt::key_F1
, replacing it with key::whatever... But it's your code. -
@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 isQt::key_F1
, replacing it with key::whatever... But it's your code.wrote on 27 May 2021, 19:07 last edited by@artwaw thx, I will change it may be.
1/10