Adding Consumer Key Support to MACROPAD Hotkey

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
BrookeDot
 
Posts: 34
Joined: Sat Sep 05, 2015 12:34 pm

Adding Consumer Key Support to MACROPAD Hotkey

Post by BrookeDot »

This is just a quick project share in the event it's helpful to others. When I first saw Adafruit getting into Keyboards I was excited as my current keyboard does not have media keys. *Spoiler for Adabox 019* When the MACROPAD came in the Adabox 019 I was ecstatic it was just what I needed. On top of that Phillip's tutorial on HotKeys and JP's 3D Printed case were the perfect place to start.

The only problem was that the HotKey example code did not support Consumer Keys out of the box. I thought about how to solve this for a bit. I originally tested by hard coding a specific key to be the Consumer Key but wanted more flexibility. In the end I decide to add a forth parameter to the array to determine if it was a consumer key.

For example instead of:

Code: Select all

        # COLOR    LABEL    KEY SEQUENCE  
        (0xA50000, 'Copy', [Keycode.COMMAND, c]),                 # Copy
You have:

Code: Select all


from adafruit_hid.keycode import Keycode # REQUIRED if using Keycode.* values
from adafruit_hid.consumer_control import ConsumerControl # REQUIRED if using ConsumerControlCode.* values
from adafruit_hid.consumer_control_code import ConsumerControlCode # REQUIRED if using ConsumerControlCode.* values

        # COLOR    LABEL    KEY SEQUENCE   CONSUMER CONTROL
        (0xA50000, 'Mute', [Keycode.COMMAND, c], False),                       # Copy
        (0xA50000, 'Play', [ConsumerControlCode.PLAY_PAUSE], True),  # Play/Pause
The reason for this is that to use Consumer Keys you must use `MACROPAD.consumer_control.send(KEY)` instead of `MACROPAD.keyboard.press(KEY)`. To accomplish this I added another conditional within the code:

Code: Select all

 SEQUENCE = APPS[APP_INDEX].macros[KEY_NUMBER][2]
 CONSUMER_KEY = APPS[APP_INDEX].macros[KEY_NUMBER][3]

    if PRESSED:
        if KEY_NUMBER < 12: # No pixel for encoder button
            MACROPAD.pixels[KEY_NUMBER] = 0xECEBE6
            MACROPAD.pixels.show()
        for item in SEQUENCE:
            # If the fouth array value is set to true, 1 or True then send composer key.
            if CONSUMER_KEY:
                MACROPAD.consumer_control.send(item)
            if isinstance(item, int):
                if item >= 0:
                    MACROPAD.keyboard.press(item)
                else:
                    MACROPAD.keyboard.release(item)
            else:
                MACROPAD.keyboard_layout.write(item)
    else:
...
The other change was to explicitly include the Consumer Key parts of HID in the macro files. I put a working Gist together so you can see it all put together:
https://gist.github.com/BrookeDot/32ff2 ... 3848bf02f9

Happy to create a PR upstream is this is helpful to others, and of course always open to feedback on other idea or if there's a different approach you may take.

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: Adding Consumer Key Support to MACROPAD Hotkey

Post by kattni »

Hello! I wanted to let you know I did something similar in this example: https://github.com/adafruit/Adafruit_Le ... ortcuts.py

The associated code is here: https://github.com/adafruit/Adafruit_Le ... ps/code.py

It's not the best solution, but it was the best I could come up with at the time.

User avatar
tannewt
 
Posts: 3298
Joined: Thu Oct 06, 2016 8:48 pm

Re: Adding Consumer Key Support to MACROPAD Hotkey

Post by tannewt »

Awesome work Brooke! A PR would be welcome. I do think we should use an enum or constants for the type instead of true/false because we may want to support mouse and gamepad in the future too.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”