Skip to content

Commit c81da17

Browse files
Merge pull request #1783 from henrygab/float_as_delay
Add support for delay in key sequences
2 parents 4c0f146 + 31d173f commit c81da17

5 files changed

Lines changed: 450 additions & 1 deletion

File tree

Macropad_Hotkeys/code.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# pylint: disable=import-error, unused-import, too-few-public-methods
1010

1111
import os
12+
import time
1213
import displayio
1314
import terminalio
1415
from adafruit_display_shapes.rect import Rect
@@ -128,6 +129,15 @@ def switch(self):
128129

129130
sequence = apps[app_index].macros[key_number][2]
130131
if pressed:
132+
# the sequence is arbitrary-length
133+
# each item in the sequence is either
134+
# an integer (e.g., Keycode.KEYPAD_MINUS),
135+
# a floating point value (e.g., 0.20)
136+
# or a string.
137+
# Positive Integers ==> key pressed
138+
# Negative Integers ==> key released
139+
# Float ==> sleep in seconds
140+
# String ==> each key in string pressed & released
131141
if key_number < 12: # No pixel for encoder button
132142
macropad.pixels[key_number] = 0xFFFFFF
133143
macropad.pixels.show()
@@ -137,10 +147,12 @@ def switch(self):
137147
macropad.keyboard.press(item)
138148
else:
139149
macropad.keyboard.release(-item)
150+
elif isinstance(item, float):
151+
time.sleep(item)
140152
else:
141153
macropad.keyboard_layout.write(item)
142154
else:
143-
# Release any still-pressed modifier keys
155+
# Release any still-pressed keys
144156
for item in sequence:
145157
if isinstance(item, int) and item >= 0:
146158
macropad.keyboard.release(item)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# MACROPAD Hotkeys example: Minecraft hotbar (inventory)
2+
3+
# Note: Must enable "full keyboad gameplay" for Prev/Next buttons to work.
4+
# This is found under "settings", then "keyboard and mouse".
5+
6+
from adafruit_hid.keycode import Keycode # REQUIRED if using Keycode.* values
7+
8+
app = { # REQUIRED dict, must be named 'app'
9+
'name' : 'Minecraft Hotbar', # Application name
10+
'macros' : [ # List of button macros...
11+
# COLOR LABEL KEY SEQUENCE
12+
# 1st row ----------
13+
(0x202000, '7', ['7']),
14+
(0x202000, '8', ['8']),
15+
(0x202000, '9', ['9']),
16+
# 2nd row ----------
17+
(0x202000, '4', ['4']),
18+
(0x202000, '5', ['5']),
19+
(0x202000, '6', ['6']),
20+
# 3rd row ----------
21+
(0x202000, '1', ['1']),
22+
(0x202000, '2', ['2']),
23+
(0x202000, '3', ['3']),
24+
# 4th row ----------
25+
(0x002020, 'Prev', [Keycode.PAGE_UP]),
26+
(0x000000, '', []),
27+
(0x002020, 'Next', [Keycode.PAGE_DOWN]),
28+
# Encoder button ---
29+
(0x000000, '', [])
30+
]
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# MACROPAD Hotkeys example: Minecraft Messaging
2+
3+
# NOTE: There appears to be a line length limit. Exceeding that limit appears
4+
# to result in silent failure. Therefore, the key sequences are split
5+
# across multiple lines.
6+
7+
from adafruit_hid.keycode import Keycode # REQUIRED if using Keycode.* values
8+
9+
# NOTE: There appears to be some delay when bringing up the command screen.
10+
11+
DELAY_AFTER_SLASH = 0.80 # required so minecraft has time to bring up command screen
12+
DELAY_BEFORE_RETURN = 0.10
13+
14+
# NOTE: On PC, characters are sometimes lost due to lag. No simple fix for
15+
# lost keystrokes is known. However, the commands do work most of the time.
16+
17+
18+
app = { # REQUIRED dict, must be named 'app'
19+
'name' : 'Minecraft (/msg)', # Application name
20+
'macros' : [ # List of button macros...
21+
# COLOR LABEL KEY SEQUENCE
22+
# 1st row ----------
23+
(0x000020, 'list', [
24+
'/', DELAY_AFTER_SLASH,
25+
'list',
26+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
27+
(0x000020, 'list', [
28+
'/', DELAY_AFTER_SLASH,
29+
'list',
30+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
31+
(0x000020, 'list', [
32+
'/', DELAY_AFTER_SLASH,
33+
'list',
34+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
35+
# 2nd row ----------
36+
(0x000000, '', []),
37+
(0x000000, '', []),
38+
(0x000000, '', []),
39+
# 3rd row ----------
40+
(0x000000, '', []),
41+
(0x000000, '', []),
42+
(0x000000, '', []),
43+
# 4th row ----------
44+
(0x101010, 'bed', [
45+
'/', DELAY_AFTER_SLASH,
46+
'msg @a Time for bed!',
47+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
48+
(0x101010, 'bed', [
49+
'/', DELAY_AFTER_SLASH,
50+
'msg @a Time for bed!',
51+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
52+
(0x101010, 'bed', [
53+
'/', DELAY_AFTER_SLASH,
54+
'msg @a Time for bed!',
55+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
56+
# Encoder button ---
57+
(0x000000, '', [])
58+
]
59+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# MACROPAD Hotkeys example: Minecraft Effects (Creative) for Bedrock Edition
2+
3+
# NOTE: There appears to be a line length limit. Exceeding that limit appears
4+
# to result in silent failure. Therefore, the key sequences are split
5+
# across multiple lines.
6+
7+
from adafruit_hid.keycode import Keycode # REQUIRED if using Keycode.* values
8+
9+
# See https://minecraft.fandom.com/wiki/Effect
10+
11+
DELAY_AFTER_SLASH = 0.80 # required so minecraft has time to bring up command screen
12+
DELAY_BEFORE_RETURN = 0.10 # give minecraft time to show all the keys pressed...
13+
14+
app = { # REQUIRED dict, must be named 'app'
15+
'name' : 'Minecraft PE (effect)', # Application name
16+
#
17+
# /effect <player: target> <effect: Effect>
18+
# [seconds: int] [amplifier: int] [hideParticles: Boolean]
19+
#
20+
'macros' : [ # List of button macros...
21+
# COLOR LABEL KEY SEQUENCE
22+
# 1st row ----------
23+
(0x002000, 'speed', [
24+
'/', DELAY_AFTER_SLASH,
25+
'effect @s speed 999999999 1 true',
26+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
27+
(0x002000, 'str', [
28+
'/', DELAY_AFTER_SLASH,
29+
'effect @s strength 999999999 1 true',
30+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
31+
(0x002000, 'haste', [
32+
'/', DELAY_AFTER_SLASH,
33+
'effect @s haste 999999999 1 true',
34+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
35+
# 2nd row ----------
36+
(0x002000, 'jump', [
37+
'/', DELAY_AFTER_SLASH,
38+
'effect @s jump_boost 999999999 1 true',
39+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
40+
(0x000030, 'breath', [
41+
'/', DELAY_AFTER_SLASH,
42+
'effect @s water_breathing 999999999 0 true',
43+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
44+
(0x202020, 'darkv', [
45+
'/', DELAY_AFTER_SLASH,
46+
'effect @s night_vision 999999999 0 true',
47+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
48+
# 3rd row ----------
49+
(0x300000, 'health', [
50+
'/', DELAY_AFTER_SLASH,
51+
'effect @s health_boost 999999999 4 true',
52+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
53+
(0x300000, 'regen', [
54+
'/', DELAY_AFTER_SLASH,
55+
'effect @s regeneration 999999999 4 true',
56+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
57+
(0x002000, 'absorb', [
58+
'/', DELAY_AFTER_SLASH,
59+
'effect @s absorption 999999999 3 true',
60+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
61+
# 4th row ---------
62+
(0x002000, 'resist', [
63+
'/', DELAY_AFTER_SLASH,
64+
'effect @s resistance 999999999 3 true',
65+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
66+
(0x101010, 'invis', [
67+
'/', DELAY_AFTER_SLASH,
68+
'effect @s invisibility 999999999 0 true',
69+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
70+
(0x300000, 'fire_r', [
71+
'/', DELAY_AFTER_SLASH,
72+
'effect @s fire_resistance 999999999 0 true',
73+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
74+
# Encoder button --- Remove all status effects....
75+
(0x000000, '', [
76+
'/', DELAY_AFTER_SLASH,
77+
'effect @s clear',
78+
DELAY_BEFORE_RETURN, Keycode.RETURN, -Keycode.RETURN]),
79+
]
80+
}

0 commit comments

Comments
 (0)