整理来源:
// https://learn.microsoft.com/zh-cn/windows/win32/inputdev/about-keyboard-input
// https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input
HID用法名 | HID用法ID | 按键扫描码 | |
A | 0x0004 | 0x001E | |
B | 0x0005 | 0x0030 | |
C | 0x0006 | 0x002E | |
D | 0x0007 | 0x0020 | |
E | 0x0008 | 0x0012 | |
F | 0x0009 | 0x0021 | |
G | 0x000A | 0x0022 | |
H | 0x000B | 0x0023 | |
I | 0x000C | 0x0017 | |
J | 0x000D | 0x0024 | |
K | 0x000E | 0x0025 | |
L | 0x000F | 0x0026 | |
M | 0x0010 | 0x0032 | |
N | 0x0011 | 0x0031 | |
O | 0x0012 | 0x0018 | |
P | 0x0013 | 0x0019 | |
Q | 0x0014 | 0x0010 | |
R | 0x0015 | 0x0013 | |
S | 0x0016 | 0x001F | |
T | 0x0017 | 0x0014 | |
U | 0x0018 | 0x0016 | |
V | 0x0019 | 0x002F | |
W | 0x001A | 0x0011 | |
X | 0x001B | 0x002D | |
Y | 0x001C | 0x0015 | |
Z | 0x001D | 0x002C | |
1 & ! | 0x001E | 0x0002 | |
2 & @ | 0x001F | 0x0003 | |
3 & # | 0x0020 | 0x0004 | |
4 & $ | 0x0021 | 0x0005 | |
5 & % | 0x0022 | 0x0006 | |
6 & ^ | 0x0023 | 0x0007 | |
7 & & | 0x0024 | 0x0008 | |
8 & * | 0x0025 | 0x0009 | |
9 & ( | 0x0026 | 0x000A | |
0 & ) | 0x0027 | 0x000B | |
Enter | 0x0028 | 0x001C | |
Escape | 0x0029 | 0x0001 | |
Backspace | 0x002A | 0x000E | |
Tab | 0x002B | 0x000F | |
Space | 0x002C | 0x0039 | |
- & _ | 0x002D | 0x000C | |
= & + | 0x002E | 0x000D | |
[ & { | 0x002F | 0x001A | |
] & } | 0x0030 | 0x001B | |
\ & | | 0x0031 | 0x002B | |
; & : | 0x0033 | 0x0027 | |
' & " | 0x0034 | 0x0028 | |
` & ~ | 0x0035 | 0x0029 | |
, & < | 0x0036 | 0x0033 | |
. & > | 0x0037 | 0x0034 | |
? & / | 0x0038 | 0x0035 | |
Non-US # & ~ | 0x0032 | 0x002B | |
Caps Lock | 0x0039 | 0x003A | |
F1 | 0x003A | 0x003B | |
F2 | 0x003B | 0x003C | |
F3 | 0x003C | 0x003D | |
F4 | 0x003D | 0x003E | |
F5 | 0x003E | 0x003F | |
F6 | 0x003F | 0x0040 | |
F7 | 0x0040 | 0x0041 | |
F8 | 0x0041 | 0x0042 | |
F9 | 0x0042 | 0x0043 | |
F10 | 0x0043 | 0x0044 | |
F11 | 0x0044 | 0x0057 | |
F12 | 0x0045 | 0x0058 | |
Print Screen | 0x0046 | 0xE037 | |
Sya Req | 0x0046 | 0x0054 | |
Scroll Lock | 0x0047 | 0x0046 | |
Pause | 0x0048 | 0x0045 | |
Break | 0x0048 | 0xE046 | |
Insert | 0x0049 | 0xE052 | |
Home | 0x004A | 0xE047 | |
Page Up | 0x004B | 0xE049 | |
Delete | 0x004C | 0xE053 | |
End | 0x004D | 0xE04F | |
Page Down | 0x004E | 0xE051 | |
Right Arrow | 0x004F | 0xE04D | |
Left Arrow | 0x0050 | 0xE04B | |
Down Arrow | 0x0051 | 0xE050 | |
Up Arrow | 0x0052 | 0xE048 | |
Num Lock | 0x0053 | 0xE045 | |
Num / | 0x0054 | 0xE035 | |
Num * | 0x0055 | 0x0037 | |
Num - | 0x0056 | 0x004A | |
Num + | 0x0057 | 0x004E | |
Num Enter | 0x0058 | 0xE01C | |
Num 1 & End | 0x0059 | 0x004F | |
Num 2 & Down Arrow | 0x005A | 0x0050 | |
Num 3 & Page Dn | 0x005B | 0x0051 | |
Num 4 & Left Arrow | 0x005C | 0x004B | |
Num 5 | 0x005D | 0x004C | |
Num 6 & Right Arrow | 0x005E | 0x004D | |
Num 7 & Home | 0x005F | 0x0047 | |
Num 8 & Up Arrow | 0x0060 | 0x0048 | |
Num 9 & Page Up | 0x0061 | 0x0049 | |
Num 0 & Insert | 0x0062 | 0x0052 | |
Num . & Delete | 0x0063 | 0x0053 | |
Non-US / | 0x0064 | 0x0056 | |
Application | 0x0065 | 0xE05D | |
Power | 0x0066 | 0xE05E | |
Keypad = | 0x0067 | 0x0059 | |
F13 | 0x0068 | 0x0064 | |
F14 | 0x0069 | 0x0065 | |
F15 | 0x006A | 0x0066 | |
F16 | 0x006B | 0x0067 | |
F17 | 0x006C | 0x0068 | |
F18 | 0x006D | 0x0069 | |
F19 | 0x006E | 0x006A | |
F20 | 0x006F | 0x006B | |
F21 | 0x0070 | 0x006C | |
F22 | 0x0071 | 0x006D | |
F23 | 0x0072 | 0x006E | |
F24 | 0x0073 | 0x0076 | |
Left Ctrl | 0x00E0 | 0x001D | |
Left Shift | 0x00E1 | 0x002A | |
Left Alt | 0x00E2 | 0x0038 | |
Left Win | 0x00E3 | 0xE05B | |
Right Ctrl | 0x00E4 | 0xE01D | |
Right Shift | 0x00E5 | 0x0036 | |
Right Alt | 0x00E6 | 0xE038 | |
Right Win | 0x00E7 | 0xE05C |
C++中使用
#include <windows.h>
#include <string>
#include <stdint.h>
#include <map>
#include <tchar.h>#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endiftypedef struct _HID_SCAN_INFO
{_tstring strName; // 按键名uint8_t nUsageID; // HID 用法 IDuint16_t nScanCode; // 扫描码_HID_SCAN_INFO(_tstring _name, uint8_t _id, uint16_t _sc) :strName(_name), nUsageID(_id), nScanCode(_sc){}}HID_SCANCODE_INFO;#define AddHidKeyboardInfo(_name, _id, _sc) {_T(_name), HID_SCANCODE_INFO(_T(_name), _id, _sc) }// https://learn.microsoft.com/zh-cn/windows/win32/inputdev/about-keyboard-input
// https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input
std::map<_tstring, HID_SCANCODE_INFO> g_mapHidScanOrigin = //原始映射表(扫描码查询键名使用)
{//AddHidKeyboardInfo("ErrorRollOver", 0x0001, 0x00FF),AddHidKeyboardInfo("A", 0x0004, 0x001E),AddHidKeyboardInfo("B", 0x0005, 0x0030),AddHidKeyboardInfo("C", 0x0006, 0x002E),AddHidKeyboardInfo("D", 0x0007, 0x0020),AddHidKeyboardInfo("E", 0x0008, 0x0012),AddHidKeyboardInfo("F", 0x0009, 0x0021),AddHidKeyboardInfo("G", 0x000A, 0x0022),AddHidKeyboardInfo("H", 0x000B, 0x0023),AddHidKeyboardInfo("I", 0x000C, 0x0017),AddHidKeyboardInfo("J", 0x000D, 0x0024),AddHidKeyboardInfo("K", 0x000E, 0x0025),AddHidKeyboardInfo("L", 0x000F, 0x0026),AddHidKeyboardInfo("M", 0x0010, 0x0032),AddHidKeyboardInfo("N", 0x0011, 0x0031),AddHidKeyboardInfo("O", 0x0012, 0x0018),AddHidKeyboardInfo("P", 0x0013, 0x0019),AddHidKeyboardInfo("Q", 0x0014, 0x0010),AddHidKeyboardInfo("R", 0x0015, 0x0013),AddHidKeyboardInfo("S", 0x0016, 0x001F),AddHidKeyboardInfo("T", 0x0017, 0x0014),AddHidKeyboardInfo("U", 0x0018, 0x0016),AddHidKeyboardInfo("V", 0x0019, 0x002F),AddHidKeyboardInfo("W", 0x001A, 0x0011),AddHidKeyboardInfo("X", 0x001B, 0x002D),AddHidKeyboardInfo("Y", 0x001C, 0x0015),AddHidKeyboardInfo("Z", 0x001D, 0x002C),AddHidKeyboardInfo("1 & !", 0x001E, 0x0002),AddHidKeyboardInfo("2 & @", 0x001F, 0x0003),AddHidKeyboardInfo("3 & #", 0x0020, 0x0004),AddHidKeyboardInfo("4 & $", 0x0021, 0x0005),AddHidKeyboardInfo("5 & %", 0x0022, 0x0006),AddHidKeyboardInfo("6 & ^", 0x0023, 0x0007),AddHidKeyboardInfo("7 & &", 0x0024, 0x0008),AddHidKeyboardInfo("8 & *", 0x0025, 0x0009),AddHidKeyboardInfo("9 & (", 0x0026, 0x000A),AddHidKeyboardInfo("0 & )", 0x0027, 0x000B),AddHidKeyboardInfo("Enter", 0x0028, 0x001C),AddHidKeyboardInfo("Escape", 0x0029, 0x0001),AddHidKeyboardInfo("Backspace", 0x002A, 0x000E),AddHidKeyboardInfo("Tab", 0x002B, 0x000F),AddHidKeyboardInfo("Space", 0x002C, 0x0039),AddHidKeyboardInfo("- & _", 0x002D, 0x000C),AddHidKeyboardInfo("= & +", 0x002E, 0x000D),AddHidKeyboardInfo("[ & {", 0x002F, 0x001A),AddHidKeyboardInfo("] & }", 0x0030, 0x001B),AddHidKeyboardInfo("\\ & |", 0x0031, 0x002B),AddHidKeyboardInfo("; & :", 0x0033, 0x0027),AddHidKeyboardInfo("' & \"", 0x0034, 0x0028),AddHidKeyboardInfo("` & ~", 0x0035, 0x0029),AddHidKeyboardInfo(", & <", 0x0036, 0x0033),AddHidKeyboardInfo(". & >", 0x0037, 0x0034),AddHidKeyboardInfo("? & /", 0x0038, 0x0035),AddHidKeyboardInfo("Non-US # & ~", 0x0032, 0x002B),AddHidKeyboardInfo("Caps Lock", 0x0039, 0x003A),AddHidKeyboardInfo("F1", 0x003A, 0x003B),AddHidKeyboardInfo("F2", 0x003B, 0x003C),AddHidKeyboardInfo("F3", 0x003C, 0x003D),AddHidKeyboardInfo("F4", 0x003D, 0x003E),AddHidKeyboardInfo("F5", 0x003E, 0x003F),AddHidKeyboardInfo("F6", 0x003F, 0x0040),AddHidKeyboardInfo("F7", 0x0040, 0x0041),AddHidKeyboardInfo("F8", 0x0041, 0x0042),AddHidKeyboardInfo("F9", 0x0042, 0x0043),AddHidKeyboardInfo("F10", 0x0043, 0x0044),AddHidKeyboardInfo("F11", 0x0044, 0x0057),AddHidKeyboardInfo("F12", 0x0045, 0x0058),AddHidKeyboardInfo("Print Screen", 0x0046, 0xE037),AddHidKeyboardInfo("Sya Req", 0x0046, 0x0054),AddHidKeyboardInfo("Scroll Lock", 0x0047, 0x0046), AddHidKeyboardInfo("Pause", 0x0048, 0x0045),AddHidKeyboardInfo("Break", 0x0048, 0xE046),AddHidKeyboardInfo("Insert", 0x0049, 0xE052),AddHidKeyboardInfo("Home", 0x004A, 0xE047),AddHidKeyboardInfo("Page Up", 0x004B, 0xE049),AddHidKeyboardInfo("Delete", 0x004C, 0xE053),AddHidKeyboardInfo("End", 0x004D, 0xE04F),AddHidKeyboardInfo("Page Down", 0x004E, 0xE051),AddHidKeyboardInfo("Right Arrow", 0x004F, 0xE04D),AddHidKeyboardInfo("Left Arrow", 0x0050, 0xE04B),AddHidKeyboardInfo("Down Arrow", 0x0051, 0xE050),AddHidKeyboardInfo("Up Arrow", 0x0052, 0xE048),AddHidKeyboardInfo("Num Lock", 0x0053, 0xE045),AddHidKeyboardInfo("Num /", 0x0054, 0xE035),AddHidKeyboardInfo("Num *", 0x0055, 0x0037),AddHidKeyboardInfo("Num -", 0x0056, 0x004A),AddHidKeyboardInfo("Num +", 0x0057, 0x004E),AddHidKeyboardInfo("Num Enter", 0x0058, 0xE01C),AddHidKeyboardInfo("Num 1 & End", 0x0059, 0x004F),AddHidKeyboardInfo("Num 2 & Down Arrow", 0x005A, 0x0050),AddHidKeyboardInfo("Num 3 & Page Dn", 0x005B, 0x0051),AddHidKeyboardInfo("Num 4 & Left Arrow", 0x005C, 0x004B),AddHidKeyboardInfo("Num 5", 0x005D, 0x004C),AddHidKeyboardInfo("Num 6 & Right Arrow", 0x005E, 0x004D),AddHidKeyboardInfo("Num 7 & Home", 0x005F, 0x0047),AddHidKeyboardInfo("Num 8 & Up Arrow", 0x0060, 0x0048),AddHidKeyboardInfo("Num 9 & Page Up", 0x0061, 0x0049),AddHidKeyboardInfo("Num 0 & Insert", 0x0062, 0x0052),AddHidKeyboardInfo("Num . & Delete", 0x0063, 0x0053),AddHidKeyboardInfo("Non-US /", 0x0064, 0x0056),AddHidKeyboardInfo("Application", 0x0065, 0xE05D),AddHidKeyboardInfo("Power", 0x0066, 0xE05E),AddHidKeyboardInfo("Keypad =", 0x0067, 0x0059),AddHidKeyboardInfo("F13", 0x0068, 0x0064),AddHidKeyboardInfo("F14", 0x0069, 0x0065),AddHidKeyboardInfo("F15", 0x006A, 0x0066),AddHidKeyboardInfo("F16", 0x006B, 0x0067),AddHidKeyboardInfo("F17", 0x006C, 0x0068),AddHidKeyboardInfo("F18", 0x006D, 0x0069),AddHidKeyboardInfo("F19", 0x006E, 0x006A),AddHidKeyboardInfo("F20", 0x006F, 0x006B),AddHidKeyboardInfo("F21", 0x0070, 0x006C),AddHidKeyboardInfo("F22", 0x0071, 0x006D),AddHidKeyboardInfo("F23", 0x0072, 0x006E),AddHidKeyboardInfo("F24", 0x0073, 0x0076),AddHidKeyboardInfo("Left Ctrl", 0x00E0, 0x001D),AddHidKeyboardInfo("Left Shift", 0x00E1, 0x002A),AddHidKeyboardInfo("Left Alt", 0x00E2, 0x0038),AddHidKeyboardInfo("Left Win", 0x00E3, 0xE05B),AddHidKeyboardInfo("Right Ctrl", 0x00E4, 0xE01D),AddHidKeyboardInfo("Right Shift ", 0x00E5, 0x0036),AddHidKeyboardInfo("Right Alt", 0x00E6, 0xE038),AddHidKeyboardInfo("Right Win", 0x00E7, 0xE05C),
};