String to HID key codes

huangapple go评论130阅读模式
英文:

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作为“唯一”的真实来源。

https://www.usb.org/hid

您可以使用一些转换器从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. ))

String to HID key codes

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.

https://www.usb.org/hid

You can use some converter to extract json from PDF file.

Relevant part of HID Usage Tables JSON:

{
&quot;Kind&quot;: &quot;Defined&quot;,
&quot;Id&quot;: 7,
&quot;Name&quot;: &quot;Keyboard/Keypad&quot;,
&quot;UsageIds&quot;: [
...
{
&quot;Id&quot;: 4,
&quot;Name&quot;: &quot;Keyboard A&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 5,
&quot;Name&quot;: &quot;Keyboard B&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 6,
&quot;Name&quot;: &quot;Keyboard C&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 7,
&quot;Name&quot;: &quot;Keyboard D&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 8,
&quot;Name&quot;: &quot;Keyboard E&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 9,
&quot;Name&quot;: &quot;Keyboard F&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 10,
&quot;Name&quot;: &quot;Keyboard G&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 11,
&quot;Name&quot;: &quot;Keyboard H&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 12,
&quot;Name&quot;: &quot;Keyboard I&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 13,
&quot;Name&quot;: &quot;Keyboard J&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 14,
&quot;Name&quot;: &quot;Keyboard K&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 15,
&quot;Name&quot;: &quot;Keyboard L&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 16,
&quot;Name&quot;: &quot;Keyboard M&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 17,
&quot;Name&quot;: &quot;Keyboard N&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 18,
&quot;Name&quot;: &quot;Keyboard O&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 19,
&quot;Name&quot;: &quot;Keyboard P&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 20,
&quot;Name&quot;: &quot;Keyboard Q&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 21,
&quot;Name&quot;: &quot;Keyboard R&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 22,
&quot;Name&quot;: &quot;Keyboard S&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 23,
&quot;Name&quot;: &quot;Keyboard T&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 24,
&quot;Name&quot;: &quot;Keyboard U&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 25,
&quot;Name&quot;: &quot;Keyboard V&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 26,
&quot;Name&quot;: &quot;Keyboard W&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 27,
&quot;Name&quot;: &quot;Keyboard X&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 28,
&quot;Name&quot;: &quot;Keyboard Y&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 29,
&quot;Name&quot;: &quot;Keyboard Z&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 30,
&quot;Name&quot;: &quot;Keyboard 1 and Bang&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 31,
&quot;Name&quot;: &quot;Keyboard 2 and At&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 32,
&quot;Name&quot;: &quot;Keyboard 3 and Hash&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 33,
&quot;Name&quot;: &quot;Keyboard 4 and Dollar&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 34,
&quot;Name&quot;: &quot;Keyboard 5 and Percent&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 35,
&quot;Name&quot;: &quot;Keyboard 6 and Caret&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 36,
&quot;Name&quot;: &quot;Keyboard 7 and Ampersand&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 37,
&quot;Name&quot;: &quot;Keyboard 8 and Star&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 38,
&quot;Name&quot;: &quot;Keyboard 9 and Left Bracket&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},
{
&quot;Id&quot;: 39,
&quot;Name&quot;: &quot;Keyboard 0 and Right Bracket&quot;,
&quot;Kinds&quot;: [
&quot;Sel&quot;
]
},

If you want only US-ASCII character &lt;-&gt; 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 -&gt; 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 -&gt; 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.

huangapple
  • 本文由 发表于 2023年2月19日 15:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定