2

I'm analysing packets captured from a USB keyboard. I'm looking for a python library or dictionary that will convert the last 8 data bytes or even just the third byte which holds the Usage ID (of a single keypress) to an ASCII code (or to some other useful data structure which lets me quickly extract the character typed).

Here are a few example packets which represent 'g' and '{' keypresses (second one is '[' with Right Shift held down). (Note: only look at the last 8 bytes)

0000  1b 00 40 39 2d ac 89 b6 ff ff 00 00 00 00 09 00   ..@9-...........
0010  01 02 00 01 00 81 01 08 00 00 00 00 00 0a 00 00   ................
0020  00 00 00                                          ...

0000  1b 00 c0 e8 82 b4 89 b6 ff ff 00 00 00 00 09 00   ................
0010  01 02 00 01 00 81 01 08 00 00 00 00 00 00 00 00   ................
0020  00 00 00                                          ...

0000  1b 00 40 39 2d ac 89 b6 ff ff 00 00 00 00 09 00   ..@9-...........
0010  01 02 00 01 00 81 01 08 00 00 00 20 00 00 00 00   ........... ....
0020  00 00 00                                          ...

0000  1b 00 c0 e8 82 b4 89 b6 ff ff 00 00 00 00 09 00   ................
0010  01 02 00 01 00 81 01 08 00 00 00 20 00 2f 00 00   ........... ./..
0020  00 00 00                                          ...

For more information on the Usage ID, see page 53 of this document. This question is related, but I'm specifically looking for an easy way to do this in python without having to create my own dictionary. Note that evdev does not return the correct code.

Community
  • 1
  • 1
Aralox
  • 1,441
  • 1
  • 24
  • 44

1 Answers1

3

as your keyboard actually does only send the USB HID Usage ID you have to write a conversion table (afaik and also http://www.stackoverflow.com/questions/33610124/how-to-convert-from-a-hid-usage-id-to-a-virtual-key-code-in-windows-os : conversion is done by driver / kernel module).

in the usb_hid kernel module is a conversion table so if your keyboard can use this module you can skip the conversion ( https://github.com/torvalds/linux/blob/master/drivers/hid/usbhid/usbkbd.c )

it begins with 0, 0, 0, 0, 30, 48, 46,... compare to http://www.hiemalis.org/~keiji/PC/scancode-translate.pdf . this is a table that translates USB HID usage ID to normal PS/2 scancode (table is in hex)

in https://what.thedailywtf.com/topic/18926/how-to-easily-map-ps-2-scan-code-to-hid-usage-id/2 is linked a solution for a ready USB HID Usage ID-to-scancode-table that you may copy:

https://chromium.googlesource.com/chromium/src/+/3b0960abbbec96673b933a686677485aaf1a4e4/ui/events/keycodes/dom/keycode_converter_data.inc

in this table the USB row is USB Usage Page + USB Usage ID so 0x070004 is keycode 'KEY_A'

if you had scancode or keycode you could use evdev like in http://www.stackoverflow.com/questions/19732978/how-can-i-get-a-string-from-hid-device-in-python-with-evdev or http://www.stackoverflow.com/questions/5060710/format-of-dev-input-event

ralf htp
  • 9,149
  • 4
  • 22
  • 34