How to check if option key is pressed when running mac bash application

My question is relatively simple.

I have several Mac apps that have launchers written in bash. I wanted to add a little function to the launchers by giving others access to config.app or something else in / Contents / Resources when they press the "option / alt" key when the application starts. Kinda, like iTunes or iPhoto, where you can access a small options menu.

I do not know what the code looks like when it is pure in bash; I found several examples that use applescript and / or cocoa hooks, but none of them were in bash.

Sort of: if 'optionKeyDown'; then open "$WORKDIR/../Resources/config.app"

Or is this generally not possible in pure bash?

+3
source share
1 answer

There is a good solution here: http://lists.apple.com/archives/applescript-users/2009/Sep/msg00374.html

Take the following code:

#import <Carbon/Carbon.h>

int main (int argc, const char * argv[]) {
    unsigned int modifiers = GetCurrentKeyModifiers();

    if (argc == 1)
        printf("%d\n", modifiers);

    else {

        int i, result = 1;
        for (i = 1; i < argc; ++i) {

            if (0 == strcmp(argv[i], "shift"))
                result = result && (modifiers & shiftKey);

            else if (0 == strcmp(argv[i], "option"))
                result = result && (modifiers & optionKey);

            else if (0 == strcmp(argv[i], "cmd"))
                result = result && (modifiers & cmdKey);

            else if (0 == strcmp(argv[i], "control"))
                result = result && (modifiers & controlKey);

            else if (0 == strcmp(argv[i], "capslock"))
                result = result && (modifiers & alphaLock);

        }
        printf("%d\n", result);
    }
    return 0;
}

Paste it into a file called for example. keys.m.

Then create a command line utility as follows:

$ gcc keys.m -framework Carbon -o keys

Put the executable keyssomewhere in your path, for example. /usr/local/binor even in the same directory as your bash script, then you can name it, for example. keys optionand check the returned string for "0" or "1".

+2
source

Source: https://habr.com/ru/post/1777081/


All Articles