macOS 开发 - MASShortcut

文章目录


macOS 开发交流 秋秋群:644096295,V : ez-code


关于 MASShortcut

MASShortcut 是一款快捷键管理工具,替代和兼容 ShortcutRecorder



项目结构

  • model
    • MASKeyCodes : 功能键和快捷键 枚举
    • MASShortcut : 管理 key 的组合(不管是否可用)
    • MASShortcutValidator 验证shortcut 是否可用
  • Monitoring
    • MASHotKey : 初始化和注册 MASShortcut
    • MASShortcutMonitor : 注册/解除/检测 MASShortcut,监控更新
  • User Defaults Storage
    • MASDictionaryTransformer : 转换 shortcut 数据格式,以便于存储
    • MASShortcutBinder : 绑定用户操作和 shortcut
  • UI
    • MASLocalization : 替代 NSLocalizedString,以便退出 app 时,能从 framework 种读取字符串。
    • MASShortcutView : Shortcut 视图图
    • MASShortcutViewButtonCell : MASShortcutView 内部的 cell 样式,可调整和重绘
    • MASShortcutView+Bindings : 设置关联的默认键

快速使用

单独使用一个 MASShortcutView

你可以修改 它的属性,来改变显示的样式

c 复制代码
    MASShortcutView *shortCutView = [[MASShortcutView alloc] initWithFrame:NSMakeRect(10, 10, 200, 50)];
    [self.window.contentView addSubview:shortCutView];
    
    shortCutView.wantsLayer = YES;
    shortCutView.layer.backgroundColor = [NSColor blueColor].CGColor;

源码学习

检测是否有热键冲突

MASShortcutValidator.m

c 复制代码
- (BOOL) isShortcutAlreadyTakenBySystem:(MASShortcut *)shortcut explanation: (NSString**) explanation
{
    CFArrayRef globalHotKeys;
    if (CopySymbolicHotKeys(&globalHotKeys) == noErr) {

        // Enumerate all global hotkeys and check if any of them matches current shortcut
        for (CFIndex i = 0, count = CFArrayGetCount(globalHotKeys); i < count; i++) {
            
            CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(globalHotKeys, i);
            
            CFNumberRef code = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
            CFNumberRef flags = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
            CFNumberRef enabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);

            if (([(__bridge NSNumber *)code integerValue] == [shortcut keyCode]) &&
                ([(__bridge NSNumber *)flags unsignedIntegerValue] == [shortcut carbonFlags]) &&
                ([(__bridge NSNumber *)enabled boolValue])) {

                if (explanation) {
                    *explanation = MASLocalizedString(@"This combination cannot be used because it is already used by a system-wide "
                                                     @"keyboard shortcut.\nIf you really want to use this key combination, most shortcuts "
                                                     @"can be changed in the Keyboard & Mouse panel in System Preferences.",
                                                     @"Message for alert when shortcut is already used by the system");
                }
                return YES;
            }
        }
        CFRelease(globalHotKeys);
    }
    return [self isShortcut:shortcut alreadyTakenInMenu:[NSApp mainMenu] explanation:explanation];
}
  • CopySymbolicHotKeys 来自 Carbon -- HiToolbox -- CarbonEvents.h

处理 Event

c 复制代码
- (void) handleEvent: (EventRef) event
{
    if (GetEventClass(event) != kEventClassKeyboard) {
        return;
    }

    EventHotKeyID hotKeyID;
    OSStatus status = GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
    if (status != noErr || hotKeyID.signature != MASHotKeySignature) {
        return;
    }

    [_hotKeys enumerateKeysAndObjectsUsingBlock:^(MASShortcut *shortcut, MASHotKey *hotKey, BOOL *stop) {
        if (hotKeyID.id == [hotKey carbonID]) {
            if ([hotKey action]) {
                dispatch_async(dispatch_get_main_queue(), [hotKey action]);
            }
            *stop = YES;
        }
    }];
}

https://music.163.com/#/song?id=865632948

伊织 2023-12-22

相关推荐
乌恩大侠2 小时前
【Xcode Command Line Tools】安装指南
macos·fpga开发·c
GEEKVIP4 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
GEEKVIP4 小时前
如何在 Windows 10 上恢复未保存/删除的 Word 文档
macos·ios·智能手机·电脑·word·笔记本电脑·iphone
奇客软件5 小时前
iPhone使用技巧:如何恢复变砖的 iPhone 或 iPad
数码相机·macos·ios·电脑·笔记本电脑·iphone·ipad
前端张三6 小时前
Mac 电脑pink 后端ip地址进行本地联调
服务器·tcp/ip·macos
缘友一世12 小时前
macos安装mongodb
数据库·mongodb·macos
笑非不退20 小时前
macOS开发环境配置与应用开发
macos
colorknight1 天前
1.2.3 HuggingFists安装说明-MacOS安装
人工智能·低代码·macos·huggingface·数据科学·ai agent
GEEKVIP1 天前
如何修复变砖的手机并恢复丢失的数据
macos·ios·智能手机·word·手机·笔记本电脑·iphone
kuai-1 天前
MacOS配置python环境
开发语言·python·macos