如何以编程方式获取在系统偏好设置中设置的macOS键盘快捷方式? [英] How to get macOS keyboard shortcuts set in System Preferences programmatically?

查看:111
本文介绍了如何以编程方式获取在系统偏好设置中设置的macOS键盘快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在macOS上,使用英文键盘时,组合键CMD+Backtick用于在应用程序的打开的窗口中循环显示.例如,在德语键盘上,组合为CMD+<.甚至可以使用System Preferences-> Keyboard-> Shortcuts-> Keyboard-> Move focus to next window来配置此快捷方式.

On macOS the key combination CMD+Backtick is used to cycle through the open windows of an application when using an english keyboard. On German keyboards for example the combination is CMD+<. This shortcut can even be configured using System Preferences -> Keyboard -> Shortcuts -> Keyboard -> Move focus to next window.

对于使用FLTK的多窗口GUI应用程序,我想利用此快捷方式,但不知道如何获取用户在其系统上设置的组合.因此,我要寻找的是macOS系统调用,该调用为我提供了用于此Mac上Move focus to next window的组合键.

For my multi-window GUI application using FLTK I want to utilize this shortcut, but have no idea how to fetch the combination the user has set on his or her system. So what I'm looking for is a macOS system call that gives me the key combination that is used to Move focus to next window on this very Mac.

当然,如果使用FLTK有某种内建的方式,我宁愿使用本机系统调用,也不愿使用它.

Of course if there would be a somewhat builtin way using FLTK I'd prefer that over having to use native system calls.

搜索此问题是一场噩梦...

Googling for this issue is a nightmare ...

更新08/10/2017 Öö的 answer 给了我一些进一步研究的想法.从那以后,我了解到首选项存储在com.apple.symbolichotkeys中,更确切地说是存储在键27中.

Update 08/10/2017 Öö's answer gave me some ideas for additional research. I've since learned that the preferences are stored in com.apple.symbolichotkeys, more precisely in key 27.

        27 =         {
        enabled = 1;
        value =             {
            parameters =                 (
                98,
                11,
                524288
            );
            type = standard;
        };
    };

参数1(98):这是"b"的ASCII码.第一个参数具有所用快捷方式的ascii代码;如果它是非ascii字符,则为65535.

Parameter 1 (98): That's the ASCII code for "b". The first parameter has the ascii code of the shortcut used or 65535 if it's a non-ascii character.

参数2(11):这是kVK_ANSI_B的键盘代码(

Parameter 2 (11): That's the keyboard code for the kVK_ANSI_B (source). These codes are keyboard dependent. On a US keyboard, kVK_ANSI_Z is 0x06, while on a german keyboard it's 0x10.

参数3(524288):用于修饰键:

0x000000 => "No modifier",
0x020000 => "Shift",
0x040000 => "Control",
0x080000 => "Option",
0x100000 => "Command",

(0x80000等于524288.)

(0x80000 equals 524288.)

所以我的任务似乎只是解析defaults read com.apple.symbolichotkeys的输出,从参数字典中获取按键组合,根据键盘布局正确地解释这些组合,并使用这些信息在我的FLTK应用程序中设置回调.

So my task just seems to be to parse the output of defaults read com.apple.symbolichotkeys, get the key combinations from the parameter dictionary, interpret those combinations correctly depending on the keyboard layout and use these information to set the callbacks in my FLTK app.

推荐答案

我现在无法测试答案……但我首先会尝试popen defaults命令,如:

I can't test right now the answer ... but I would first try to popen the defaults command like:

HFILE file;
if (!(file = popen("defaults read NSGlobalDomain NSUserKeyEquivalents", "r")))
{
    return nullptr;    
}
const int MAX_BUF_SIZE = 512;
char temp[MAX_BUF_SIZE+1] = "";
while (fgets(temp, MAX_BUF_SIZE, file) > 0)
{
    printf("%s",temp);
    memset(temp, 0, MAX_BUF_SIZE+1);
}
pclose(file);

在这里,我只是printf它的输出,但是您可能想解析它.

Here I just printf its output but you will likely want to parse it.

这篇关于如何以编程方式获取在系统偏好设置中设置的macOS键盘快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆