0

HELP NEEDED: Macropad App Launcher
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

HELP NEEDED: Macropad App Launcher

by pr3cis1on on Sun Aug 01, 2021 10:14 pm

I'm trying to use the macropad to launch applications similar to an Elgato Streamdeck. So I'm trying to use the Windows key, type the name off the application, then press enter. Here is my code. It does not work. Has anyone else tried this?

Code: Select all | TOGGLE FULL SIZE
from adafruit_hid.keycode import Keycode # REQUIRED if using Keycode.* values

app = {                    # REQUIRED dict, must be named 'app'
    'name' : 'App Launcher', # Application name
    'macros' : [           # List of button macros...
        # COLOR    LABEL    KEY SEQUENCE
        # 1st row ----------
        (0x004000, 'Brave', [Keycode.LEFT_GUI, -Keycode.LEFT_GUI, Keycode.SHIFT, 'brave', Keycode.ENTER]),


Any help is very much appreciated!

pr3cis1on
 
Posts: 4
Joined: Thu Dec 17, 2020 8:22 pm

Re: HELP NEEDED: Macropad App Launcher

by belsonc on Mon Aug 02, 2021 12:48 pm

I put something together quickly over the weekend to macro some expressions I use a lot in a chat, and I pass a new line character instead of an enter - that might work here?

belsonc
 
Posts: 38
Joined: Tue Sep 15, 2020 8:39 pm

Re: HELP NEEDED: Macropad App Launcher

by pr3cis1on on Mon Aug 02, 2021 8:51 pm

belsonc wrote:I put something together quickly over the weekend to macro some expressions I use a lot in a chat, and I pass a new line character instead of an enter - that might work here?


Hmm not sure. The problem I'm having is that when I use the Keycode.LEFT_GUI by itself, I do have the start menu open. As soon as I add the text afterward, the text is written to whatever program is open and I never see the start menu open. Is there a way to add a 'wait' command or something?

pr3cis1on
 
Posts: 4
Joined: Thu Dec 17, 2020 8:22 pm

Re: HELP NEEDED: Macropad App Launcher

by pr3cis1on on Mon Aug 02, 2021 9:39 pm

I think I've narrowed it down to the commands are being sent too quickly. So quickly that some commands are being ignored. This might be limited to my laptop being slightly old, but if we could somehow add a wait command, that would be ideal. If someone knows how to add that, let me know.

pr3cis1on
 
Posts: 4
Joined: Thu Dec 17, 2020 8:22 pm

Re: HELP NEEDED: Macropad App Launcher

by jimjimbobim on Tue Aug 03, 2021 10:06 pm

I got this to work on windows. I wanted to open chrome.
The problem is that the commands for a key in the macro file are basically being sent one after another, first as a press and then as a release, so all held down at once and then released, like ctrl-alt-del, etc. (sort of, there is a six character limit in the library)
I am on my phone right now, so I will post what I did to solve it later tonight.
jimjimbobim
 
Posts: 4
Joined: Thu Jan 03, 2013 12:24 am

Re: HELP NEEDED: Macropad App Launcher

by jimjimbobim on Wed Aug 04, 2021 2:10 am

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 | TOGGLE FULL SIZE
# 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 | TOGGLE FULL SIZE
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
jimjimbobim
 
Posts: 4
Joined: Thu Jan 03, 2013 12:24 am

Re: HELP NEEDED: Macropad App Launcher

by jimjimbobim on Wed Aug 04, 2021 4:21 pm

Update:
I discovered that when I tried to put in a full program path into the window search box that if the were any newline characters (\n) in the path, it would throw an error. This is likely true for other special characters as well.

The solution that worked for me was to use a raw text string (Note the 'r' in front of the program path):
Code: Select all | TOGGLE FULL SIZE
(0x002000, 'Notepad++', [999], [Keycode.WINDOWS, r'C:\Program Files\Notepad++\notepad++.exe', Keycode.ENTER]),
jimjimbobim
 
Posts: 4
Joined: Thu Jan 03, 2013 12:24 am

Re: HELP NEEDED: Macropad App Launcher

by blakbuzzrd on Wed Aug 11, 2021 5:28 pm

Why not follow a process similar to the one detailed here? https://learn.adafruit.com/launch-deck-trellis-m4/windows

blakbuzzrd
 
Posts: 8
Joined: Tue Feb 09, 2021 10:12 pm

Re: HELP NEEDED: Macropad App Launcher

by jimjimbobim on Sat Aug 14, 2021 8:03 pm

This works very well, I don’t really need another solution. Also, I am not interested in installing an app to make it work. I want to be able to use the macropad on a different machine and have it just work without having to install anything extra.
jimjimbobim
 
Posts: 4
Joined: Thu Jan 03, 2013 12:24 am

Please be positive and constructive with your questions and comments.