57 Commits

Author SHA1 Message Date
Vadim Shpakovski 8e196a14a0 Merge pull request #45 from pfandrade/master
Implemented initWithCoder: to fix #44
2014-09-17 19:00:06 +03:00
Paulo F. Andrade e53eed24bc Implemented -initWithCoder: in MASShortcutView 2014-09-17 16:50:37 +01:00
Paulo F. Andrade 724376092d Updated MASShortcut 2014-09-17 16:47:37 +01:00
Paulo F. Andrade a5c2e15935 Added support for secure coding 2014-09-17 16:39:48 +01:00
Vadim Shpakovski 0633545a46 Ignoring disabled system shortcuts. 2014-09-02 09:43:53 +03:00
Vadim Shpakovski b30a0b02c4 Merge pull request #43 from darwin/master
allow [MODIFIER]+ESC shortcuts
2014-08-27 12:39:43 +03:00
Antonin Hildebrand 549b3ef29e allow [MODIFIER]+ESC shortcuts
only naked ESC without modifiers should cancel editing

http://discuss.binaryage.com/t/cant-change-activation-key-to-control-esc

tested in TotalTerminal codebase
2014-08-26 13:20:37 +02:00
Vadim Shpakovski f3a8a9a95a Merge pull request #40 from kainjow/master
Fixed compiling with projects that don't use a precompiled header.
2014-07-22 21:14:49 +03:00
Vadim Shpakovski 6e161f95fa Merge pull request #39 from xhacker/podspec
Create MASShortcut.podspec for CocoaPods
2014-07-22 21:12:37 +03:00
Kevin Wojniak 42dfc38ef6 Fixed compiling with projects that don't use a precompiled header. 2014-07-20 15:50:15 -07:00
LIU Dongyuan / 柳东原 dc088a3a77 Create MASShortcut.podspec 2014-07-18 22:45:18 -07:00
Vadim Shpakovski a21ac331ad Merge pull request #35 from ShadowLightz/master
Replace deprecated NSRunCriticalAlertPanel with NSAlert
2014-06-14 15:44:37 +03:00
ShadowLightz 7d3604820e Replace depr. NSRunCriticalAlertPanel with NSAlert
- Now using modern API
- The dialog is now shown as a modal sheet, which suits better
- The alert style is changed to NSWarningAlertStyle,
- NSCriticalAlertStyle was not required
2014-06-14 14:08:03 +02:00
Vadim Shpakovski ea5a30fe6d Merge pull request #32 from iluuu1994/master
Fixed string literal warning
2014-04-06 21:04:36 +03:00
Paulo F. Andrade da27736330 Replaced call to deprecated NSRunCriticalAlertPanel API 2014-04-03 11:25:28 +01:00
Ilija Tovilo 48a311eb90 Fixed string literal warning 2014-03-31 15:27:35 +02:00
Paulo F. Andrade 9c0b4a327c Added support for secure coding 2014-02-17 15:09:41 +00:00
Vadim Shpakovski 28c656d654 Merge pull request #29 from ShadowLightz/master
Silenced compiler warnings, updated README
2014-01-06 06:30:57 -08:00
Jonathan Rahn ce5760d61c update README 2014-01-06 14:08:32 +01:00
Jonathan Rahn f1228d6594 Silence compiler warning "No previous prototype for function..." and added an explicit typecast, update README with a hint to the view's height 2014-01-06 13:59:26 +01:00
Vadim Shpakovski 9edbf670f5 Adding new API for custom drawing. 2013-12-25 18:01:37 +02:00
Vadim Shpakovski b32a19ff7a Adding support for custom drawing. 2013-12-25 18:00:14 +02:00
Vadim Shpakovski fb4ac110a0 Fixing errors in MASShortcutDemo. 2013-07-31 14:26:49 +03:00
Vadim Shpakovski abda36331f Improving README. 2013-06-16 21:20:28 +03:00
Vadim Shpakovski 86443bc53c Adding syntax highlighting to the usage section. 2013-04-16 14:59:09 +03:00
Vadim Shpakovski 278c5fa2a5 Switching to NSUserDefaultsController.
NSUserDefaults does not support KVO officially.
2013-04-16 14:57:23 +03:00
Vadim Shpakovski b3e2e54830 Merge pull request #25 from corybohon/master
Adding a Notification section to the documentation.
2013-04-16 04:45:55 -07:00
Cory Bohon 7321b16163 Update README.md 2013-04-15 23:16:53 -03:00
Cory Bohon 9890a61538 Update README.md 2013-04-15 23:16:20 -03:00
Cory Bohon 75c763f697 Update README.md
Updated the documentation to cover NSUserDefaults observing.
2013-04-15 23:15:32 -03:00
Vadim Shpakovski e5b32d7d47 Fixing the latest changes. 2013-04-15 23:34:46 +03:00
Vadim Shpakovski 8ede004687 Fixing a problem with user defaults. 2013-04-15 23:24:46 +03:00
Vadim Shpakovski fe33039c18 Fixing the possible memory leak. 2013-03-24 11:21:40 +03:00
Vadim Shpakovski ffafb30498 Fixing a bug with resetting user defaults. 2013-03-06 13:53:45 +03:00
Vadim Shpakovski c6131623a7 Adding support for setting default user shortcut in preferences. 2013-03-06 13:27:57 +03:00
Vadim Shpakovski caf0c1e95e Merge pull request #19 from uasi/disambiguate-license
Disambiguate "the BSD license"
2013-02-07 00:42:46 -08:00
Tomoki Aonuma 7f0769adf4 State clearly that the license is the 2-clause BSD
LICENSE is taken from http://en.wikipedia.org/wiki/BSD_licenses
2013-02-07 12:01:35 +09:00
Vadim Shpakovski 20d323b59c Merge pull request #16 from OldFriend/master
Fixed mistypes (double 'used' x2)
2013-02-06 11:44:03 -08:00
Pedro Vieria dfdcd5655e Fixed mistypes (double 'used' x2) 2013-02-06 19:21:57 +00:00
Vadim Shpakovski 42be5135fb Merge pull request #15 from OldFriend/master
Fixed 'shortcut' mistype
2013-02-06 11:03:47 -08:00
Pedro Vieria 0266a4cee2 fixed mistype 2013-02-06 18:56:31 +00:00
Vadim Shpakovski 7300a064ec Update README.md
Adds one more non-ARC repository reference.
2012-12-13 16:45:45 +03:00
Vadim Shpakovski 0f89f3b962 Merge pull request #11 from radex/master
Make F17, F18 and F19 work
2012-12-03 14:24:31 -08:00
Radex 9239e1c98d Make F17-F19 work 2012-12-03 22:53:39 +01:00
Vadim Shpakovski 2c688abec9 Merge branch 'master' of github.com:shpakovski/MASShortcut 2012-11-23 23:46:21 +03:00
Vadim Shpakovski a89afec679 Enables support for exlusive Option modifier. 2012-11-23 23:46:04 +03:00
Vadim Shpakovski c4bc7c135f Updates README
Now there is a link to the non-ARC compatible fork.
2012-11-13 01:05:33 +03:00
Vadim Shpakovski bc56cdc907 Now the component properly handles your trying to set another handler for an existing shortcut.
Only the first assignable handler will be working, the second and other handlers all will be ignored. Also, previous workaround with calling performSelector:withObject:afterDelay: is removed, it seems to be working with an apprpriate use of objc_setAssociatedObject.
2012-11-10 12:34:05 +03:00
Vadim Shpakovski 3d4235f879 Fixes problem with view controllers.
When view controller is loaded from NIB, it posts a notification about changes in user defaults. This causes user defaults watcher to reload the hotkey. Some shortcut objects perform cleanup in -[dealloc], but this method may be called later then -[init] in new shortcut objects. It resulted in freeing just created keyboard shortcut referring to the same user defaults key. The problem has been fixed by moving creation of the replacement hotkey in the next run loop when -[dealloc] is guaranteed to be called in previous watcher.
2012-10-08 13:34:34 +03:00
Vadim Shpakovski 4099b62587 Isolates monitoring code from data. 2012-09-19 12:43:39 +03:00
Vadim Shpakovski 243aee3038 Adds support for direct monitoring shortcuts. 2012-09-19 12:36:53 +03:00
Vadim Shpakovski 723eeb4346 Improves API. 2012-09-19 08:57:26 +03:00
Vadim Shpakovski b1f90fa096 Merge. 2012-09-19 01:50:15 +03:00
Vadim Shpakovski 08717ff81e Merge remote-tracking branch 'origin/master' 2012-09-19 01:49:24 +03:00
Vadim Shpakovski da2e1af574 Adds a new appearance and fixes bugs related to key equivalents. 2012-09-19 01:44:49 +03:00
Vadim Shpakovski dddb527bfb Fixes the issue with setting associated User Defaults key twice. 2012-09-19 00:56:51 +03:00
Vadim Shpakovski e01e9075e5 Improves textured view appearance. 2012-09-03 16:23:21 +03:00
12 changed files with 483 additions and 146 deletions
+22
View File
@@ -0,0 +1,22 @@
Copyright (c) 2012-2013, Vadim Shpakovski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+8
View File
@@ -0,0 +1,8 @@
#import "MASShortcut.h"
@interface MASShortcut (Monitoring)
+ (id)addGlobalHotkeyMonitorWithShortcut:(MASShortcut *)shortcut handler:(void (^)())handler;
+ (void)removeGlobalHotkeyMonitor:(id)monitor;
@end
+165
View File
@@ -0,0 +1,165 @@
#import "MASShortcut+Monitoring.h"
NSMutableDictionary *MASRegisteredHotKeys(void);
BOOL InstallCommonEventHandler(void);
BOOL InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, EventHotKeyRef *outCarbonHotKey);
void UninstallEventHandler(void);
#pragma mark -
@interface MASShortcutHotKey : NSObject
@property (nonatomic, readonly) MASShortcut *shortcut;
@property (nonatomic, readonly, copy) void (^handler)();
@property (nonatomic, readonly) EventHotKeyRef carbonHotKey;
@property (nonatomic, readonly) UInt32 carbonHotKeyID;
- (id)initWithShortcut:(MASShortcut *)shortcut handler:(void (^)())handler;
@end
#pragma mark -
@implementation MASShortcut (Monitoring)
+ (id)addGlobalHotkeyMonitorWithShortcut:(MASShortcut *)shortcut handler:(void (^)())handler
{
NSString *monitor = [NSString stringWithFormat:@"%@", shortcut.description];
if ([MASRegisteredHotKeys() objectForKey:monitor]) return nil;
MASShortcutHotKey *hotKey = [[MASShortcutHotKey alloc] initWithShortcut:shortcut handler:handler];
if (hotKey == nil) return nil;
[MASRegisteredHotKeys() setObject:hotKey forKey:monitor];
return monitor;
}
+ (void)removeGlobalHotkeyMonitor:(id)monitor
{
if (monitor == nil) return;
NSMutableDictionary *registeredHotKeys = MASRegisteredHotKeys();
[registeredHotKeys removeObjectForKey:monitor];
if (registeredHotKeys.count == 0) {
UninstallEventHandler();
}
}
@end
#pragma mark -
@implementation MASShortcutHotKey
@synthesize carbonHotKeyID = _carbonHotKeyID;
@synthesize handler = _handler;
@synthesize shortcut = _shortcut;
@synthesize carbonHotKey = _carbonHotKey;
#pragma mark -
- (id)initWithShortcut:(MASShortcut *)shortcut handler:(void (^)())handler
{
self = [super init];
if (self) {
_shortcut = shortcut;
_handler = [handler copy];
if (!InstallHotkeyWithShortcut(shortcut, &_carbonHotKeyID, &_carbonHotKey))
self = nil;
}
return self;
}
- (void)dealloc
{
[self uninstallExisitingHotKey];
}
- (void)uninstallExisitingHotKey
{
if (_carbonHotKey) {
UnregisterEventHotKey(_carbonHotKey);
_carbonHotKey = NULL;
}
}
@end
#pragma mark - Carbon magic
NSMutableDictionary *MASRegisteredHotKeys()
{
static NSMutableDictionary *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [NSMutableDictionary dictionary];
});
return shared;
}
#pragma mark -
FourCharCode const kMASShortcutSignature = 'MASS';
BOOL InstallHotkeyWithShortcut(MASShortcut *shortcut, UInt32 *outCarbonHotKeyID, EventHotKeyRef *outCarbonHotKey)
{
if ((shortcut == nil) || !InstallCommonEventHandler()) return NO;
static UInt32 sCarbonHotKeyID = 0;
EventHotKeyID hotKeyID = { .signature = kMASShortcutSignature, .id = ++ sCarbonHotKeyID };
EventHotKeyRef carbonHotKey = NULL;
if (RegisterEventHotKey(shortcut.carbonKeyCode, shortcut.carbonFlags, hotKeyID, GetEventDispatcherTarget(), kEventHotKeyExclusive, &carbonHotKey) != noErr) {
return NO;
}
if (outCarbonHotKeyID) *outCarbonHotKeyID = hotKeyID.id;
if (outCarbonHotKey) *outCarbonHotKey = carbonHotKey;
return YES;
}
static OSStatus CarbonCallback(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
if (GetEventClass(inEvent) != kEventClassKeyboard) return noErr;
EventHotKeyID hotKeyID;
OSStatus status = GetEventParameter(inEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
if (status != noErr) return status;
if (hotKeyID.signature != kMASShortcutSignature) return noErr;
[MASRegisteredHotKeys() enumerateKeysAndObjectsUsingBlock:^(id key, MASShortcutHotKey *hotKey, BOOL *stop) {
if (hotKeyID.id == hotKey.carbonHotKeyID) {
if (hotKey.handler) {
hotKey.handler();
}
*stop = YES;
}
}];
return noErr;
}
#pragma mark -
static EventHandlerRef sEventHandler = NULL;
BOOL InstallCommonEventHandler()
{
if (sEventHandler == NULL) {
EventTypeSpec hotKeyPressedSpec = { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyPressed };
OSStatus status = InstallEventHandler(GetEventDispatcherTarget(), CarbonCallback, 1, &hotKeyPressedSpec, NULL, &sEventHandler);
if (status != noErr) {
sEventHandler = NULL;
return NO;
}
}
return YES;
}
void UninstallEventHandler()
{
if (sEventHandler) {
RemoveEventHandler(sEventHandler);
sEventHandler = NULL;
}
}
+1 -1
View File
@@ -4,6 +4,6 @@
+ (void)registerGlobalShortcutWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler;
+ (void)unregisterGlobalShortcutWithUserDefaultsKey:(NSString *)userDefaultsKey;
+ (void)setGlobalShortcut:(MASShortcut *)shortcut forUserDefaultsKey:(NSString *)userDefaultsKey;
@end
+36 -90
View File
@@ -1,23 +1,21 @@
#import "MASShortcut+UserDefaults.h"
#import "MASShortcut+Monitoring.h"
@interface MASShortcutHotKey : NSObject
@interface MASShortcutUserDefaultsHotKey : NSObject
@property (nonatomic, readonly) NSString *userDefaultsKey;
@property (nonatomic, readonly, copy) void (^handler)();
@property (nonatomic, readonly) EventHotKeyRef carbonHotKey;
@property (nonatomic, readonly) UInt32 carbonHotKeyID;
@property (nonatomic, copy) void (^handler)();
@property (nonatomic, weak) id monitor;
- (id)initWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler;
+ (void)uninstallEventHandler;
@end
#pragma mark -
@implementation MASShortcut (UserDefaults)
+ (NSMutableDictionary *)registeredHotKeys
+ (NSMutableDictionary *)registeredUserDefaultsHotKeys
{
static NSMutableDictionary *shared = nil;
static dispatch_once_t onceToken;
@@ -29,130 +27,78 @@
+ (void)registerGlobalShortcutWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler;
{
MASShortcutHotKey *hotKey = [[MASShortcutHotKey alloc] initWithUserDefaultsKey:userDefaultsKey handler:handler];
[[self registeredHotKeys] setObject:hotKey forKey:userDefaultsKey];
MASShortcutUserDefaultsHotKey *hotKey = [[MASShortcutUserDefaultsHotKey alloc] initWithUserDefaultsKey:userDefaultsKey handler:handler];
[[self registeredUserDefaultsHotKeys] setObject:hotKey forKey:userDefaultsKey];
}
+ (void)unregisterGlobalShortcutWithUserDefaultsKey:(NSString *)userDefaultsKey
{
NSMutableDictionary *registeredHotKeys = [self registeredHotKeys];
NSMutableDictionary *registeredHotKeys = [self registeredUserDefaultsHotKeys];
[registeredHotKeys removeObjectForKey:userDefaultsKey];
if (registeredHotKeys.count == 0) {
[MASShortcutHotKey uninstallEventHandler];
}
}
+ (void)setGlobalShortcut:(MASShortcut *)shortcut forUserDefaultsKey:(NSString *)userDefaultsKey
{
NSData *shortcutData = shortcut.data;
if (shortcutData)
[[NSUserDefaults standardUserDefaults] setObject:shortcutData forKey:userDefaultsKey];
else
[[NSUserDefaults standardUserDefaults] removeObjectForKey:userDefaultsKey];
}
@end
#pragma mark -
@implementation MASShortcutHotKey
@implementation MASShortcutUserDefaultsHotKey {
NSString *_observableKeyPath;
}
@synthesize carbonHotKeyID = _carbonHotKeyID;
@synthesize monitor = _monitor;
@synthesize handler = _handler;
@synthesize userDefaultsKey = _userDefaultsKey;
@synthesize carbonHotKey = _carbonHotKey;
#pragma mark -
void *MASShortcutUserDefaultsContext = &MASShortcutUserDefaultsContext;
- (id)initWithUserDefaultsKey:(NSString *)userDefaultsKey handler:(void (^)())handler
{
self = [super init];
if (self) {
_userDefaultsKey = userDefaultsKey.copy;
_handler = [handler copy];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:)
name:NSUserDefaultsDidChangeNotification object:[NSUserDefaults standardUserDefaults]];
[self installHotKeyFromUserDefaults];
_observableKeyPath = [@"values." stringByAppendingString:_userDefaultsKey];
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:_observableKeyPath options:NSKeyValueObservingOptionInitial context:MASShortcutUserDefaultsContext];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:[NSUserDefaults standardUserDefaults]];
[self uninstallExisitingHotKey];
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:_observableKeyPath context:MASShortcutUserDefaultsContext];
[MASShortcut removeGlobalHotkeyMonitor:self.monitor];
}
#pragma mark -
- (void)userDefaultsDidChange:(NSNotification *)note
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self uninstallExisitingHotKey];
[self installHotKeyFromUserDefaults];
}
#pragma mark - Carbon events
static EventHandlerRef sEventHandler = NULL;
+ (BOOL)installCommonEventHandler
{
if (sEventHandler == NULL) {
EventTypeSpec hotKeyPressedSpec = { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyPressed };
OSStatus status = InstallEventHandler(GetEventDispatcherTarget(), CarbonCallback, 1, &hotKeyPressedSpec, NULL, &sEventHandler);
if (status != noErr) {
sEventHandler = NULL;
return NO;
}
if (context == MASShortcutUserDefaultsContext) {
[MASShortcut removeGlobalHotkeyMonitor:self.monitor];
[self installHotKeyFromUserDefaults];
}
return YES;
}
+ (void)uninstallEventHandler
{
if (sEventHandler) {
RemoveEventHandler(sEventHandler);
sEventHandler = NULL;
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark -
- (void)uninstallExisitingHotKey
{
if (_carbonHotKey) {
UnregisterEventHotKey(_carbonHotKey);
_carbonHotKey = NULL;
}
}
FourCharCode const kMASShortcutSignature = 'MASS';
- (void)installHotKeyFromUserDefaults
{
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:_userDefaultsKey];
MASShortcut *shortcut = [MASShortcut shortcutWithData:data];
if ((shortcut == nil) || ![[self class] installCommonEventHandler]) return;
static UInt32 sCarbonHotKeyID = 0;
_carbonHotKeyID = ++ sCarbonHotKeyID;
EventHotKeyID hotKeyID = { .signature = kMASShortcutSignature, .id = _carbonHotKeyID };
if (RegisterEventHotKey(shortcut.carbonKeyCode, shortcut.carbonFlags, hotKeyID, GetEventDispatcherTarget(), kEventHotKeyExclusive, &_carbonHotKey) != noErr) {
_carbonHotKey = NULL;
}
if (shortcut == nil) return;
self.monitor = [MASShortcut addGlobalHotkeyMonitorWithShortcut:shortcut handler:self.handler];
}
static OSStatus CarbonCallback(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
if (GetEventClass(inEvent) != kEventClassKeyboard) return noErr;
EventHotKeyID hotKeyID;
OSStatus status = GetEventParameter(inEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID);
if (status != noErr) return status;
if (hotKeyID.signature != kMASShortcutSignature) return noErr;
[[MASShortcut registeredHotKeys] enumerateKeysAndObjectsUsingBlock:^(id key, MASShortcutHotKey *hotKey, BOOL *stop) {
if (hotKeyID.id == hotKey.carbonHotKeyID) {
if (hotKey.handler) {
hotKey.handler();
}
*stop = YES;
}
}];
return noErr;
}
@end
@end
+8 -1
View File
@@ -1,4 +1,5 @@
#import <Carbon/Carbon.h>
#import <AppKit/AppKit.h>
#define MASShortcutChar(char) [NSString stringWithFormat:@"%C", (unsigned short)(char)]
#define MASShortcutClear(flags) (flags & (NSControlKeyMask | NSShiftKeyMask | NSAlternateKeyMask | NSCommandKeyMask))
@@ -30,13 +31,14 @@ enum {
kMASShortcutGlyphSoutheastArrow = 0x2198,
} MASShortcutGlyph;
@interface MASShortcut : NSObject <NSCoding>
@interface MASShortcut : NSObject <NSSecureCoding>
@property (nonatomic) NSUInteger keyCode;
@property (nonatomic) NSUInteger modifierFlags;
@property (nonatomic, readonly) UInt32 carbonKeyCode;
@property (nonatomic, readonly) UInt32 carbonFlags;
@property (nonatomic, readonly) NSString *keyCodeString;
@property (nonatomic, readonly) NSString *keyCodeStringForKeyEquivalent;
@property (nonatomic, readonly) NSString *modifierFlagsString;
@property (nonatomic, readonly) NSData *data;
@property (nonatomic, readonly) BOOL shouldBypass;
@@ -50,4 +52,9 @@ enum {
- (BOOL)isTakenError:(NSError **)error;
// The following API enable hotkeys with the Option key as the only modifier
// For example, Option-G will not generate © and Option-R will not paste ®
+ (void)setAllowsAnyHotkeyWithOptionModifier:(BOOL)allow;
+ (BOOL)allowsAnyHotkeyWithOptionModifier;
@end
+106 -25
View File
@@ -1,7 +1,7 @@
#import "MASShortcut.h"
NSString *const kMASShortcutKeyCode = @"KeyCode";
NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
NSString *const MASShortcutKeyCode = @"KeyCode";
NSString *const MASShortcutModifierFlags = @"ModifierFlags";
@implementation MASShortcut {
NSUInteger _keyCode; // NSNotFound if empty
@@ -13,19 +13,24 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
#pragma mark -
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInteger:(self.keyCode != NSNotFound ? (NSInteger)self.keyCode : - 1) forKey:kMASShortcutKeyCode];
[coder encodeInteger:(NSInteger)self.modifierFlags forKey:kMASShortcutModifierFlags];
[coder encodeInteger:(self.keyCode != NSNotFound ? (NSInteger)self.keyCode : - 1) forKey:MASShortcutKeyCode];
[coder encodeInteger:(NSInteger)self.modifierFlags forKey:MASShortcutModifierFlags];
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self) {
NSInteger code = [decoder decodeIntegerForKey:kMASShortcutKeyCode];
NSInteger code = [decoder decodeIntegerForKey:MASShortcutKeyCode];
self.keyCode = (code < 0 ? NSNotFound : (NSUInteger)code);
self.modifierFlags = [decoder decodeIntegerForKey:kMASShortcutModifierFlags];
self.modifierFlags = [decoder decodeIntegerForKey:MASShortcutModifierFlags];
}
return self;
}
@@ -52,7 +57,8 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
+ (MASShortcut *)shortcutWithData:(NSData *)data
{
return (data ? (MASShortcut *)[NSKeyedUnarchiver unarchiveObjectWithData:data] : nil);
id shortcut = (data ? [NSKeyedUnarchiver unarchiveObjectWithData:data] : nil);
return shortcut;
}
#pragma mark - Shortcut accessors
@@ -82,6 +88,38 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
return [NSString stringWithFormat:@"%@%@", self.modifierFlagsString, self.keyCodeString];
}
- (NSString *)keyCodeStringForKeyEquivalent
{
NSString *keyCodeString = self.keyCodeString;
if (keyCodeString.length > 1) {
switch (self.keyCode) {
case kVK_F1: return MASShortcutChar(0xF704);
case kVK_F2: return MASShortcutChar(0xF705);
case kVK_F3: return MASShortcutChar(0xF706);
case kVK_F4: return MASShortcutChar(0xF707);
case kVK_F5: return MASShortcutChar(0xF708);
case kVK_F6: return MASShortcutChar(0xF709);
case kVK_F7: return MASShortcutChar(0xF70a);
case kVK_F8: return MASShortcutChar(0xF70b);
case kVK_F9: return MASShortcutChar(0xF70c);
case kVK_F10: return MASShortcutChar(0xF70d);
case kVK_F11: return MASShortcutChar(0xF70e);
case kVK_F12: return MASShortcutChar(0xF70f);
// From this point down I am guessing F13 etc come sequentially, I don't have a keyboard to test.
case kVK_F13: return MASShortcutChar(0xF710);
case kVK_F14: return MASShortcutChar(0xF711);
case kVK_F15: return MASShortcutChar(0xF712);
case kVK_F16: return MASShortcutChar(0xF713);
case kVK_F17: return MASShortcutChar(0xF714);
case kVK_F18: return MASShortcutChar(0xF715);
case kVK_F19: return MASShortcutChar(0xF716);
case kVK_Space: return MASShortcutChar(0x20);
default: return @"";
}
}
return keyCodeString.lowercaseString;
}
- (NSString *)keyCodeString
{
// Some key codes don't have an equivalent
@@ -103,6 +141,9 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
case kVK_F14: return @"F14";
case kVK_F15: return @"F15";
case kVK_F16: return @"F16";
case kVK_F17: return @"F17";
case kVK_F18: return @"F18";
case kVK_F19: return @"F19";
case kVK_Space: return NSLocalizedString(@"Space", @"Shortcut glyph name for SPACE key");
case kVK_Escape: return MASShortcutChar(kMASShortcutGlyphEscape);
case kVK_Delete: return MASShortcutChar(kMASShortcutGlyphDeleteLeft);
@@ -153,7 +194,7 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
UniCharCount length = 0;
UniChar chars[256] = { 0 };
UInt32 deadKeyState = 0;
error = UCKeyTranslate(layoutData, self.keyCode, kUCKeyActionDisplay, 0, // No modifiers
error = UCKeyTranslate(layoutData, (UInt16)self.keyCode, kUCKeyActionDisplay, 0, // No modifiers
LMGetKbdType(), kUCKeyTranslateNoDeadKeysMask, &deadKeyState,
sizeof(chars) / sizeof(UniChar), &length, chars);
keystroke = ((error == noErr) && length ? [NSString stringWithCharacters:chars length:length] : @"");
@@ -202,19 +243,50 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
return (self.modifierFlags == NSCommandKeyMask) && ([codeString isEqualToString:@"W"] || [codeString isEqualToString:@"Q"]);
}
BOOL MASShortcutAllowsAnyHotkeyWithOptionModifier = NO;
+ (void)setAllowsAnyHotkeyWithOptionModifier:(BOOL)allow
{
MASShortcutAllowsAnyHotkeyWithOptionModifier = allow;
}
+ (BOOL)allowsAnyHotkeyWithOptionModifier
{
return MASShortcutAllowsAnyHotkeyWithOptionModifier;
}
- (BOOL)isValid
{
BOOL hasFlags = (_modifierFlags > 0);
BOOL hasCommand = ((_modifierFlags & NSCommandKeyMask) > 0);
BOOL hasControl = ((_modifierFlags & NSControlKeyMask) > 0);
BOOL hasOption = ((_modifierFlags & NSAlternateKeyMask) > 0);
BOOL isFunction = ((_keyCode == kVK_F1) || (_keyCode == kVK_F2) || (_keyCode == kVK_F3) || (_keyCode == kVK_F4) ||
(_keyCode == kVK_F5) || (_keyCode == kVK_F6) || (_keyCode == kVK_F7) || (_keyCode == kVK_F8) ||
(_keyCode == kVK_F9) || (_keyCode == kVK_F10) || (_keyCode == kVK_F11) || (_keyCode == kVK_F12) ||
(_keyCode == kVK_F13) || (_keyCode == kVK_F14) || (_keyCode == kVK_F15) || (_keyCode == kVK_F16) ||
(_keyCode == kVK_F17) || (_keyCode == kVK_F18) || (_keyCode == kVK_F19) || (_keyCode == kVK_F20));
BOOL isSpecial = ((_keyCode == kVK_Space) || (_keyCode == kVK_Escape) || (_keyCode == kVK_Return));
return ((hasFlags && (hasCommand || hasControl || (hasOption && isSpecial))) || isFunction);
// Allow any function key with any combination of modifiers
BOOL includesFunctionKey = ((_keyCode == kVK_F1) || (_keyCode == kVK_F2) || (_keyCode == kVK_F3) || (_keyCode == kVK_F4) ||
(_keyCode == kVK_F5) || (_keyCode == kVK_F6) || (_keyCode == kVK_F7) || (_keyCode == kVK_F8) ||
(_keyCode == kVK_F9) || (_keyCode == kVK_F10) || (_keyCode == kVK_F11) || (_keyCode == kVK_F12) ||
(_keyCode == kVK_F13) || (_keyCode == kVK_F14) || (_keyCode == kVK_F15) || (_keyCode == kVK_F16) ||
(_keyCode == kVK_F17) || (_keyCode == kVK_F18) || (_keyCode == kVK_F19) || (_keyCode == kVK_F20));
if (includesFunctionKey) return YES;
// Do not allow any other key without modifiers
BOOL hasModifierFlags = (_modifierFlags > 0);
if (!hasModifierFlags) return NO;
// Allow any hotkey containing Control or Command modifier
BOOL includesCommand = ((_modifierFlags & NSCommandKeyMask) > 0);
BOOL includesControl = ((_modifierFlags & NSControlKeyMask) > 0);
if (includesCommand || includesControl) return YES;
// Allow Option key only in selected cases
BOOL includesOption = ((_modifierFlags & NSAlternateKeyMask) > 0);
if (includesOption) {
// Always allow Option-Space and Option-Escape because they do not have any bind system commands
if ((_keyCode == kVK_Space) || (_keyCode == kVK_Escape)) return YES;
// Allow Option modifier with any key even if it will break the system binding
if ([[self class] allowsAnyHotkeyWithOptionModifier]) return YES;
}
// The hotkey does not have any modifiers or violates system bindings
return NO;
}
- (BOOL)isKeyEquivalent:(NSString *)keyEquivalent flags:(NSUInteger)flags takenInMenu:(NSMenu *)menu error:(NSError **)outError
@@ -223,10 +295,17 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
if (menuItem.hasSubmenu && [self isKeyEquivalent:keyEquivalent flags:flags takenInMenu:menuItem.submenu error:outError]) return YES;
BOOL equalFlags = (MASShortcutClear(menuItem.keyEquivalentModifierMask) == flags);
BOOL equalHotkey = [menuItem.keyEquivalent.uppercaseString isEqualToString:keyEquivalent];
if (equalFlags && equalHotkey) {
BOOL equalHotkeyLowercase = [menuItem.keyEquivalent.lowercaseString isEqualToString:keyEquivalent];
// Check if the cases are different, we know ours is lower and that shift is included in our modifiers
// If theirs is capitol, we need to add shift to their modifiers
if (equalHotkeyLowercase && ![menuItem.keyEquivalent isEqualToString:keyEquivalent]) {
equalFlags = (MASShortcutClear(menuItem.keyEquivalentModifierMask | NSShiftKeyMask) == flags);
}
if (equalFlags && equalHotkeyLowercase) {
if (outError) {
NSString *format = NSLocalizedString(@"This shortcut cannot be used used because it is already used by the menu item %@.",
NSString *format = NSLocalizedString(@"This shortcut cannot be used because it is already used by the menu item %@.",
@"Message for alert when shortcut is already used");
NSDictionary *info = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:format, menuItem.title]
forKey:NSLocalizedDescriptionKey];
@@ -248,12 +327,14 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(globalHotKeys, i);
CFNumberRef code = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
CFNumberRef flags = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
CFNumberRef enabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);
if (([(__bridge NSNumber *)code unsignedIntegerValue] == self.keyCode) &&
([(__bridge NSNumber *)flags unsignedIntegerValue] == self.carbonFlags)) {
([(__bridge NSNumber *)flags unsignedIntegerValue] == self.carbonFlags) &&
([(__bridge NSNumber *)enabled boolValue])) {
if (outError) {
NSString *description = NSLocalizedString(@"This combination cannot be used used because it is already used by a system-wide "
NSString *description = NSLocalizedString(@"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");
@@ -265,7 +346,7 @@ NSString *const kMASShortcutModifierFlags = @"ModifierFlags";
}
CFRelease(globalHotKeys);
}
return [self isKeyEquivalent:self.keyCodeString flags:self.modifierFlags takenInMenu:[NSApp mainMenu] error:outError];
return [self isKeyEquivalent:self.keyCodeStringForKeyEquivalent flags:self.modifierFlags takenInMenu:[NSApp mainMenu] error:outError];
}
@end
+13
View File
@@ -0,0 +1,13 @@
Pod::Spec.new do |s|
s.name = 'MASShortcut'
s.version = '1.2.3'
s.summary = 'Modern framework for managing global keyboard shortcuts compatible with Mac App Store'
s.homepage = 'https://github.com/sonoramac/MASShortcut'
s.authors = { 'Vadim Shpakovski' => 'vadim@shpakovski.com' }
s.license = 'BSD 2-clause'
s.source = { :git => 'git@github.com:shpakovski/MASShortcut.git', :tag => '1.2.3' }
s.source_files = '*.{h,m}'
s.framework = 'Carbon'
s.requires_arc = true
end
+14 -4
View File
@@ -15,18 +15,24 @@
@implementation MASShortcutView (UserDefaults)
void *kDefaultsObserver = &kDefaultsObserver;
void *MASAssociatedDefaultsObserver = &MASAssociatedDefaultsObserver;
- (NSString *)associatedUserDefaultsKey
{
MASShortcutDefaultsObserver *defaultsObserver = objc_getAssociatedObject(self, kDefaultsObserver);
MASShortcutDefaultsObserver *defaultsObserver = objc_getAssociatedObject(self, MASAssociatedDefaultsObserver);
return defaultsObserver.userDefaultsKey;
}
- (void)setAssociatedUserDefaultsKey:(NSString *)associatedUserDefaultsKey
{
// First, stop observing previous shortcut view
objc_setAssociatedObject(self, MASAssociatedDefaultsObserver, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (associatedUserDefaultsKey.length == 0) return;
// Next, start observing current shortcut view
MASShortcutDefaultsObserver *defaultsObserver = [[MASShortcutDefaultsObserver alloc] initWithShortcutView:self userDefaultsKey:associatedUserDefaultsKey];
objc_setAssociatedObject(self, kDefaultsObserver, defaultsObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, MASAssociatedDefaultsObserver, defaultsObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@@ -109,7 +115,11 @@ void *kShortcutValueObserver = &kShortcutValueObserver;
if (_internalShortcutChange) return;
MASShortcut *shortcut = [object valueForKey:keyPath];
_internalPreferenceChange = YES;
[[NSUserDefaults standardUserDefaults] setObject:shortcut.data forKey:_userDefaultsKey];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:(shortcut.data ?: [NSKeyedArchiver archivedDataWithRootObject:nil]) forKey:_userDefaultsKey];
[defaults synchronize];
_internalPreferenceChange = NO;
}
else {
+8 -2
View File
@@ -1,8 +1,11 @@
#import <AppKit/AppKit.h>
@class MASShortcut;
typedef enum {
MASShortcutViewAppearanceDefault = 0,
MASShortcutViewAppearanceTexturedRect
MASShortcutViewAppearanceDefault = 0, // Height = 19 px
MASShortcutViewAppearanceTexturedRect, // Height = 25 px
MASShortcutViewAppearanceRounded // Height = 43 px
} MASShortcutViewAppearance;
@interface MASShortcutView : NSView
@@ -13,4 +16,7 @@ typedef enum {
@property (nonatomic, copy) void (^shortcutValueChange)(MASShortcutView *sender);
@property (nonatomic) MASShortcutViewAppearance appearance;
/// Returns custom class for drawing control.
+ (Class)shortcutCellClass;
@end
+49 -11
View File
@@ -29,22 +29,42 @@
@synthesize shortcutPlaceholder = _shortcutPlaceholder;
@synthesize shortcutValueChange = _shortcutValueChange;
@synthesize recording = _recording;
@synthesize appearance = _appearance;
#pragma mark -
+ (Class)shortcutCellClass
{
return [NSButtonCell class];
}
- (id)initWithFrame:(CGRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
_shortcutCell = [[NSButtonCell alloc] init];
_shortcutCell.buttonType = NSPushOnPushOffButton;
_shortcutCell.font = [[NSFontManager sharedFontManager] convertFont:_shortcutCell.font toSize:BUTTON_FONT_SIZE];
_enabled = YES;
[self resetShortcutCellStyle];
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
_shortcutCell = [[[self.class shortcutCellClass] alloc] init];
_shortcutCell.buttonType = NSPushOnPushOffButton;
_shortcutCell.font = [[NSFontManager sharedFontManager] convertFont:_shortcutCell.font toSize:BUTTON_FONT_SIZE];
_enabled = YES;
[self resetShortcutCellStyle];
}
- (void)dealloc
{
[self activateEventMonitoring:NO];
@@ -83,6 +103,10 @@
_shortcutCell.bezelStyle = NSTexturedRoundedBezelStyle;
break;
}
case MASShortcutViewAppearanceRounded: {
_shortcutCell.bezelStyle = NSRoundedBezelStyle;
break;
}
}
}
@@ -148,6 +172,10 @@
[_shortcutCell drawWithFrame:CGRectOffset(frame, 0.0, 1.0) inView:self];
break;
}
case MASShortcutViewAppearanceRounded: {
[_shortcutCell drawWithFrame:CGRectOffset(frame, 0.0, 1.0) inView:self];
break;
}
}
}
@@ -161,7 +189,7 @@
[self getShortcutRect:&shortcutRect hintRect:NULL];
NSString *title = (self.recording
? (_hinting
? NSLocalizedString(@"Use Old Shortuct", @"Cancel action button for non-empty shortcut in recording state")
? NSLocalizedString(@"Use Old Shortcut", @"Cancel action button for non-empty shortcut in recording state")
: (self.shortcutPlaceholder.length > 0
? self.shortcutPlaceholder
: NSLocalizedString(@"Type New Shortcut", @"Non-empty shortcut button in recording state")))
@@ -195,7 +223,13 @@
- (void)getShortcutRect:(CGRect *)shortcutRectRef hintRect:(CGRect *)hintRectRef
{
CGRect shortcutRect, hintRect;
CGRectDivide(self.bounds, &hintRect, &shortcutRect, HINT_BUTTON_WIDTH, CGRectMaxXEdge);
CGFloat hintButtonWidth = HINT_BUTTON_WIDTH;
switch (self.appearance) {
case MASShortcutViewAppearanceTexturedRect: hintButtonWidth += 2.0; break;
case MASShortcutViewAppearanceRounded: hintButtonWidth += 3.0; break;
default: break;
}
CGRectDivide(self.bounds, &hintRect, &shortcutRect, hintButtonWidth, CGRectMaxXEdge);
if (shortcutRectRef) *shortcutRectRef = shortcutRect;
if (hintRectRef) *hintRectRef = hintRect;
}
@@ -339,7 +373,7 @@ void *kUserDataHint = &kUserDataHint;
weakSelf.recording = NO;
event = nil;
}
else if (shortcut.keyCode == kVK_Escape) {
else if (shortcut.keyCode == kVK_Escape && !shortcut.modifierFlags) {
// Cancel recording
weakSelf.recording = NO;
event = nil;
@@ -360,9 +394,13 @@ void *kUserDataHint = &kUserDataHint;
[weakSelf activateEventMonitoring:NO];
NSString *format = NSLocalizedString(@"The key combination %@ cannot be used",
@"Title for alert when shortcut is already used");
NSRunCriticalAlertPanel([NSString stringWithFormat:format, shortcut], error.localizedDescription,
NSLocalizedString(@"OK", @"Alert button when shortcut is already used"),
nil, nil);
NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSCriticalAlertStyle;
alert.informativeText = [NSString stringWithFormat:format, shortcut];
alert.messageText = error.localizedDescription;
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"Alert button when shortcut is already used")];
[alert runModal];
weakSelf.shortcutPlaceholder = nil;
[weakSelf activateResignObserver:YES];
[weakSelf activateEventMonitoring:YES];
+53 -12
View File
@@ -8,23 +8,64 @@ The project MASShortcut introduces modern API and user interface for recording,
I hope, it is really easy:
// Drop a custom view into XIB and set its class to MASShortcutView
@property (nonatomic, weak) IBOutlet MASShortcutView *shortcutView;
```objective-c
// Drop a custom view into XIB, set its class to MASShortcutView and its height to 19. If you select another appearance style look up the correct values in MASShortcutView.h
@property (nonatomic, weak) IBOutlet MASShortcutView *shortcutView;
// Think up a preference key to store a global shortcut between launches
NSString *const kPreferenceGlobalShortcut = @"GlobalShortcut";
// Think up a preference key to store a global shortcut between launches
NSString *const kPreferenceGlobalShortcut = @"GlobalShortcut";
// Assign the preference key and the shortcut view will take care of persistence
self.shortcutView.associatedUserDefaultsKey = kPreferenceGlobalShortcut;
// Assign the preference key and the shortcut view will take care of persistence
self.shortcutView.associatedUserDefaultsKey = kPreferenceGlobalShortcut;
// Execute your block of code automatically when user triggers a shortcut from preferences
[MASShortcut registerGlobalShortcutWithUserDefaultsKey:kPreferenceGlobalShortcut handler:^{
// Let me know if you find a better or more convenient API.
}];
// Execute your block of code automatically when user triggers a shortcut from preferences
[MASShortcut registerGlobalShortcutWithUserDefaultsKey:kPreferenceGlobalShortcut handler:^{
// Let me know if you find a better or more convenient API.
}];
```
To set an example, I made a demo project: [MASShortcutDemo](https://github.com/shpakovski/MASShortcutDemo). Enjoy!
#Notifications
By registering for KVO notifications from `NSUserDefaultsController`, you can get a callback whenever a user changes the shortcut, allowing you to perform any UI updates, or other code handling tasks.
This is just as easy to implement:
```objective-c
// Declare an ivar for key path in the user defaults controller
NSString *_observableKeyPath;
// Make a global context reference
void *kGlobalShortcutContext = &kGlobalShortcutContext;
// Implement when loading view
_observableKeyPath = [@"values." stringByAppendingString:kPreferenceGlobalShortcut];
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:_observableKeyPath
options:NSKeyValueObservingOptionInitial
context:kGlobalShortcutContext];
// Capture the KVO change and do something
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)obj
change:(NSDictionary *)change context:(void *)ctx
{
if (ctx == kGlobalShortcutContext) {
NSLog(@"Shortcut has changed");
}
else {
[super observeValueForKeyPath:keyPath ofObject:obj change:change context:ctx];
}
}
// Do not forget to remove the observer
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self
forKeyPath:_observableKeyPath
context:kGlobalShortcutContext];
```
# Non-ARC Version
If you like retain/release, please check out these forks: [heardrwt/MASShortcut](https://github.com/heardrwt/MASShortcut) and [chendo/MASShortcut](https://github.com/chendo/MASShortcut). However, the preferred way is to enable the `-fobjc-arc` in Xcode source options.
# Copyright
MASShortcut is licensed under the BSD license.
MASShortcut is licensed under the 2-clause BSD license.