How to get keyboard input PS/2 scan codes or USB usage codes in Linux?
-
Hello,
I'm trying to get the PS/2 scan codes or USB usage codes of keyboard keys pressed in a Qt application running on Linux. I tried overriding "QWidget::keyPressEvent()":http://qt-project.org/doc/qt-4.8/qwidget.html#keyPressEvent but "QKeyEvent::navtiveScanCode()":http://qt-project.org/doc/qt-4.8/qkeyevent.html#nativeScanCode returns a value which doesn't correspond to the key's equilvalent PS/2 Set 1, Set 2, or Set 3 scan code nor the USB usage code. Instead, I believe this function returns a X11 mapped keycode.
Example: the 'a' key returns 0x26 instead of 0x1E (PS/2 Set 1) or 0x1C (Set 2 & 3) or 0x04 (USB).
This occurs even if I try to intercept the key at the X11 event loop "QWidget::x11Event(":http://qt-project.org/doc/qt-4.8/qwidget.html#x11Event). In this case, "XKeyPressedEvent::keycode":http://www.x.org/releases/X11R7.6/doc/libX11/specs/libX11/libX11.html#Keyboard_and_Pointer_Events_b also equals to 0x26 for the 'a' key.
However, the terminal commands "showkey -k":http://linux.die.net/man/1/showkey and "showkey -s":http://linux.die.net/man/1/showkey both show the 'a' key to be equal to 0x1E as expected.
How do I get the PS/2 scan code or the USB usage code for key presses?
OS: Fedora 17 x86
X Server: 1.12.4
X Protocol: 11 rev0
Qt: 4.8.4Regards,
Daniel K. -
If your just looking to look for keypresses, then the keyboard event that you described in the beginning is definitely what you want.
The keypress override returns the Qt key "type", which basically is a bunch of enumerated keyboard presses. For example: Qt::Key_A is the A key.
Check the key pressed like this:
@
QWidget::keyPressEvent ( QKeyEvent * event )
{
switch(event->key()){
case Qt::Key_a:
break;
default:
break;
}
}
@ -
[quote author="dvez43" date="1358806327"]If your just looking to look for keypresses, then the keyboard event that you described in the beginning is definitely what you want.[/quote]
It's not keypresses I'm after, it's the scan code the keyboard emits. Either the scan code conforming to the PS/2 or USB specification.
[quote author="dvez43" date="1358806327"]The keypress override returns the Qt key “type”, which basically is a bunch of enumerated keyboard presses. For example: Qt::Key_A is the A key.[/quote]
I tried out QKeyEvent::key() but it is not what I'm looking for because those returned values don't conform to either the PS/2 or USB specifications. For example, the 'a' key returns 0x41.