MacOS (OSX) 听写 - 有没有办法读取分配给 Mac 中的听写功能的快捷键?或以编程方式开始听写?

MacOS (OSX) Dictation - Is there a way to read the shortcut keys assigned to dictation feature in Mac? or programmatically start dictation?

我想在任何键盘按键按下时启用听写。

目前我正在使用 AppleScript 点击最前面应用程序的菜单->编辑->开始听写。 此脚本需要自动化权限,我正在犹豫是否要创建具有这么多权限的应用程序。

有没有办法获得分配给听写功能的快捷键?

目标是:

  1. 从首选项文件中读取快捷键
  2. 从应用 (CGEVENTPOST) 触发这些键

我最终实现了自己。捕获修改任何键盘设置后对 Preferences 文件夹所做的更改。

遇到一个名为 com.apple.symbolichotkeys.plist 的文件,该文件会在修改任何快捷方式时更改。

这是实现以编程方式启用听写的示例代码(希望我的代码是不言自明的)

#include <Carbon/Carbon.h>

[self enableDication];

-(void) enableDication
{
    int press_count = 2;

    NSString *pathSymbHotKeys = @"~/Library/Preferences/com.apple.symbolichotkeys.plist";
    NSMutableDictionary *symbolicHotKeysDict = [[NSMutableDictionary alloc]initWithContentsOfFile:pathSymbHotKeys.stringByExpandingTildeInPath];
    
    NSNumber *enabled = symbolicHotKeysDict[@"AppleSymbolicHotKeys"][@"164"][@"enabled"] ;
    if (![enabled boolValue]) {
        NSLog(@"Dication shortcut not enabled");
        return;
    }
    
    NSString *type = symbolicHotKeysDict[@"AppleSymbolicHotKeys"][@"164"][@"value"][@"type"];
    
    NSNumber *param_key_code  = nil;
    NSNumber *param_mask = nil;
    
    if ([type isEqualToString:@"standard"]) {
        //mask
        param_mask = symbolicHotKeysDict[@"AppleSymbolicHotKeys"][@"164"][@"value"][@"parameters"][2];
        param_key_code = symbolicHotKeysDict[@"AppleSymbolicHotKeys"][@"164"][@"value"][@"parameters"][1];
        press_count = 1;
    }
    else {
        param_key_code = symbolicHotKeysDict[@"AppleSymbolicHotKeys"][@"164"][@"value"][@"parameters"][0];
    }

    CGEventRef fnDown = CGEventCreateKeyboardEvent(NULL, [self keyCode:param_key_code type:type], true);
    if ([type isEqualToString:@"standard"]) {
        CGEventSetFlags(fnDown, (CGEventFlags)([param_mask intValue] | CGEventGetFlags(fnDown)));
    }

    CGEventRef fnUp   = CGEventCreateKeyboardEvent(NULL, [self keyCode:param_key_code type:type], false);
    if ([type isEqualToString:@"standard"]) {
        CGEventSetFlags(fnUp, (CGEventFlags)([param_mask intValue]));
    }
    
    for (int i = 0; i <press_count; i++)
    {
        CGEventPost(kCGSessionEventTap, fnDown);
        CGEventPost(kCGSessionEventTap, fnUp);
    }

    CFRelease (fnDown);
    CFRelease (fnUp);
}


-(CGKeyCode) keyCode:(NSNumber *)code type:(NSString *)type
{
    if ([type isEqualToString:@"standard"]) {
        return (CGKeyCode)[code longValue];
    }
    else{
        //Default Modifier shortcuts provided by Mac for dictation
        if (([code longValue] & 0x00100008) == 0x00100008) {
            //Left command key
            return kVK_Command;
        }
        else if (([code longValue] & 0x00100010) == 0x00100010) {
            // right command key
            return kVK_RightCommand;
        }
        else if (([code longValue] & kCGEventFlagMaskCommand) == kCGEventFlagMaskCommand) {
            //any command key
            return kVK_Command;
        }
        else if (([code longValue] & NSEventModifierFlagFunction) == NSEventModifierFlagFunction)
        {
            //function key
            return kVK_Function;
        }
        else if (([code longValue] & kCGEventFlagMaskControl) == kCGEventFlagMaskControl)
        {
            //control key
            return kVK_Control;
        }
    }
    return -1;
}