In order to process each of the commands a a separate keypress, I need to change to format of the macro dictionary
example_maco.py
Code: Select all
# MACROPAD Hotkeys example: Multi-Keypress command Example
#from adafruit_hid.keycode import Keycode # REQUIRED if using Keycode.* values
#from adafruit_hid.keyboard import Keyboard # REQUIRED if using Keycode.* values
import usb_hid
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
kbd = Keyboard(usb_hid.devices)
app = { # REQUIRED dict, must be named 'app'
'name' : 'Example', # Application name
'macros' : [ # List of button macros...
# COLOR LABEL FLAG(Optional) KEY SEQUENCE
# 1st row ----------
(0x200000, 'Chrome', [999], [Keycode.WINDOWS, 'chrome', Keycode.ENTER]), #ADDED - Note the extra list that contains the 999
(0x000040, 'Reopen Tab', [Keycode.CONTROL, Keycode.SHIFT, 't']),
(0x000000, '?', []),
# 2nd row ---------
(0x000000, '?', []),
(0x000000, '?', []),
(0x000000, '?', []),
# 3rd row ----------
(0x000000, '?', []),
(0x000000, '?', []),
(0x000000, '?', []),
# 4th row ----------
(0x000000, '?', []),
(0x000000, '?', []),
(0x000000, '?', []),
# Encoder button ---
(0x000000, '', []) #
]
}
I also needed to modify the method that processes the keypress.
code.py -
This is just the main while loop not the complete file!!
Code: Select all
while True:
# Read encoder position. If it's changed, switch apps.
POSITION = MACROPAD.encoder
if POSITION != LAST_POSITION:
APP_INDEX = POSITION % len(APPS)
APPS[APP_INDEX].switch()
LAST_POSITION = POSITION
# Handle encoder button. If state has changed, and if there's a
# corresponding macro, set up variables to act on this just like
# the keypad keys, as if it were a 13th key/macro.
MACROPAD.encoder_switch_debounced.update()
ENCODER_SWITCH = MACROPAD.encoder_switch_debounced.pressed
if ENCODER_SWITCH != LAST_ENCODER_SWITCH:
LAST_ENCODER_SWITCH = ENCODER_SWITCH
if len(APPS[APP_INDEX].macros) < 13:
continue # No 13th macro, just resume main loop
KEY_NUMBER = 12 # else process below as 13th macro
PRESSED = ENCODER_SWITCH
else:
EVENT = MACROPAD.keys.events.get()
if not EVENT or EVENT.key_number >= len(APPS[APP_INDEX].macros):
continue # No key events, or no corresponding macro, resume loop
KEY_NUMBER = EVENT.key_number
PRESSED = EVENT.pressed
# If code reaches here, a key or the encoder button WAS pressed/released
# and there IS a corresponding macro available for it...other situations
# are avoided by 'continue' statements above which resume the loop.
SEQUENCE = APPS[APP_INDEX].macros[KEY_NUMBER][2]
if PRESSED:
if KEY_NUMBER < 12: # No pixel for encoder button
MACROPAD.pixels[KEY_NUMBER] = 0xFF0000 #ADDED - I like it flashing red
MACROPAD.pixels.show()
for item in SEQUENCE:
if isinstance(item, int):
if item >= 0:
if item != 999: #ADDED - Detect if it is a special command set
MACROPAD.keyboard.press(item)
else:
SEP_ITEMS = APPS[APP_INDEX].macros[KEY_NUMBER][3] #ADDED - Start to process special command set - get the extra list
for this_item in SEP_ITEMS: #ADDED - loop over list
if isinstance(this_item, int) and this_item >= 0: #ADDED - is it an object
MACROPAD.keyboard.press(this_item) #ADDED - press command
MACROPAD.keyboard.release(this_item) #ADDED - release command
time.sleep(.25) #ADDED - WAIT! it needs this because the UI needs time to react
else: #ADDED
MACROPAD.keyboard_layout.write(this_item) #ADDED - Process text (not a keycode object)
time.sleep(.25) #ADDED - WAIT! it needs this because the UI needs time to react
else:
MACROPAD.keyboard.release(item)
else:
MACROPAD.keyboard_layout.write(item)
else:
# Release any still-pressed modifier keys
for item in SEQUENCE:
if isinstance(item, int) and item >= 0:
MACROPAD.keyboard.release(item)
if KEY_NUMBER < 12: # No pixel for encoder button
MACROPAD.pixels[KEY_NUMBER] = APPS[APP_INDEX].macros[KEY_NUMBER][0]
MACROPAD.pixels.show()
This is still very rough. I was just trying to get it working. I intend on refactoring to whole thing to make it more flexible and allow single and multiple keypress commands under the same key.
I hope this helps, feel free to ask follow-up questions.
Cheers,
-Jim