MASShortcut实战案例:Demo项目源码深度剖析

【免费下载链接】MASShortcut Modern framework for managing global keyboard shortcuts compatible with Mac App Store. More details: 【免费下载链接】MASShortcut 项目地址: https://gitcode.com/gh_mirrors/mas/MASShortcut

MASShortcut是一个为Mac应用提供全局键盘快捷键管理的现代框架,特别适合Mac App Store应用使用。本文将通过深入剖析Demo项目的核心源码,展示如何快速实现专业级的快捷键功能。

项目结构概览

Demo项目作为MASShortcut框架的实践示例,采用了清晰的模块化结构:

  • 核心文件Demo/AppDelegate.hDemo/AppDelegate.m 实现了应用的主逻辑
  • 界面组件Framework/UI/MASShortcutView.h 提供了可视化的快捷键录制组件
  • 资源文件:多语言支持文件位于 Demo/[语言].lproj/Localizable.strings

核心功能实现

1. 快捷键初始化与默认设置

Demo/AppDelegate.m中,应用启动时会初始化默认快捷键设置:

// 设置默认快捷键为 Command+F1
MASShortcut *firstLaunchShortcut = [MASShortcut shortcutWithKeyCode:kVK_F1 modifierFlags:NSEventModifierFlagCommand];
NSData *firstLaunchShortcutData = [NSKeyedArchiver archivedDataWithRootObject:firstLaunchShortcut];

// 注册用户默认值
[defaults registerDefaults:@{
    MASHardcodedShortcutEnabledKey : @YES,
    MASCustomShortcutEnabledKey : @YES,
    MASCustomShortcutKey : firstLaunchShortcutData
}];

这段代码展示了如何为应用设置初始快捷键,确保用户首次启动时就有可用的快捷键配置。

2. 快捷键视图绑定

Demo项目使用MASShortcut提供的MASShortcutView组件实现可视化快捷键录制:

// 将快捷键录制视图绑定到用户默认设置
[_customShortcutView setAssociatedUserDefaultsKey:MASCustomShortcutKey];

// 根据复选框状态启用或禁用录制视图
[_customShortcutView bind:@"enabled" toObject:defaults
    withKeyPath:MASCustomShortcutEnabledKey options:nil];

MASShortcutView支持多种显示样式,定义在Framework/UI/MASShortcutView.h中:

typedef NS_ENUM(NSInteger, MASShortcutViewStyle) {
    MASShortcutViewStyleDefault = 0,  // 高度 = 19 px
    MASShortcutViewStyleTexturedRect, // 高度 = 25 px
    MASShortcutViewStyleRounded,      // 高度 = 43 px
    MASShortcutViewStyleFlat,
    MASShortcutViewStyleRegularSquare
};

3. 快捷键监听与响应

Demo项目实现了两种快捷键绑定方式:

1. 通过用户默认设置绑定

- (void) setCustomShortcutEnabled: (BOOL) enabled
{
    if (enabled) {
        [[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:MASCustomShortcutKey toAction:^{
            [self playShortcutFeedback];
        }];
    } else {
        [[MASShortcutBinder sharedBinder] breakBindingWithDefaultsKey:MASCustomShortcutKey];
    }
}

2. 硬编码快捷键注册

- (void) setHardcodedShortcutEnabled: (BOOL) enabled
{
    MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_F2 modifierFlags:NSEventModifierFlagCommand];
    if (enabled) {
        [[MASShortcutMonitor sharedMonitor] registerShortcut:shortcut withAction:^{
            [self playShortcutFeedback];
        }];
    } else {
        [[MASShortcutMonitor sharedMonitor] unregisterShortcut:shortcut];
    }
}

这两种方式分别适用于用户可配置的快捷键和应用固定的功能快捷键。

4. 用户界面主题适配

Demo项目还展示了如何适配不同的macOS视觉主题:

- (IBAction) setAppearance: (id) sender
{
    NSInteger tag = popUpButton.selectedTag;
    switch (tag) {
        case 0: // 继承系统外观
            self.visualEffectView.appearance = nil;
            break;
        case 1: // 亮色主题
            self.visualEffectView.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantLight];
            break;
        case 2: // 暗色主题
            self.visualEffectView.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
            break;
        // 其他主题...
    }
}

实用功能解析

快捷键反馈机制

Demo实现了简洁的快捷键触发反馈:

- (void)playShortcutFeedback
{
    [[NSSound soundNamed:@"Ping"] play];
    [self.feedbackTextField setStringValue:NSLocalizedString(@"Shortcut pressed!", @"快捷键按下时显示的反馈")];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.feedbackTextField setStringValue:@""];
    });
}

这种即时反馈机制能提升用户体验,让用户明确知道快捷键已被触发。

用户偏好设置观察

通过KVO机制监听用户设置变化:

// 观察用户默认设置中的复选框状态变化
[defaults addObserver:self forKeyPath:MASCustomShortcutEnabledKey
    options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew
    context:MASObservingContext];

当用户修改设置时,应用能即时更新快捷键状态,无需重启。

总结与扩展

通过Demo项目的源码分析,我们看到MASShortcut框架提供了:

  • 简洁的API设计,降低快捷键管理复杂度
  • 灵活的快捷键绑定机制,支持用户自定义和应用固定快捷键
  • 美观的可视化组件,提升用户体验
  • 完整的多语言支持,位于Framework/Resources/[语言].lproj/Localizable.strings

要开始使用MASShortcut,可以通过以下命令克隆仓库:

git clone https://gitcode.com/gh_mirrors/mas/MASShortcut

MASShortcut框架特别适合需要实现全局快捷键功能的Mac应用,其设计符合Mac App Store的要求,同时提供了丰富的自定义选项,帮助开发者快速集成专业级的快捷键功能。

【免费下载链接】MASShortcut Modern framework for managing global keyboard shortcuts compatible with Mac App Store. More details: 【免费下载链接】MASShortcut 项目地址: https://gitcode.com/gh_mirrors/mas/MASShortcut

Logo

立足具身智能前沿赛道,致力于搭建全球化、开源化、全栈式技术交流与实践共创平台。

更多推荐