Loading... # 概述 在编写键盘固件时,理解如何注册设备并发送按键上报是至关重要的。常用的USB键盘通常采用WASD布局。下面将介绍如何实现HID键盘与对应中断报文的通信。 # 设备注册 **想要想系统注册设备,首先需要了解一下 USB 的 HID 类中最重要的报告描述符。** 报告描述符定义了 USB 设备功能的数据格式和使用方法。 USB中文网有专门的专题页对其进行讲解,推荐看看。 [USB官网HID报告描述符生成工具 - USB中文网 (usbzh.com)](https://www.usbzh.com/article/detail-19.html) 以下为我在瀚文75扩展中注册的虚拟键盘设备的报告描述符 ![image.png](https://blog.hyiy.top/usr/uploads/2024/03/3842657782.png) # 按键上报 HID报文长度为8字节,我的报文增加了一个Report ID,所以是9字节,首字节固定为报告ID,也就是2 ```cpp uint8_t keyBoardData[9] = {2, 0, 0, 0, 0, 0, 0, 0}; ``` 除掉报告首字节,第一个报表报文字节为附加按键状态 ![报文码](https://blog.hyiy.top/usr/uploads/2024/03/3886917196.jpg) 第一个字节可以使用计算器或者位移计算 例如: 我想同时按下`左CTRL`+`左GUI(WIN)`+`TAB`那么第一位如下 ![计算器算键码](https://blog.hyiy.top/usr/uploads/2024/03/1836880179.png) 加上键扫描码`0x2B`(扫描代码大全见附录) 最终发送 ```cpp uint8_t keyBoardData[9] = {2, 0x9, 0, 0x2B, 0, 0, 0, 0}; ``` # 键扫描码 | 按键名称 | HID码(扫描码)十进制[十六进制] | 虚拟键码VK 十进制[十六进制] | | ------------------- | ------------------------------- | ---------------------------- | | ESC | 41 [0X29] | 27 [0x1B] | | F1 | 58 [0X3a] | 112 [0x70] | | F2 | 59 [0X3b] | 113 [0x71] | | F3 | 60 [0X3c] | 114 [0x72] | | F4 | 61 [0X3d] | 115 [0x73] | | F5 | 62 [0X3e] | 116 [0x74] | | F6 | 63 [0X3f] | 117 [0x75] | | F7 | 64 [0X40] | 118 [0x76] | | F8 | 65 [0X41] | 119 [0x77] | | F9 | 66 [0X42] | 120 [0x78] | | F10 | 67 [0X43] | 121 [0x79] | | F11 | 68 [0X44] | 122 [0x7A] | | F12 | 69 [0X45] | 123 [0x7B] | | Esc | 41 [0X29] | 27 [0x1B] | | (回退)Back | 42 [0X2a] | 8 [0x08] | | Tab | 43 [0X2b] | 9 [0x09] | | (大小写)CapLck | 57 [0X39] | 20 [0x14] | | (回车)Enter | 40 [0X28] | 13 [0x0D] | | (空格)Space | 44 [0X2c] | 32 [0x20] | | Scroll | 71 [0X47] | 145 [0x91] | | (暂停)Pause | 72 [0X48] | 19 [0x13] | | (插入)Insert | 73 [0X49] | 45 [0x2D] | | (截屏)PrintScr | 70 [0X46] | 44 [0x2C] | | (删除)Delete | 76 [0X4c] | 46 [0x2E] | | (首页)Home | 74 [0X4a] | 36 [0x24] | | (结尾)End | 77 [0X4d] | 35 [0x23] | | (上一页)PageUp | 75 [0X4b] | 33 [0x21] | | (下一页)PageDn | 78 [0X4e] | 34 [0x22] | | (左)Left | 80 [0X50] | 37 [0x25] | | (上)Up | 82 [0X52] | 38 [0x26] | | (右)Right | 79 [0X4f] | 39 [0x27] | | (下)Down | 81 [0X51] | 40 [0x28] | | (小键盘0)Num0 | 98 [0X62] | 96 [0x60] | | Num1 | 89 [0X59] | 97 [0x61] | | Num2 | 90 [0X5a] | 98 [0x62] | | Num3 | 91 [0X5b] | 99 [0x63] | | Num4 | 92 [0X5c] | 100 [0x64] | | Num5 | 93 [0X5d] | 101 [0x65] | | Num6 | 94 [0X5e] | 102 [0x66] | | Num7 | 95 [0X5f] | 103 [0x67] | | Num8 | 96 [0X60] | 104 [0x68] | | Num9 | 97 [0X61] | 105 [0x69] | | (加号)NumAdd | 87 [0X57] | 107 [0x6B] | | (减号)NumSub | 86 [0X56] | 109 [0x6D] | | (乘号)NumMult | 85 [0X55] | 106 [0x6A] | | (除号)NumDiv | 84 [0X54] | 111 [0x6F] | | (点)NumDecim | 99 [0X63] | 110 [0x6E] | | (数字锁定键)NumLock | 83 [0X53] | 144 [0x90] | | Ctrl | 1 [0X01] | 17 [0x11] | | (左CTR)LCtrl | 1 [0X01] | 162 [0xA2] | | RCtrl | 16 [0X10] | 163 [0xA3] | | Shift | 2 [0X02] | 16 [0x10] | | LShift | 2 [0X02] | 160 [0xA0] | | RShift | 32 [0X20] | 161 [0xA1] | | Alt | 4 [0X04] | 18 [0x12] | | LAlt | 4 [0X04] | 164 [0xA4] | | RAlt | 64 [0X40] | 165 [0xA5] | | WIN | 8 [0X08] | 91 [0x5B] | | LWIN | 8 [0X08] | 91 [0x5B] | | RWIN | 128 [0X80] | 92 [0x5C] | | A | 4 [0X04] | 65 [0x41] | | B | 5 [0X05] | 66 [0x42] | | C | 6 [0X06] | 67 [0x43] | | D | 7 [0X07] | 68 [0x44] | | E | 8 [0X08] | 69 [0x45] | | F | 9 [0X09] | 70 [0x46] | | G | 10 [0X0a] | 71 [0x47] | | H | 11 [0X0b] | 72 [0x48] | | I | 12 [0X0c] | 73 [0x49] | | J | 13 [0X0d] | 74 [0x4A] | | K | 14 [0X0e] | 75 [0x4B] | | L | 15 [0X0f] | 76 [0x4C] | | M | 16 [0X10] | 77 [0x4D] | | N | 17 [0X11] | 78 [0x4E] | | O | 18 [0X12] | 79 [0x4F] | | P | 19 [0X13] | 80 [0x50] | | Q | 20 [0X14] | 81 [0x51] | | R | 21 [0X15] | 82 [0x52] | | S | 22 [0X16] | 83 [0x53] | | T | 23 [0X17] | 84 [0x54] | | U | 24 [0X18] | 85 [0x55] | | V | 25 [0X19] | 86 [0x56] | | W | 26 [0X1a] | 87 [0x57] | | X | 27 [0X1b] | 88 [0x58] | | Y | 28 [0X1c] | 89 [0x59] | | Z | 29 [0X1d] | 90 [0x5A] | | 0 | 39 [0X27] | 48 [0x30] | | 1 | 30 [0X1e] | 49 [0x31] | | 2 | 31 [0X1f] | 50 [0x32] | | 3 | 32 [0X20] | 51 [0x33] | | 4 | 33 [0X21] | 52 [0x34] | | 5 | 34 [0X22] | 53 [0x35] | | 6 | 35 [0X23] | 54 [0x36] | | 7 | 36 [0X24] | 55 [0x37] | | 8 | 37 [0X25] | 56 [0x38] | | 9 | 38 [0X26] | 57 [0x39] | 最后修改:2024 年 03 月 04 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏