Skip to content

Commit a81dfa7

Browse files
committed
Added example for .write() and updated docs
1 parent 717e05c commit a81dfa7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
.. automodule:: adafruit_hid.keyboard_layout_us
1111
:members:
12+
:inherited-members:
1213

1314
.. automodule:: adafruit_hid.keyboard_layout_base
1415
:members:

docs/examples.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ Send ALT+Tab for swapping windows, and CTRL+K for searching in a browser.
1616
:caption: examples/hid_keyboard_shortcuts.py
1717
:linenos:
1818

19+
Keyboard Layout
20+
---------------
21+
22+
While the ``Keyboard`` class itself provides easy way for sending key shortcuts, for writing more
23+
complex text you may want to use a ``KeyboardLayout`` and a ``.write()`` method.
24+
25+
It is also posible to adjust the typing speed by specifying ``delay`` between key presses.
26+
27+
.. literalinclude:: ../examples/hid_keyboard_layout.py
28+
:caption: examples/hid_keyboard_layout.py
29+
:emphasize-lines: 12-13,29,33
30+
:linenos:
31+
1932
Simple Gamepad
2033
---------------
2134

examples/hid_keyboard_layout.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import digitalio
7+
import usb_hid
8+
9+
from adafruit_hid.keyboard import Keyboard
10+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
11+
12+
keyboard = Keyboard(usb_hid.devices)
13+
layout = KeyboardLayoutUS(keyboard)
14+
15+
16+
# define buttons. these can be any physical switches/buttons, but the values
17+
# here work out-of-the-box with a CircuitPlayground Express' A and B buttons.
18+
slow_write = digitalio.DigitalInOut(board.D4)
19+
slow_write.direction = digitalio.Direction.INPUT
20+
slow_write.pull = digitalio.Pull.DOWN
21+
22+
fast_write = digitalio.DigitalInOut(board.D5)
23+
fast_write.direction = digitalio.Direction.INPUT
24+
fast_write.pull = digitalio.Pull.DOWN
25+
26+
while True:
27+
# Write `Hello World!` slowly
28+
if slow_write.value:
29+
layout.write("Hello World!", delay=0.2)
30+
31+
# Write `Hello World!` normally
32+
elif fast_write.value:
33+
layout.write("Hello World!")
34+
35+
time.sleep(0.1)

0 commit comments

Comments
 (0)