英文:
String to HID key codes
问题
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.
英文:
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.
答案1
得分: 1
适当的键盘输入很困难。))
您可以从官方来源获取带有HID用途的JSON:
HID使用表1.4文档还将所有用途定义作为JSON文件附加到PDF中。PDF作为“唯一”的真实来源。
您可以使用一些转换器从PDF文件中提取JSON。
HID使用表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"
]
}
如果您只需要在US QWERTY键盘布局中进行US-ASCII字符 <-> HID用途的转换,只需映射:
0004:0007..0004:0001D
->a..z
0004:001E..0004:00027
->1..9,0
如果您需要进行更高级的字符 -> HID用途
转换,那么您需要查看键盘布局映射(每种键盘布局语言可能将不同的键映射到不同的字符 - US QWERTY、French AZERTY、German QWERTZ、Russian ЙЦУКЕН等等)。访问这些映射是特定于平台的,并与特定于平台的keycodes
相关联(这是在HID规范甚至存在之前我们拥有的内容)。
例如,Linux的keycode -> character
表位于此处,Linux的keycodes 在特殊的头文件中定义。
Windows在内部使用PS/2扫描码作为keycodes,将扫描码映射到字符的官方方式是使用ToUnicode[Ex]
英文:
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. ))
答案2
得分: 0
你可以使用 dict
来高效存储代码表。还可以考虑使用 str.translate()
方法。
英文:
You can use dict
for effecient storing of codes table. Also consider str.translate()
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论