macOS 开发 - MASShortcut

news/2024/10/18 3:30:20/

文章目录

    • 关于 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.ppmy.cn/news/1279114.html

相关文章

2023 英特尔On技术创新大会直播 |AI科技创新的引路者

英特尔大会 前言英特尔人工智能英特尔创新技术基于英特尔架构的科学计算总结 前言 英特尔技术创新大会是一个令人激动和启发的盛会。在这次大会上&#xff0c;我有幸观看了许多令人瞩目的科技创新和前沿技术的展示。这些展示不仅展示了英特尔作为科技巨头的实力&#xff0c;更…

单调栈分类、封装和总结

作者推荐 map|动态规划|单调栈|LeetCode975:奇偶跳 通过枚举最小&#xff08;最大&#xff09;值不重复、不遗漏枚举所有子数组 C算法&#xff1a;美丽塔O(n)解法单调栈左右寻找第一个小于maxHeight[i]的left,right&#xff0c;[left,right]直接的高度都是maxHeight[i] 可以…

Mac电脑上soucetree账户更改

在开发公司项目的时候遇到一个问题。soucetree提示需要输入已离职员工-张三的密码。 问题&#xff1a;Mac电脑使用souetree&#xff0c;拉取仓库代码提示需要输入其他员工密码。 解决&#xff1a; Mac电脑 SourceTree去掉之前的账户 1、前往文件路径 /Library/Application Su…

RocketMQ的Docker镜像部署(以及Dashboard的部署、ACL配置)

RocketMQ的Docker镜像部署&#xff08;以及Dashboard、ACL&#xff09; 准备 包含RocketMQ部署&#xff08;NameServer、Broker&#xff09;、Dashboard、ACL拉取镜像 RocketMQ$ docker pull apache/rocketmq:5.1.4Dashboard$ docker pull apacherocketmq/rocketmq-dashboard…

c++学习笔记-提高篇-STL标准模板库3(stack容器、queue容器以及list容器)

目录 Stack容器 一、Stack容器介绍 二、stack常用接口 三、栈的示例 queue&#xff08;队列&#xff09;容器 一、queue容器介绍 二、queue常用接口 三、queue示例 list容器 一、list容器介绍 二、list常用接口及示例 &#xff08;一&#xff09;list构造函数 &am…

Unity 问题 之 ScrollView ,LayoutGroup,ContentSizeFitter 一起使用时,动态变化时无法及时刷新更新适配界面的问题

Unity 问题 之 ScrollView ,LayoutGroup,ContentSizeFitter 一起使用时&#xff0c;动态变化时无法及时刷新更新适配界面的问题 目录 Unity 问题 之 ScrollView ,LayoutGroup,ContentSizeFitter 一起使用时&#xff0c;动态变化时无法及时刷新更新适配界面的问题 一、简单介绍…

基于Netty构建Websocket服务端

除了构建TCP和UDP服务器和客户端&#xff0c;Netty还可以用于构建WebSocket服务器。WebSocket是一种基于TCP协议的双向通信协议&#xff0c;可以在Web浏览器和Web服务器之间建立实时通信通道。下面是一个简单的示例&#xff0c;演示如何使用Netty构建一个WebSocket服务器。 项目…

扫码展示多视频怎么做?视频的活码制作技巧

现在扫码看视频的应用场景越来越多&#xff0c;用这种方式不仅能够简单有效的低成本完成视频传播&#xff0c;而且也符合用户的习惯。那么当需要将视频制作二维码来展示内容时&#xff0c;多个视频文件生成二维码的制作方法是怎么操作的呢&#xff1f;下面教大家使用视频二维码…