@@ -10,8 +10,7 @@ Introduction
10
10
:target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
11
11
:alt: Gitter
12
12
13
- This driver provides USB HID related constants. In the future it will include
14
- helper functions and classes as well.
13
+ This driver simulates USB HID devices. Currently keyboard and mouse are implemented.
15
14
16
15
Dependencies
17
16
=============
@@ -26,39 +25,82 @@ This is easily achieved by downloading
26
25
Usage Example
27
26
=============
28
27
29
- The current `keyboard ` module stores key constants which make it easier to
30
- construct keypress reports for a keyboard device.
28
+ The ``Keyboard `` class sends keypress reports for a USB keyboard device to the host.
29
+
30
+ The ``Keycode `` class defines USB HID keycodes to send using ``Keyboard ``.
31
31
32
32
.. code-block :: python
33
33
34
- import usb_hid
35
- import adafruit_hid.keyboard as kbd
36
- import time
34
+ from adafruit_hid.keyboard import Keyboard
35
+ from adafruit_hid.keycode import Keycode
36
+
37
+ # Set up a keyboard device.
38
+ kbd = Keyboard()
39
+
40
+ # Type control-x.
41
+ kbd.press(Keycode.CONTROL , Keycode.X)
42
+ kbd.release_all()
43
+
44
+ # Type capital 'A'.
45
+ kbd.press(Keycode.SHIFT , Keycode.A)
46
+ kbd.release_all()
47
+
48
+ # Press and hold the shifted '1' key to get '!' (exclamation mark).
49
+ kbd.press(Keycode.SHIFT , Keycode.ONE )
50
+ # Release the ONE key and send another report.
51
+ kbd.release(Keycode.ONE )
52
+ # Press shifted '2' to get '@'.
53
+ kbd.press(Keycode.TWO )
54
+ # Release all keys.
55
+ kbd.release_all()
56
+
57
+ The ``KeyboardLayoutUS `` sends ASCII characters using keypresses. It assumes
58
+ the host is set to accept keypresses from a US keyboard.
59
+
60
+ If the host is expecting a non-US keyboard, the character to key mapping provided by
61
+ ``KeyboardLayoutUS `` will not always be correct.
62
+ Different keypresses will be needed in some cases. For instance, to type an ``'A' `` on
63
+ a French keyboard (AZERTY instead of QWERTY), ``Keycode.Q `` should be pressed.
64
+
65
+ Currently this package provides only ``KeyboardLayoutUS ``. More ``KeyboardLayout ``
66
+ classes could be added to handle non-US keyboards and the different input methods provided
67
+ by various operating systems.
68
+
69
+ .. code-block :: python
70
+
71
+ from adafruit_hid.keyboard import Keyboard
72
+ from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
73
+
74
+ kbd = Keyboard()
75
+ layout = KeyboardLayoutUS(kbd)
76
+
77
+ # Type 'abc' followed by Enter (a newline).
78
+ layout.write(' abc\n ' )
79
+
80
+ # Get the keycodes needed to type a '$'.
81
+ # The method will return (Keycode.SHIFT, Keycode.FOUR).
82
+ keycodes = layout.keycodes(' $' )
83
+
84
+ The ``Mouse `` class simulates a three-button mouse with a scroll wheel.
85
+
86
+ .. code-block :: python
87
+
88
+ from adafruit_hid.mouse import Mouse
89
+
90
+ m = Mouse()
37
91
38
- report = bytearray (8 ) # Keyboard reports are always 8 bytes.
92
+ # Click the left mouse button.
93
+ m.click(Mouse.LEFT_BUTTON )
39
94
40
- # Devices are initialized earlier so find the one for the keyboard.
41
- keyboard = None
42
- for device in usb_hid.devices:
43
- if device.usage_page is 0x 1 and device.usage is 0x 06 :
44
- keyboard = device
45
- break
95
+ # Move the mouse diagonally to the upper left.
96
+ m.move(- 100 , 100 , 0 )
46
97
47
- # The first byte of the report includes a bitfield indicating which
48
- # modifiers are pressed. Their bit position is their code's difference from
49
- # 0xE0.
50
- report[0 ] |= 1 << (kbd.LEFT_SHIFT - 0x E0 )
51
- # Normal keys are simply their byte code in bytes 2-7. When fewer than six
52
- # keys are pressed then the trailing bytes are zero.
53
- report[2 ] = kbd.A
54
- keyboard.send_report(report)
98
+ # Move the mouse while holding down the left button.
99
+ m.press(Mouse.LEFT_BUTTON )
100
+ m.move(50 , 20 , 0 )
101
+ m.release_all() # or m.release(Mouse.LEFT_BUTTON)
55
102
56
- time.sleep(0.1 )
57
103
58
- # Clear the key presses and send another report.
59
- report[0 ] = 0
60
- report[2 ] = 0
61
- keyboard.send_report(report)
62
104
63
105
Contributing
64
106
============
@@ -73,4 +115,7 @@ API Reference
73
115
.. toctree ::
74
116
:maxdepth: 2
75
117
76
- api
118
+ keyboard
119
+ keycode
120
+ keyboard_layout_us
121
+ mouse
0 commit comments