当前位置: 首页 > news >正文

macOS 开发 - MASShortcut

文章目录

    • 关于 MASShortcut
      • 项目结构
    • 快速使用
    • 源码学习
      • 检测是否有热键冲突
      • 处理 Event


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


关于 MASShortcut

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

  • github : https://github.com/cocoabits/MASShortcut

在这里插入图片描述


项目结构

  • 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
你可以修改 它的属性,来改变显示的样式

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

- (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 shortcutfor (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

- (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

http://www.lryc.cn/news/265278.html

相关文章:

  • 【大数据面试】Flink面试题附答案
  • 语音识别之百度语音试用和OpenAiGPT开源Whisper使用
  • Rust报错:the msvc targets depend on the msvc linker but `link.exe` was not found
  • 2312llvm,04后端上
  • springboot学习笔记(五)
  • 文件上传——后端
  • 虾皮开通:如何在虾皮上开通跨境电商店铺
  • C语言—每日选择题—Day60
  • 【3D生成与重建】SSDNeRF:单阶段Diffusion NeRF的三维生成和重建
  • 计算机网络:应用层
  • 现代雷达车载应用——第3章 MIMO雷达技术 3.2节 汽车MIMO雷达波形正交策略
  • Unresolved plugin: ‘org.apache.maven.plugins‘解决报错
  • 阿里云林立翔:基于阿里云 GPU 的 AIGC 小规模训练优化方案
  • 从0开始学Git指令
  • B039-SpringMVC基础
  • Tomcat报404问题解决方案大全(包括tomcat可以正常运行但是报404)
  • debian10安装配置vim+gtags
  • vue跳转方式
  • 基于ssm+jsp学生综合测评管理系统源码和论文
  • 网络基础篇【网线的制作,OSI七层模型,集线器和交换机的介绍,路由器的介绍与设置】
  • CSRF检测工具(XSRF检测工具)使用说明
  • docker 部署kafka
  • Android 架构 - 组件化
  • 数字图像处理-空间域图像增强-爆肝18小时用通俗语言进行超详细的总结
  • 【Java】【SQL】DATE_FORMAT函数详解
  • Pooling方法总结(语音识别)
  • Java可变参数(学习推荐版,通俗易懂)
  • 异步编程Promise
  • Centos上的默认文本编辑器vi的操作方法积累
  • 海康rtsp拉流,rtmp推流,nginx部署转flv集成