原帖地址:http://www.amobbs.com/forum.php?mod=viewthread&tid=4823160&page=1&authorid=249202
想用STM32做个无线键鼠遥控、找了好几天没有找到完整的USB鼠标键盘协议介绍。最后自己收集整理了一份,希望对需要的朋友有所帮助。
废话不多说,进入正题:
鼠标发送给PC的数据每次4个字节
BYTE1 BYTE2 BYTE3 BYTE4
定义分别是:
BYTE1 —
|–bit7: 1 表示 Y 坐标的变化量超出-256 ~ 255的范围,0表示没有溢出
|–bit6: 1 表示 X 坐标的变化量超出-256 ~ 255的范围,0表示没有溢出
|–bit5: Y 坐标变化的符号位,1表示负数,即鼠标向下移动
|–bit4: X 坐标变化的符号位,1表示负数,即鼠标向左移动
|–bit3: 恒为1
|–bit2: 1表示中键按下
|–bit1: 1表示右键按下
|–bit0: 1表示左键按下
BYTE2 — X坐标变化量,与byte的bit4组成9位符号数,负数表示向左移,正数表右移。用补码表示变化量
BYTE3 — Y坐标变化量,与byte的bit5组成9位符号数,负数表示向下移,正数表上移。用补码表示变化量
BYTE4 — 滚轮变化。
由于手上没有USB鼠标,对BYTE1的4-7位没有测试,对于BYTE2 BYTE3做个测试,BYTE1的4-7全为0的时候,BYTE2 BYTE3的正负表示鼠标移动方向
键盘发送给PC的数据每次8个字节
BYTE1 BYTE2 BYTE3 BYTE4 BYTE5 BYTE6 BYTE7 BYTE8
定义分别是:
BYTE1 —
|–bit0: Left Control是否按下,按下为1
|–bit1: Left Shift 是否按下,按下为1
|–bit2: Left Alt 是否按下,按下为1
|–bit3: Left GUI 是否按下,按下为1
|–bit4: Right Control是否按下,按下为1
|–bit5: Right Shift 是否按下,按下为1
|–bit6: Right Alt 是否按下,按下为1
|–bit7: Right GUI 是否按下,按下为1
BYTE2 — 暂不清楚,有的地方说是保留位
BYTE3–BYTE8 — 这六个为普通按键
键盘经过测试。
例如:键盘发送一帧数据 02 00 0x04 0x05 00 00 00 00
表示同时按下了Left Shift + ‘a’+’b’三个键
对于键码和PS2的键盘有所不同,具体请看附件。
USB HID to PS2 Scan Code 对照表.pdfourdev_651088NZ5EKW.pdf(文件大小:133K) (原文件名:USB HID to PS2 Scan Code Translation Table .pdf)
2011.6.23补充说明
补充对于键盘BYTE2的说明:
参照百合网(http://www.baiheee.com/OpenSource/Easy%20USB%2051%20Programer/Easy%20USB%2051%20Programer12.htm)键盘的报告描述符,如下
/*******************************************************************************************/
//这里只列出了第二个字节的说明
//这样的数据段个数为1
0x95, 0x01, // REPORT_COUNT (1)
//每个段长度为8bits
0x75, 0x08, // REPORT_SIZE (8)
//输入用,常量,值,绝对值
0x81, 0x03, // INPUT (Cnst,Var,Abs)
//上面这8个bit是常量,设备必须返回0
/*******************************************************************************************/
这里只是指明了这是一个以字节为单位的输入数据,并没有指明它的取值范围。
在HID1.11协议的Appendix B–>B.1 Protocol 1 (Keyboard) 的报告描述符中也有这样的说明(HID1.11协议的59页)
/*******************************************************************************************/
Report Count (1),
Report Size (8),
Input (Constant), ;Reserved byte
Report Count (5),
Report Size (1),
/*******************************************************************************************/
在注释中说明,这个字节是保留字节。
这里我也把HID1.11传上来给大家研究。
ourdev_651409JXHOAD.pdf(文件大小:661K) (原文件名:HID1.11协议.pdf)
ourdev_651410WJINWH.pdf(文件大小:745K) (原文件名:HID用途表1.12.pdf)
usb-hid 键值表
0 00 Reserved (no event indicated)9 N/A |
58 3A Keyboard F1 112 |
116 74 Keyboard Execute √ |
184 B8 Keypad { |
1 01 Keyboard ErrorRollOver9 N/A |
59 3B Keyboard F2 113 |
117 75 Keyboard Help √ |
185 B9 Keypad } |
2 02 Keyboard POSTFail9 N/A |
60 3C Keyboard F3 114 |
118 76 Keyboard Menu √ |
186 BA Keypad Tab |
3 03 Keyboard ErrorUndefined9 N/A |
61 3D Keyboard F4 115 |
119 77 Keyboard Select √ |
187 BB Keypad Backspace |
4 04 Keyboard a and A4 31 |
62 3E Keyboard F5 116 |
120 78 Keyboard Stop √ |
188 BC Keypad A |
5 05 Keyboard b and B 50 |
63 3F Keyboard F6 117 |
121 79 Keyboard Again √ |
189 BD Keypad B |
6 06 Keyboard c and C4 48 |
64 40 Keyboard F7 118 |
122 7A Keyboard Undo √ |
190 BE Keypad C |
7 07 Keyboard d and D 33 |
65 41 Keyboard F8 119 |
123 7B Keyboard Cut √ |
191 BF Keypad D |
8 08 Keyboard e and E 19 |
66 42 Keyboard F9 120 |
124 7C Keyboard Copy √ |
192 C0 Keypad E |
9 09 Keyboard f and F 34 |
67 43 Keyboard F10 121 |
125 7D Keyboard Paste √ |
193 C1 Keypad F |
10 0A Keyboard g and G 35 |
68 44 Keyboard F11 122 |
126 7E Keyboard Find √ |
194 C2 Keypad XOR |
11 0B Keyboard h and H 36 |
69 45 Keyboard F12 123 |
127 7F Keyboard Mute √ |
195 C3 Keypad ^ |
12 0C Keyboard i and I 24 |
70 46 Keyboard PrintScreen1 124 |
128 80 Keyboard Volume Up √ |
196 C4 Keypad % |
13 0D Keyboard j and J 37 |
71 47 Keyboard Scroll Lock11 125 |
129 81 Keyboard Volume Down √ |
197 C5 Keypad < |
14 0E Keyboard k and K 38 |
72 48 Keyboard Pause1 126 |
130 82 Keyboard Locking Caps Lock12 √ |
198 C6 Keypad > |
15 0F Keyboard l and L 39 |
73 49 Keyboard Insert1 75 |
131 83 Keyboard Locking Num Lock12 √ |
199 C7 Keypad & |
16 10 Keyboard m and M4 52 |
74 4A Keyboard Home1 80 |
132 84 Keyboard Locking Scroll Lock12 √ |
200 C8 Keypad && |
17 11 Keyboard n and N 51 |
75 4B Keyboard PageUp1 85 |
133 85 Keypad Comma27 107 |
201 C9 Keypad | |
18 12 Keyboard o and O4 25 |
76 4C Keyboard Delete Forward1;14 76 |
134 86 Keypad Equal Sign29 |
202 CA Keypad || |
19 13 Keyboard p and P4 26 |
77 4D Keyboard End1 81 |
135 87 Keyboard International115,28 56 |
203 CB Keypad : |
20 14 Keyboard q and Q4 17 |
78 4E Keyboard PageDown1 86 |
136 88 Keyboard International216 |
204 CC Keypad # |
21 15 Keyboard r and R 20 |
79 4F Keyboard RightArrow1 89 |
137 89 Keyboard International317 |
205 CD Keypad Space |
22 16 Keyboard s and S4 32 |
80 50 Keyboard LeftArrow1 79 |
138 8A Keyboard International418 |
206 CE Keypad @ |
23 17 Keyboard t and T 21 |
81 51 Keyboard DownArrow1 84 |
139 8B Keyboard International519 |
207 CF Keypad ! |
24 18 Keyboard u and U 23 |
82 52 Keyboard UpArrow1 83 |
140 8C Keyboard International620 |
208 D0 Keypad Memory Store |
25 19 Keyboard v and V 49 |
83 53 Keypad Num Lock and Clear11 90 |
141 8D Keyboard International721 |
209 D1 Keypad Memory Recall |
26 1A Keyboard w and W4 18 |
84 54 Keypad /1 95 |
142 8E Keyboard International822 |
210 D2 Keypad Memory Clear |
27 1B Keyboard x and X4 47 |
85 55 Keypad * 100 |
143 8F Keyboard International922 |
211 D3 Keypad Memory Add |
28 1C Keyboard y and Y4 22 |
86 56 Keypad – 105 |
144 90 Keyboard LANG125 |
212 D4 Keypad Memory Subtract |
29 1D Keyboard z and Z4 46 |
87 57 Keypad + 106 |
145 91 Keyboard LANG226 |
213 D5 Keypad Memory Multiply |
30 1E Keyboard 1 and !4 2 |
88 58 Keypad ENTER5 108 |
146 92 Keyboard LANG330 |
214 D6 Keypad Memory Divide |
31 1F Keyboard 2 and @4 3 |
89 59 Keypad 1 and End 93 |
147 93 Keyboard LANG431 |
215 D7 Keypad +/- |
32 20 Keyboard 3 and #4 4 |
90 5A Keypad 2 and Down Arrow 98 |
148 94 Keyboard LANG532 |
216 D8 Keypad Clear |
33 21 Keyboard 4 and $4 5 |
91 5B Keypad 3 and PageDn 103 |
149 95 Keyboard LANG68 |
217 D9 Keypad Clear Entry |
34 22 Keyboard 5 and %4 6 |
92 5C Keypad 4 and Left Arrow 92 |
150 96 Keyboard LANG78 |
218 DA Keypad Binary |
35 23 Keyboard 6 and ^4 7 |
93 5D Keypad 5 97 |
151 97 Keyboard LANG88 |
219 DB Keypad Octal |
36 24 Keyboard 7 and &4 8 |
94 5E Keypad 6 and Right Arrow 102 |
152 98 Keyboard LANG98 |
220 DC Keypad Decimal |
37 25 Keyboard 8 and *4 9 |
95 5F Keypad 7 and Home 91 |
153 99 Keyboard Alternate Erase7 |
221 DD Keypad Hexadecimal |
38 26 Keyboard 9 and (4 10 |
96 60 Keypad 8 and Up Arrow 96 |
154 9A Keyboard SysReq/Attention1 |
222-223 DE-DF Reserved |
39 27 Keyboard 0 and )4 11 |
97 61 Keypad 9 and PageUp 101 |
155 9B Keyboard Cancel |
224 E0 Keyboard LeftControl 58 |
40 28 Keyboard Return (ENTER)5 43 |
98 62 Keypad 0 and Insert 99 |
156 9C Keyboard Clear |
225 E1 Keyboard LeftShift 44 |
41 29 Keyboard ESCAPE 110 |
99 63 Keypad . and Delete 104 |
157 9D Keyboard Prior |
226 E2 Keyboard LeftAlt 60 |
42 2A Keyboard DELETE (Backspace)13 15 |
100 64 Keyboard Non-US \ and |3;6 45 |
158 9E Keyboard Return |
227 E3 Keyboard Left GUI10;23 127 104 |
43 2B Keyboard Tab 16 |
101 65 Keyboard Application10 129 |
159 9F Keyboard Separator |
228 E4 Keyboard RightControl 64 |
44 2C Keyboard Spacebar 61 |
102 66 Keyboard Power9 |
160 A0 Keyboard Out |
229 E5 Keyboard RightShift 57 |
45 2D Keyboard – and (underscore)4 12 |
103 67 Keypad = √ |
161 A1 Keyboard Oper |
230 E6 Keyboard RightAlt 62 |
46 2E Keyboard = and +4 13 |
104 68 Keyboard F13 |
162 A2 Keyboard Clear/Again |
231 E7 Keyboard Right GUI10;24 128 104 |
47 2F Keyboard [ and {4 27 |
105 69 Keyboard F14 |
163 A3 Keyboard CrSel/Props |
|
48 30 Keyboard ] and }4 28 |
106 6A Keyboard F15 |
164 A4 Keyboard ExSel |
|
49 31 Keyboard \ and | 29 |
107 6B Keyboard F16 |
165-175 A5-CF Reserved |
|
50 32 Keyboard Non-US # and ~2 42 |
108 6C Keyboard F17 |
176 B0 Keypad 00 |
|
51 33 Keyboard ; and :4 40 |
109 6D Keyboard F18 |
177 B1 Keypad 000 |
|
52 34 Keyboard ‘ and “4 41 |
110 6E Keyboard F19 |
178 B2 Thousands Separator 33 |
|
53 35 Keyboard Grave Accent and Tilde4 1 |
111 6F Keyboard F20 |
179 B3 Decimal Separator 33 |
|
54 36 Keyboard, and <4 53 |
112 70 Keyboard F21 |
180 B4 Currency Unit 34 |
|
55 37 Keyboard . and >4 54 |
113 71 Keyboard F22 |
181 B5 Currency Sub-unit 34 |
|
56 38 Keyboard / and ?4 55 |
114 72 Keyboard F23 |
182 B6 Keypad ( |
|
57 39 Keyboard Caps Lock11 30 |
115 73 Keyboard F24 |
183 B7 Keypad ) |
转载请注明:徐自远的乱七八糟小站 » USB-HID鼠标、键盘通讯格式