Skip to content

Commit 2c6641c

Browse files
committed
* update hid doc
1 parent 251a932 commit 2c6641c

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

docs/doc/en/peripheral/hid.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Note: Since only 4 USB devices are supported, only 4 devices can be started at t
2020

2121
You need to enable `HID Keyboard` to run it.
2222

23-
The following example sends `rstuv` four characters through the keyboard and then releases the key.
23+
The following example sends keyboard event to pc.
2424

2525
```python
2626
from maix import hid, time
@@ -35,6 +35,14 @@ for key in keys:
3535

3636
```
3737

38+
After creating the `hid` object, key events can be sent using the `write` method. A key event is represented by an 8-byte array, where:
39+
- **Byte 1**: Indicates the status of modifier keys like `ctrl`, `shift`, `alt`, etc. Each bit represents a specific modifier key:`bit0: left ctrl`,`bit1: left shift`,`bit2: left alt`,`bit3: left GUI (e.g., Windows key)`,`bit4: right ctrl`,`bit5: right shift`,`bit6: right alt`,`bit7: right GUI`
40+
- **Byte 2**: Reserved byte.
41+
- **Byte 3**: Primary key value. A value of 0 means the key is released. Key codes are referenced from the "Universal Serial Bus HID Usage Tables" section of the [USB HID documentation](https://www.usb.org).
42+
- **Bytes 4~8**: Additional keys, used to press multiple keys at once. A value of 0 means the key is released.
43+
44+
For specific usage, refer to the example code above.
45+
3846
## Write a mouse in MaixPy.
3947

4048
You need to enable `HID Mouse` to run it.
@@ -93,4 +101,3 @@ while True:
93101
break
94102
```
95103

96-

docs/doc/zh/peripheral/hid.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,45 @@ HID(Human Interface Device)设备是一类计算机外围设备,用于向
1919

2020
需要使能了`HID Keyboard`后才能运行。
2121

22-
下面示例中,通过键盘发送`rstuv`四个字符,然后松开按键。
22+
下面示例中,向PC发送按键事件
2323

2424
```python
2525
from maix import hid, time
2626

2727
keyboard = hid.Hid(hid.DeviceType.DEVICE_KEYBOARD)
2828

29-
# 按键编号参考[USB HID文档](https://www.usb.org))的"Universal Serial Bus HID Usage Tables"部分
30-
keys = [21, 22, 23, 24, 25, 0] # 表示[r, s, t, u, v, 0], 0表示松开按键
31-
32-
for key in keys:
29+
def press(keyboard, key):
3330
keyboard.write([0, 0, key, 0, 0, 0, 0, 0])
31+
time.sleep_ms(50)
32+
keyboard.write([0, 0, 0, 0, 0, 0, 0, 0]) # key=0, means release
33+
34+
def press2(keyboard, key0 = 0, key1 = 0, key2 = 0, key3 = 0, key4 = 0, key5 = 0):
35+
keyboard.write([0, 0, key0, key1, key2, key3, key4, key5])
36+
time.sleep_ms(50)
37+
keyboard.write([0, 0, 0, 0, 0, 0, 0, 0]) # key=0, means release
38+
39+
# key0: 0x1:left-ctrl 0x2:left-shift 0x4:left-alt 0x8:left-windows
40+
# 0x10:right-ctrl 0x20:right-shift 0x40:right-alt 0x80:right-windows
41+
def press3(keyboard, key0, key1):
42+
keyboard.write([key0, 0, key1, 0, 0, 0, 0, 0])
43+
time.sleep_ms(50)
44+
keyboard.write([0, 0, 0, 0, 0, 0, 0, 0]) # key=0, means release
3445

46+
# 按键编号参考[USB HID文档](https://www.usb.org))的"Universal Serial Bus HID Usage Tables"部分
47+
press(keyboard, 21) # press 'r'
48+
press2(keyboard, 23, 24) # press 'tu'
49+
press3(keyboard, 0x2, 25) # press 'left-shift + v'
3550
```
3651

52+
创建`hid`对象后,通过`write`方法来发送按键事件,按键事件由一个8字节的数组表示,其中:
53+
54+
- 第1字节:指示 `ctrl``shift``alt` 等修饰键状态,字节每一位代表一个修饰键:`bit0: 左ctrl``bit1:左shift``bit2:左alt``bit3:左GUI(例如windows键)``bit4:右ctrl``bit5:右shift``bit6:右alt``bit7:右GUI`
55+
- 第2字节:保留字节
56+
- 第3字节:主按键值,0代表松开按键。按键编号参考[USB HID文档](https://www.usb.org))的"Universal Serial Bus HID Usage Tables"部分
57+
- 第4~8字节:其他按键,可以用来实现一次按下多个按键,0代表松开按键
58+
59+
具体使用方法可以参考上面示例的代码
60+
3761
## 用MaixPy编写一个鼠标
3862

3963
需要使能了`HID Mouse`后才能运行。

examples/peripheral/usb/hid_keyboard.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ def press(keyboard, key):
1515
time.sleep_ms(50)
1616
keyboard.write([0, 0, 0, 0, 0, 0, 0, 0]) # key=0, means release
1717

18+
def press2(keyboard, key0 = 0, key1 = 0, key2 = 0, key3 = 0, key4 = 0, key5 = 0):
19+
keyboard.write([0, 0, key0, key1, key2, key3, key4, key5])
20+
time.sleep_ms(50)
21+
keyboard.write([0, 0, 0, 0, 0, 0, 0, 0]) # key=0, means release
22+
23+
# key0: 0x1:left-ctrl 0x2:left-shift 0x4:left-alt 0x8:left-windows
24+
# 0x10:right-ctrl 0x20:right-shift 0x40:right-alt 0x80:right-windows
25+
def press3(keyboard, key0, key1):
26+
keyboard.write([key0, 0, key1, 0, 0, 0, 0, 0])
27+
time.sleep_ms(50)
28+
keyboard.write([0, 0, 0, 0, 0, 0, 0, 0]) # key=0, means release
29+
1830
# Refer to the "Universal Serial Bus HID Usage Tables" section of the official documentation(https://www.usb.org).
1931
press(keyboard, 21) # press 'r'
20-
press(keyboard, 22) # press 's'
21-
press(keyboard, 23) # press 't'
22-
press(keyboard, 24) # press 'u'
23-
press(keyboard, 25) # press 'v'
32+
press2(keyboard, 23, 24) # press 'tu'
33+
press3(keyboard, 0x2, 25) # press 'left-shift + v'
2434

2535
print('OK')

0 commit comments

Comments
 (0)