Reputation: 9
I need some help/advice on converting a string to HID key codes that represent keys on a keyboard. These HID codes are bytes and there is a table of a list available here
My original idea was to search a table then use a loop to match characters in a string to a table but unfortunately it hasn't worked for me.
How could I do this in a simple python script? I have tried to search for other answers with no results. The codes will get sent to the /dev/hidg0 which gets processed as a keystroke.
Upvotes: 0
Views: 1181
Reputation: 1441
Proper keyboard input is hard. ))
You can get JSON with HID Usages from official source:
The HID Usage Tables 1.4 document also includes all Usage definitions as a JSON file as an attachment to the PDF. The PDF serves as the 'single' source of truth.
You can use some converter to extract json from PDF file.
Relevant part of HID Usage Tables JSON:
{
"Kind": "Defined",
"Id": 7,
"Name": "Keyboard/Keypad",
"UsageIds": [
...
{
"Id": 4,
"Name": "Keyboard A",
"Kinds": [
"Sel"
]
},
{
"Id": 5,
"Name": "Keyboard B",
"Kinds": [
"Sel"
]
},
{
"Id": 6,
"Name": "Keyboard C",
"Kinds": [
"Sel"
]
},
{
"Id": 7,
"Name": "Keyboard D",
"Kinds": [
"Sel"
]
},
{
"Id": 8,
"Name": "Keyboard E",
"Kinds": [
"Sel"
]
},
{
"Id": 9,
"Name": "Keyboard F",
"Kinds": [
"Sel"
]
},
{
"Id": 10,
"Name": "Keyboard G",
"Kinds": [
"Sel"
]
},
{
"Id": 11,
"Name": "Keyboard H",
"Kinds": [
"Sel"
]
},
{
"Id": 12,
"Name": "Keyboard I",
"Kinds": [
"Sel"
]
},
{
"Id": 13,
"Name": "Keyboard J",
"Kinds": [
"Sel"
]
},
{
"Id": 14,
"Name": "Keyboard K",
"Kinds": [
"Sel"
]
},
{
"Id": 15,
"Name": "Keyboard L",
"Kinds": [
"Sel"
]
},
{
"Id": 16,
"Name": "Keyboard M",
"Kinds": [
"Sel"
]
},
{
"Id": 17,
"Name": "Keyboard N",
"Kinds": [
"Sel"
]
},
{
"Id": 18,
"Name": "Keyboard O",
"Kinds": [
"Sel"
]
},
{
"Id": 19,
"Name": "Keyboard P",
"Kinds": [
"Sel"
]
},
{
"Id": 20,
"Name": "Keyboard Q",
"Kinds": [
"Sel"
]
},
{
"Id": 21,
"Name": "Keyboard R",
"Kinds": [
"Sel"
]
},
{
"Id": 22,
"Name": "Keyboard S",
"Kinds": [
"Sel"
]
},
{
"Id": 23,
"Name": "Keyboard T",
"Kinds": [
"Sel"
]
},
{
"Id": 24,
"Name": "Keyboard U",
"Kinds": [
"Sel"
]
},
{
"Id": 25,
"Name": "Keyboard V",
"Kinds": [
"Sel"
]
},
{
"Id": 26,
"Name": "Keyboard W",
"Kinds": [
"Sel"
]
},
{
"Id": 27,
"Name": "Keyboard X",
"Kinds": [
"Sel"
]
},
{
"Id": 28,
"Name": "Keyboard Y",
"Kinds": [
"Sel"
]
},
{
"Id": 29,
"Name": "Keyboard Z",
"Kinds": [
"Sel"
]
},
{
"Id": 30,
"Name": "Keyboard 1 and Bang",
"Kinds": [
"Sel"
]
},
{
"Id": 31,
"Name": "Keyboard 2 and At",
"Kinds": [
"Sel"
]
},
{
"Id": 32,
"Name": "Keyboard 3 and Hash",
"Kinds": [
"Sel"
]
},
{
"Id": 33,
"Name": "Keyboard 4 and Dollar",
"Kinds": [
"Sel"
]
},
{
"Id": 34,
"Name": "Keyboard 5 and Percent",
"Kinds": [
"Sel"
]
},
{
"Id": 35,
"Name": "Keyboard 6 and Caret",
"Kinds": [
"Sel"
]
},
{
"Id": 36,
"Name": "Keyboard 7 and Ampersand",
"Kinds": [
"Sel"
]
},
{
"Id": 37,
"Name": "Keyboard 8 and Star",
"Kinds": [
"Sel"
]
},
{
"Id": 38,
"Name": "Keyboard 9 and Left Bracket",
"Kinds": [
"Sel"
]
},
{
"Id": 39,
"Name": "Keyboard 0 and Right Bracket",
"Kinds": [
"Sel"
]
},
If you want only US-ASCII character <-> HID usage
in US QWERTY keyboard layout conversion then just map:
0004:0007..0004:0001D
-> a..z
0004:001E..0004:00027
-> 1..9,0
If you need to do more advanced character -> HID usage
conversion then youre need to look at keyboard layout mappings (every keyboard layout language may map different keys to a different characters - US QWERTY, French AZERTY, German QWERTZ, Russian ЙЦУКЕН etc etc). Access to these mappings is platform specific and tied to the keycodes
that are platform specific too (this is what we had before HID spec even existed).
For example keycode -> character
tables for Linux are located here and Linux keycodes are defined in a special header file.
Windows uses PS/2 scancodes as keycodes internally and official way to map scancode to character is ToUnicode[Ex] API.
Also, you can look at Unicode CLDR Project. Among other things it contains database of different keyboard layouts and maps keyboard buttons -> characters. In CLDR, the rows on the keyboard are named starting with "A" for the bottom row up to "E" for the top row. The row of keys in the function section are considered to be in row "K". These row names are consistent with those given in ISO9995-1. So here we have another set of keycodes. ))
Upvotes: 1
Reputation: 431
You can use dict
for effecient storing of codes table. Also consider str.translate()
method.
Upvotes: 0