Skip to content

Commit 30097f7

Browse files
authored
Merge pull request #116 from michalpokusa/layout-write-delay
KeyboardLayout.write() delay parameter
2 parents 9ec404f + c334f66 commit 30097f7

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

adafruit_hid/keyboard_layout_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
except ImportError:
1717
pass
1818

19+
from time import sleep
1920

2021
__version__ = "0.0.0+auto.0"
2122
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HID.git"
@@ -88,10 +89,11 @@ def _write(self, keycode: int, altgr: bool = False) -> None:
8889
self.keyboard.press(keycode)
8990
self.keyboard.release_all()
9091

91-
def write(self, string: str) -> None:
92+
def write(self, string: str, delay: float = None) -> None:
9293
"""Type the string by pressing and releasing keys on my keyboard.
9394
9495
:param string: A string of UTF-8 characters to convert to key presses and send.
96+
:param float delay: Optional delay in seconds between key presses.
9597
:raises ValueError: if any of the characters has no keycode
9698
(such as some control characters).
9799
@@ -122,6 +124,9 @@ def write(self, string: str) -> None:
122124
)
123125
)
124126

127+
if delay is not None:
128+
sleep(delay)
129+
125130
def keycodes(self, char: str) -> Tuple[int, ...]:
126131
"""Return a tuple of keycodes needed to type the given character.
127132

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 possible 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)