Skip to content

Commit 4de30a0

Browse files
committed
Add keyboard mouse demo.
1 parent c48a331 commit 4de30a0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

examples/macropad_keyboard_mouse.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
MacroPad HID keyboard and mouse demo. The demo sends "a" when the first key is pressed, a "B" when
6+
the second key is pressed, "Hello, World!" when the third key is pressed, and decreases the volume
7+
when the fourth key is pressed. It sends a right mouse click when the rotary encoder switch is
8+
pressed. Finally, it moves the mouse left and right when the rotary encoder is rotated
9+
counterclockwise and clockwise respectively.
10+
"""
11+
from adafruit_macropad import MacroPad
12+
13+
macropad = MacroPad()
14+
15+
last_position = 0
16+
while True:
17+
key_event = macropad.keys.events.get()
18+
19+
if key_event:
20+
if key_event.pressed:
21+
if key_event.key_number is 0:
22+
macropad.keyboard.send(macropad.Keycode.A)
23+
if key_event.key_number is 1:
24+
macropad.keyboard.press(macropad.Keycode.SHIFT, macropad.Keycode.B)
25+
macropad.keyboard.release_all()
26+
if key_event.key_number is 2:
27+
macropad.keyboard_layout.write("Hello, World!")
28+
if key_event.key_number is 3:
29+
macropad.consumer_control.send(
30+
macropad.ConsumerControlCode.VOLUME_DECREMENT
31+
)
32+
33+
macropad.encoder_switch_debounced.update()
34+
35+
if macropad.encoder_switch_debounced.pressed:
36+
macropad.mouse.click(macropad.Mouse.RIGHT_BUTTON)
37+
38+
current_position = macropad.encoder
39+
40+
if macropad.encoder > last_position:
41+
macropad.mouse.move(x=+5)
42+
last_position = current_position
43+
44+
if macropad.encoder < last_position:
45+
macropad.mouse.move(x=-5)
46+
last_position = current_position

0 commit comments

Comments
 (0)