11"""
2- A fairly straightforward macro/hotkey program for Adafruit MACROPAD.
3- Macro key setups are stored in the /macros folder (configurable below),
4- load up just the ones you're likely to use. Plug into computer's USB port,
5- use dial to select an application macro set, press MACROPAD keys to send
6- key sequences.
2+ A macro/hotkey program for Adafruit MACROPAD. Macro setups are stored in the
3+ /macros folder (configurable below), load up just the ones you're likely to
4+ use. Plug into computer's USB port, use dial to select an application macro
5+ set, press MACROPAD keys to send key sequences and other USB protocols.
76"""
87
98# pylint: disable=import-error, unused-import, too-few-public-methods
@@ -44,6 +43,9 @@ def switch(self):
4443 macropad .pixels [i ] = 0
4544 group [i ].text = ''
4645 macropad .keyboard .release_all ()
46+ macropad .consumer_control .release ()
47+ macropad .mouse .release_all ()
48+ macropad .stop_tone ()
4749 macropad .pixels .show ()
4850 macropad .display .refresh ()
4951
@@ -129,15 +131,13 @@ def switch(self):
129131
130132 sequence = apps [app_index ].macros [key_number ][2 ]
131133 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
134+ # 'sequence' is an arbitrary-length list, each item is one of:
135+ # Positive integer (e.g. Keycode.KEYPAD_MINUS): key pressed
136+ # Negative integer: (absolute value) key released
137+ # Float (e.g. 0.25): delay in seconds
138+ # String (e.g. "Foo"): corresponding keys pressed & released
139+ # List []: one or more Consumer Control codes (can also do float delay)
140+ # Dict {}: mouse buttons/motion (might extend in future)
141141 if key_number < 12 : # No pixel for encoder button
142142 macropad .pixels [key_number ] = 0xFFFFFF
143143 macropad .pixels .show ()
@@ -149,13 +149,49 @@ def switch(self):
149149 macropad .keyboard .release (- item )
150150 elif isinstance (item , float ):
151151 time .sleep (item )
152- else :
152+ elif isinstance ( item , str ) :
153153 macropad .keyboard_layout .write (item )
154+ elif isinstance (item , list ):
155+ for code in item :
156+ if isinstance (code , int ):
157+ macropad .consumer_control .release ()
158+ macropad .consumer_control .press (code )
159+ if isinstance (code , float ):
160+ time .sleep (code )
161+ elif isinstance (item , dict ):
162+ if 'buttons' in item :
163+ if item ['buttons' ] >= 0 :
164+ macropad .mouse .press (item ['buttons' ])
165+ else :
166+ macropad .mouse .release (- item ['buttons' ])
167+ macropad .mouse .move (item ['x' ] if 'x' in item else 0 ,
168+ item ['y' ] if 'y' in item else 0 ,
169+ item ['wheel' ] if 'wheel' in item else 0 )
170+ if 'tone' in item :
171+ if item ['tone' ] > 0 :
172+ macropad .stop_tone ()
173+ macropad .start_tone (item ['tone' ])
174+ else :
175+ macropad .stop_tone ()
176+ elif 'play' in item :
177+ macropad .play_file (item ['play' ])
154178 else :
155- # Release any still-pressed keys
179+ # Release any still-pressed keys, consumer codes, mouse buttons
180+ # Keys and mouse buttons are individually released this way (rather
181+ # than release_all()) because pad supports multi-key rollover, e.g.
182+ # could have a meta key or right-mouse held down by one macro and
183+ # press/release keys/buttons with others. Navigate popups, etc.
156184 for item in sequence :
157- if isinstance (item , int ) and item >= 0 :
158- macropad .keyboard .release (item )
185+ if isinstance (item , int ):
186+ if item >= 0 :
187+ macropad .keyboard .release (item )
188+ elif isinstance (item , dict ):
189+ if 'buttons' in item :
190+ if item ['buttons' ] >= 0 :
191+ macropad .mouse .release (item ['buttons' ])
192+ elif 'tone' in item :
193+ macropad .stop_tone ()
194+ macropad .consumer_control .release ()
159195 if key_number < 12 : # No pixel for encoder button
160196 macropad .pixels [key_number ] = apps [app_index ].macros [key_number ][0 ]
161197 macropad .pixels .show ()
0 commit comments