Skip to content

Commit 189a1c7

Browse files
authored
Merge pull request #1 from dhalbert/master
Add Keyboard class with API to send HID reports; prune constants; change examples
2 parents 603d7be + 9860fe9 commit 189a1c7

File tree

13 files changed

+894
-183
lines changed

13 files changed

+894
-183
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
__pycache__
2-
_build
3-
*.pyc
1+
__pycache__
2+
_build
3+
*.pyc
4+
.vscode

README.rst

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Introduction
1010
:target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
1111
:alt: Gitter
1212
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.
1514

1615
Dependencies
1716
=============
@@ -26,39 +25,82 @@ This is easily achieved by downloading
2625
Usage Example
2726
=============
2827

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``.
3131

3232
.. code-block:: python
3333
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()
3791
38-
report = bytearray(8) # Keyboard reports are always 8 bytes.
92+
# Click the left mouse button.
93+
m.click(Mouse.LEFT_BUTTON)
3994
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 0x1 and device.usage is 0x06:
44-
keyboard = device
45-
break
95+
# Move the mouse diagonally to the upper left.
96+
m.move(-100, 100, 0)
4697
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 - 0xE0)
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)
55102
56-
time.sleep(0.1)
57103
58-
# Clear the key presses and send another report.
59-
report[0] = 0
60-
report[2] = 0
61-
keyboard.send_report(report)
62104
63105
Contributing
64106
============
@@ -73,4 +115,7 @@ API Reference
73115
.. toctree::
74116
:maxdepth: 2
75117

76-
api
118+
keyboard
119+
keycode
120+
keyboard_layout_us
121+
mouse

adafruit_hid/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
`adafruit_hid`
2525
====================================================
2626
27-
TODO(description)
27+
This driver simulates USB HID devices. Currently keyboard and mouse are implemented.
2828
29-
* Author(s): Scott Shawcroft
29+
* Author(s): Scott Shawcroft, Dan Halbert
3030
"""

0 commit comments

Comments
 (0)