diff options
| author | Tomáš Znamenáček | 2015-01-16 11:38:43 +0100 |
|---|---|---|
| committer | Tomáš Znamenáček | 2015-01-16 11:38:43 +0100 |
| commit | 66fd9b8e4117069d2c58563d668577f5af0dae87 (patch) | |
| tree | 3a816319fad68295dd9f10eb48ab7871eb383d56 /Framework | |
| parent | 1baa2bae9d63093d64239992dc702b10d149b7c5 (diff) | |
| parent | 27eace979e202116e44db3dc6acc9a8427ecaa0a (diff) | |
| download | MASShortcut-66fd9b8e4117069d2c58563d668577f5af0dae87.tar.bz2 | |
Merge pull request #57 from shpakovski/legacy-osx-support
Legacy OS X support down to 10.6 included (fixes #56).
Diffstat (limited to 'Framework')
| -rw-r--r-- | Framework/MASHotKey.m | 2 | ||||
| -rw-r--r-- | Framework/MASHotKeyTests.m | 15 | ||||
| -rw-r--r-- | Framework/MASShortcutMonitor.h | 2 | ||||
| -rw-r--r-- | Framework/MASShortcutMonitor.m | 11 | ||||
| -rw-r--r-- | Framework/MASShortcutMonitorTests.m | 23 | ||||
| -rw-r--r-- | Framework/MASShortcutView.m | 4 |
6 files changed, 50 insertions, 7 deletions
diff --git a/Framework/MASHotKey.m b/Framework/MASHotKey.m index 7886440..c5ab744 100644 --- a/Framework/MASHotKey.m +++ b/Framework/MASHotKey.m @@ -19,7 +19,7 @@ FourCharCode const MASHotKeySignature = 'MASS'; EventHotKeyID hotKeyID = { .signature = MASHotKeySignature, .id = _carbonID }; OSStatus status = RegisterEventHotKey([shortcut carbonKeyCode], [shortcut carbonFlags], - hotKeyID, GetEventDispatcherTarget(), kEventHotKeyExclusive, &_hotKeyRef); + hotKeyID, GetEventDispatcherTarget(), 0, &_hotKeyRef); if (status != noErr) { return nil; diff --git a/Framework/MASHotKeyTests.m b/Framework/MASHotKeyTests.m new file mode 100644 index 0000000..65361ab --- /dev/null +++ b/Framework/MASHotKeyTests.m @@ -0,0 +1,15 @@ +#import "MASHotKey.h" + +@interface MASHotKeyTests : XCTestCase +@end + +@implementation MASHotKeyTests + +- (void) testBasicFunctionality +{ + MASHotKey *hotKey = [MASHotKey registeredHotKeyWithShortcut: + [MASShortcut shortcutWithKeyCode:kVK_ANSI_H modifierFlags:NSCommandKeyMask|NSAlternateKeyMask]]; + XCTAssertNotNil(hotKey, @"Register a simple Cmd-Alt-H hotkey."); +} + +@end diff --git a/Framework/MASShortcutMonitor.h b/Framework/MASShortcutMonitor.h index 69affc1..a1aaaa8 100644 --- a/Framework/MASShortcutMonitor.h +++ b/Framework/MASShortcutMonitor.h @@ -18,7 +18,7 @@ Attempting to insert an already registered shortcut probably won’t work. It may burn your house or cut your fingers. You have been warned. */ -- (void) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action; +- (BOOL) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action; - (BOOL) isShortcutRegistered: (MASShortcut*) shortcut; - (void) unregisterShortcut: (MASShortcut*) shortcut; diff --git a/Framework/MASShortcutMonitor.m b/Framework/MASShortcutMonitor.m index 099f4b1..fce8022 100644 --- a/Framework/MASShortcutMonitor.m +++ b/Framework/MASShortcutMonitor.m @@ -45,11 +45,16 @@ static OSStatus MASCarbonEventCallback(EventHandlerCallRef, EventRef, void*); #pragma mark Registration -- (void) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action +- (BOOL) registerShortcut: (MASShortcut*) shortcut withAction: (dispatch_block_t) action { MASHotKey *hotKey = [MASHotKey registeredHotKeyWithShortcut:shortcut]; - [hotKey setAction:action]; - [_hotKeys setObject:hotKey forKey:shortcut]; + if (hotKey) { + [hotKey setAction:action]; + [_hotKeys setObject:hotKey forKey:shortcut]; + return YES; + } else { + return NO; + } } - (void) unregisterShortcut: (MASShortcut*) shortcut diff --git a/Framework/MASShortcutMonitorTests.m b/Framework/MASShortcutMonitorTests.m new file mode 100644 index 0000000..ccdcaef --- /dev/null +++ b/Framework/MASShortcutMonitorTests.m @@ -0,0 +1,23 @@ +#import "MASShortcutMonitor.h" + +@interface MASShortcutMonitorTests : XCTestCase +@end + +@implementation MASShortcutMonitorTests + +- (void) testMonitorCreation +{ + XCTAssertNotNil([MASShortcutMonitor sharedMonitor], @"Create a shared shortcut monitor."); +} + +- (void) testShortcutRegistration +{ + MASShortcutMonitor *monitor = [MASShortcutMonitor sharedMonitor]; + MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:kVK_ANSI_H modifierFlags:NSCommandKeyMask|NSAlternateKeyMask]; + XCTAssertTrue([monitor registerShortcut:shortcut withAction:NULL], @"Register a shortcut."); + XCTAssertTrue([monitor isShortcutRegistered:shortcut], @"Remember a previously registered shortcut."); + [monitor unregisterShortcut:shortcut]; + XCTAssertFalse([monitor isShortcutRegistered:shortcut], @"Forget shortcut after unregistering."); +} + +@end diff --git a/Framework/MASShortcutView.m b/Framework/MASShortcutView.m index 9f26e07..d521880 100644 --- a/Framework/MASShortcutView.m +++ b/Framework/MASShortcutView.m @@ -369,7 +369,7 @@ void *kUserDataHint = &kUserDataHint; static id eventMonitor = nil; if (shouldActivate) { - __weak MASShortcutView *weakSelf = self; + __unsafe_unretained MASShortcutView *weakSelf = self; NSEventMask eventMask = (NSKeyDownMask | NSFlagsChangedMask); eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^(NSEvent *event) { @@ -450,7 +450,7 @@ void *kUserDataHint = &kUserDataHint; static id observer = nil; NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; if (shouldActivate) { - __weak MASShortcutView *weakSelf = self; + __unsafe_unretained MASShortcutView *weakSelf = self; observer = [notificationCenter addObserverForName:NSWindowDidResignKeyNotification object:self.window queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { weakSelf.recording = NO; |
