如何检查 mac bash 应用程序启动时是否按下了选项键

发布于 2024-10-04 13:07:48 字数 384 浏览 0 评论 0原文

我有一个相对简单的问题。

我有一些 Mac 应用程序,其启动器是用 bash 编写的。 我想向启动器添加一个小功能,让其他人在应用程序启动时按“option/alt”键时访问 config.app 或位于 /Contents/Resources 中的其他内容。有点像 iTunes 或 iPhoto,您可以在其中访问一个小选项菜单。

我不知道纯粹在 bash 中的代码应该是什么样子;我发现了一些使用 applescript 和/或 cocoa hooks 的例子,但没有一个纯粹在 bash 中。

类似于: if 'optionKeyDown';然后打开“$WORKDIR/../Resources/config.app”

或者这在纯 bash 中根本不可能?

I got a relatively simple question.

I have a few mac applications that have launchers written in bash.
I wanted to add a little feature to the launchers, by letting others access a config.app or something else located in /Contents/Resources, when they press the 'option/alt' key at the startup of the app. Kinda like iTunes or iPhoto, where you can access a little options menu.

I don't know how the code should look like when it's purely in bash; i found a few examples that make use of applescript and/or cocoa hooks, but none purely in bash.

Something like: if 'optionKeyDown'; then open "$WORKDIR/../Resources/config.app"

Or is this not possible in pure bash at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2024-10-11 13:07:48

这里有一个很好的解决方案: http://lists.apple.com /archives/applescript-users/2009/Sep/msg00374.html

获取以下代码:

#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;
}

将其粘贴到名为keys.m 的文件中。

然后构建一个像这样的命令行实用程序:

$ gcc keys.m -framework Carbon -o keys

keys 可执行文件放在路径中的某个位置,例如 /usr/local/bin,甚至放在与 bash 相同的目录中脚本,然后您可以将其称为 keys option 并检查返回的字符串是否为“0”或“1”。

There's 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, e.g. keys.m.

Then build a command line utility like this:

$ gcc keys.m -framework Carbon -o keys

Put the keys executable somewhere in your path, e.g. /usr/local/bin, or even just in the same directory as your bash script, then you can call it as e.g. keys option and check the returned string for "0" or "1".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文