blob: 7aff3d11dd7630050a1e9b5b01b73708a117a96f (
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
|
#import "AppDelegate.h"
NSString *const MASPreferenceKeyShortcut = @"MASDemoShortcut";
NSString *const MASPreferenceKeyShortcutEnabled = @"MASDemoShortcutEnabled";
NSString *const MASPreferenceKeyConstantShortcutEnabled = @"MASDemoConstantShortcutEnabled";
@implementation AppDelegate
#pragma mark -
- (void)awakeFromNib
{
[super awakeFromNib];
// 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 setAssociatedUserDefaultsKey:MASPreferenceKeyShortcut];
// 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) {
[[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:MASPreferenceKeyShortcut toAction:^{
[[NSSound soundNamed:@"Ping"] play];
}];
} else {
[[MASShortcutBinder sharedBinder] 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_ANSI_Keypad2 modifierFlags:NSCommandKeyMask];
if (self.constantShortcutEnabled) {
[[MASShortcutMonitor sharedMonitor] registerShortcut:shortcut withAction:^{
[[NSSound soundNamed:@"Ping"] play];
}];
} else {
[[MASShortcutMonitor sharedMonitor] unregisterShortcut:shortcut];
}
}
@end
|