blob: d2c07badb4ee7e8b6bdda6514bcc4152ab847a2c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#import "AppDelegate.h"
NSString *const MASPreferenceKeyShortcut = @"MASDemoShortcut";
NSString *const MASPreferenceKeyShortcutEnabled = @"MASDemoShortcutEnabled";
NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShortcutEnabled";
@implementation AppDelegate {
MASShortcutMonitor *_shortcutMonitor;
MASShortcutBinder *_shortcutBinder;
}
#pragma mark -
- (void)awakeFromNib
{
[super awakeFromNib];
_shortcutBinder = [[MASShortcutBinder alloc] init];
_shortcutMonitor = [[MASShortcutMonitor alloc] init];
[_shortcutBinder setShortcutMonitor:_shortcutMonitor];
// Checkbox will enable and disable the shortcut view
[self.shortcutView bind:@"enabled" toObject:self withKeyPath:@"shortcutEnabled" options:nil];
}
#pragma mark NSApplicationDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Shortcut view will follow and modify user preferences automatically
[_shortcutView bind:MASShortcutBinding
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[@"values." stringByAppendingString:MASPreferenceKeyShortcut]
options:@{NSValueTransformerNameBindingOption:NSKeyedUnarchiveFromDataTransformerName}];
// Activate the global keyboard shortcut if it was enabled last time
[self resetShortcutRegistration];
// Activate the shortcut Command-F1 if it was enabled
[self resetConstantShortcutRegistration];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
#pragma mark - Custom shortcut
- (BOOL)isShortcutEnabled
{
return [[NSUserDefaults standardUserDefaults] boolForKey:MASPreferenceKeyShortcutEnabled];
}
- (void)setShortcutEnabled:(BOOL)enabled
{
if (self.shortcutEnabled != enabled) {
[[NSUserDefaults standardUserDefaults] setBool:enabled forKey:MASPreferenceKeyShortcutEnabled];
[self resetShortcutRegistration];
}
}
- (void)resetShortcutRegistration
{
if (self.shortcutEnabled) {
[_shortcutBinder bindShortcutWithDefaultsKey:MASPreferenceKeyShortcut toAction:^{
[[NSAlert alertWithMessageText:NSLocalizedString(@"Global hotkey has been pressed.", @"Alert message for custom shortcut")
defaultButton:NSLocalizedString(@"OK", @"Default button for the alert on custom shortcut")
alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal];
}];
}
else {
[_shortcutBinder breakBindingWithDefaultsKey:MASPreferenceKeyShortcut];
}
}
#pragma mark - Constant shortcut
- (BOOL)isConstantShortcutEnabled
{
return [[NSUserDefaults standardUserDefaults] boolForKey:MASPreferenceKeyConstantShortcutEnabled];
}
- (void)setConstantShortcutEnabled:(BOOL)enabled
{
if (self.constantShortcutEnabled != enabled) {
[[NSUserDefaults standardUserDefaults] setBool:enabled forKey:MASPreferenceKeyConstantShortcutEnabled];
[self resetConstantShortcutRegistration];
}
}
- (void)resetConstantShortcutRegistration
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_F2 modifierFlags:NSCommandKeyMask];
if (self.constantShortcutEnabled) {
[_shortcutMonitor registerShortcut:shortcut withAction:^{
[[NSAlert alertWithMessageText:NSLocalizedString(@"⌘F2 has been pressed.", @"Alert message for constant shortcut")
defaultButton:NSLocalizedString(@"OK", @"Default button for the alert on constant shortcut")
alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal];
}];
}
else {
[_shortcutMonitor unregisterShortcut:shortcut];
}
}
@end
|