以前用过的一个嵌入式设备使用USB键盘作为输入。当时还研究了一下USB协议,现在都快忘得差不多了。这里整理一下当时找到的关于USB键盘数据方面的资料。
主机与USB键盘建立连接后,定时从键盘获取8字节的输入报表。数据含义如下:
Byte | Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
0 | RightGUI | RightAlt | RightShift | RightControl | LeftGUI | LeftAlt | LeftShift | LeftControl |
1 | Reserved | |||||||
2 | Keyboard Page Usage | |||||||
3 | Keyboard Page Usage | |||||||
4 | Keyboard Page Usage | |||||||
5 | Keyboard Page Usage | |||||||
6 | Keyboard Page Usage | |||||||
7 | Keyboard Page Usage |
Byte1是保留字节,Byte0各位则表示左右GUI(win),alt,shift,control键是否按下,按下为1
剩下6个字节表示此时按下的其他所有键盘按键的键码,从低到高填充,没按就是0x00.如果同时按下的键超过允许,则显示0xff
键码含义可参照《HID Usage Tables》Version 1.11第53页中的Keyboard/Keypad Page。
我把常见的列写出来
#define KB_ERROR 0x01
#define KB_A 0x04
#define KB_B &nbs