From 678f8ca1a961eb9125aa82b7d2277fda4db7f146 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 18 Nov 2016 22:54:19 -0500 Subject: Use hard-coded hotkey to invoke battery dialog dismisser * Add DDHotKey library to the project. Just copy the files in from the latest HEAD@e0481f648e0bc7e55d183622b00510b6721152d8. * Only add DDHotKeyCenter.{h,m} & DDHotKeyUtilities.{h,m} to the "Low Battery Yup.d" target as the *TextField file is only useful for a UI to choose a hotkey, and we don't have a UI in this target. * Set the DDHotKey* files to use ARC when compiling since they require it. This was done by going to Build Phases -> Compile Sources and adding this flag for both files: -fobj-arc this I figured out thanks to the following SO post: http://stackoverflow.com/questions/6448874/disable-automatic-reference-counting-for-some-files/10255815#10255815 * Link Carbon.framework because DDHotKey depends on it to register global hotkeys * Move our mouse moving & clicking code to a new method that gets used as the global hotkey action * Fix a runtime error caused by MainMenu.xib not being available (as a result of f0e8b5188e6fb984511eb01849380669e69632a6). To do this, we modify `main.m` to bypass the check for MainMenu.xib as described in this SO post: http://stackoverflow.com/questions/6945872/cocoa-app-without-a-mainmenu-xib/6946016#6946016 * Delete the `window` `IBOutlet` since we no longer have a MainMenu.xib and don't have a window in this app. --- DDHotKey/DDHotKeyCenter.h | 93 +++++++++ DDHotKey/DDHotKeyCenter.m | 284 ++++++++++++++++++++++++++++ DDHotKey/DDHotKeyTextField.h | 20 ++ DDHotKey/DDHotKeyTextField.m | 138 ++++++++++++++ DDHotKey/DDHotKeyUtilities.h | 14 ++ DDHotKey/DDHotKeyUtilities.m | 144 ++++++++++++++ Low Battery Yup d/AppDelegate.h | 2 +- Low Battery Yup d/AppDelegate.m | 12 ++ Low Battery Yup d/main.m | 6 +- Low Battery Yup.d.xcodeproj/project.pbxproj | 24 +++ 10 files changed, 735 insertions(+), 2 deletions(-) create mode 100644 DDHotKey/DDHotKeyCenter.h create mode 100644 DDHotKey/DDHotKeyCenter.m create mode 100644 DDHotKey/DDHotKeyTextField.h create mode 100644 DDHotKey/DDHotKeyTextField.m create mode 100644 DDHotKey/DDHotKeyUtilities.h create mode 100644 DDHotKey/DDHotKeyUtilities.m diff --git a/DDHotKey/DDHotKeyCenter.h b/DDHotKey/DDHotKeyCenter.h new file mode 100644 index 0000000..6f79bbb --- /dev/null +++ b/DDHotKey/DDHotKeyCenter.h @@ -0,0 +1,93 @@ +/* + DDHotKey -- DDHotKeyCenter.h + + Copyright (c) Dave DeLong + + Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + + The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. + */ + +#import + +//a convenient typedef for the required signature of a hotkey block callback +typedef void (^DDHotKeyTask)(NSEvent*); + +@interface DDHotKey : NSObject + +// creates a new hotkey but does not register it ++ (instancetype)hotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task; + +@property (nonatomic, assign, readonly) id target; +@property (nonatomic, readonly) SEL action; +@property (nonatomic, strong, readonly) id object; +@property (nonatomic, copy, readonly) DDHotKeyTask task; + +@property (nonatomic, readonly) unsigned short keyCode; +@property (nonatomic, readonly) NSUInteger modifierFlags; + +@end + +#pragma mark - + +@interface DDHotKeyCenter : NSObject + ++ (instancetype)sharedHotKeyCenter; + +/** + Register a hotkey. + */ +- (DDHotKey *)registerHotKey:(DDHotKey *)hotKey; + +/** + Register a target/action hotkey. + The modifierFlags must be a bitwise OR of NSCommandKeyMask, NSAlternateKeyMask, NSControlKeyMask, or NSShiftKeyMask; + Returns the hotkey registered. If registration failed, returns nil. + */ +- (DDHotKey *)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags target:(id)target action:(SEL)action object:(id)object; + +/** + Register a block callback hotkey. + The modifierFlags must be a bitwise OR of NSCommandKeyMask, NSAlternateKeyMask, NSControlKeyMask, or NSShiftKeyMask; + Returns the hotkey registered. If registration failed, returns nil. + */ +- (DDHotKey *)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task; + +/** + See if a hotkey exists with the specified keycode and modifier flags. + NOTE: this will only check among hotkeys you have explicitly registered with DDHotKeyCenter. This does not check all globally registered hotkeys. + */ +- (BOOL)hasRegisteredHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags; + +/** + Unregister a specific hotkey + */ +- (void)unregisterHotKey:(DDHotKey *)hotKey; + +/** + Unregister all hotkeys + */ +- (void)unregisterAllHotKeys; + +/** + Unregister all hotkeys with a specific target + */ +- (void)unregisterHotKeysWithTarget:(id)target; + +/** + Unregister all hotkeys with a specific target and action + */ +- (void)unregisterHotKeysWithTarget:(id)target action:(SEL)action; + +/** + Unregister a hotkey with a specific keycode and modifier flags + */ +- (void)unregisterHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags; + +/** + Returns a set of currently registered hotkeys + **/ +- (NSSet *)registeredHotKeys; + +@end + diff --git a/DDHotKey/DDHotKeyCenter.m b/DDHotKey/DDHotKeyCenter.m new file mode 100644 index 0000000..e2d5997 --- /dev/null +++ b/DDHotKey/DDHotKeyCenter.m @@ -0,0 +1,284 @@ +/* + DDHotKey -- DDHotKeyCenter.m + + Copyright (c) Dave DeLong + + Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + + The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. + */ + +#import +#import + +#import "DDHotKeyCenter.h" +#import "DDHotKeyUtilities.h" + +#pragma mark Private Global Declarations + +OSStatus dd_hotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData); + +#pragma mark DDHotKey + +@interface DDHotKey () + +@property (nonatomic, retain) NSValue *hotKeyRef; +@property (nonatomic) UInt32 hotKeyID; + + +@property (nonatomic, assign, setter = _setTarget:) id target; +@property (nonatomic, setter = _setAction:) SEL action; +@property (nonatomic, strong, setter = _setObject:) id object; +@property (nonatomic, copy, setter = _setTask:) DDHotKeyTask task; + +@property (nonatomic, setter = _setKeyCode:) unsigned short keyCode; +@property (nonatomic, setter = _setModifierFlags:) NSUInteger modifierFlags; + +@end + +@implementation DDHotKey + ++ (instancetype)hotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task { + DDHotKey *newHotKey = [[self alloc] init]; + [newHotKey _setTask:task]; + [newHotKey _setKeyCode:keyCode]; + [newHotKey _setModifierFlags:flags]; + return newHotKey; +} + +- (void) dealloc { + [[DDHotKeyCenter sharedHotKeyCenter] unregisterHotKey:self]; +} + +- (NSUInteger)hash { + return [self keyCode] ^ [self modifierFlags]; +} + +- (BOOL)isEqual:(id)object { + BOOL equal = NO; + if ([object isKindOfClass:[DDHotKey class]]) { + equal = ([object keyCode] == [self keyCode]); + equal &= ([object modifierFlags] == [self modifierFlags]); + } + return equal; +} + +- (NSString *)description { + NSMutableArray *bits = [NSMutableArray array]; + if ((_modifierFlags & NSControlKeyMask) > 0) { [bits addObject:@"NSControlKeyMask"]; } + if ((_modifierFlags & NSCommandKeyMask) > 0) { [bits addObject:@"NSCommandKeyMask"]; } + if ((_modifierFlags & NSShiftKeyMask) > 0) { [bits addObject:@"NSShiftKeyMask"]; } + if ((_modifierFlags & NSAlternateKeyMask) > 0) { [bits addObject:@"NSAlternateKeyMask"]; } + + NSString *flags = [NSString stringWithFormat:@"(%@)", [bits componentsJoinedByString:@" | "]]; + NSString *invokes = @"(block)"; + if ([self target] != nil && [self action] != nil) { + invokes = [NSString stringWithFormat:@"[%@ %@]", [self target], NSStringFromSelector([self action])]; + } + return [NSString stringWithFormat:@"%@\n\t(key: %hu\n\tflags: %@\n\tinvokes: %@)", [super description], [self keyCode], flags, invokes]; +} + +- (void)invokeWithEvent:(NSEvent *)event { + if (_target != nil && _action != nil && [_target respondsToSelector:_action]) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + [_target performSelector:_action withObject:event withObject:_object]; +#pragma clang diagnostic pop + } else if (_task != nil) { + _task(event); + } +} + +@end + +#pragma mark DDHotKeyCenter + +static DDHotKeyCenter *sharedHotKeyCenter = nil; + +@implementation DDHotKeyCenter { + NSMutableSet *_registeredHotKeys; + UInt32 _nextHotKeyID; +} + ++ (instancetype)sharedHotKeyCenter { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedHotKeyCenter = [super allocWithZone:nil]; + sharedHotKeyCenter = [sharedHotKeyCenter init]; + + EventTypeSpec eventSpec; + eventSpec.eventClass = kEventClassKeyboard; + eventSpec.eventKind = kEventHotKeyReleased; + InstallApplicationEventHandler(&dd_hotKeyHandler, 1, &eventSpec, NULL, NULL); + }); + return sharedHotKeyCenter; +} + ++ (id)allocWithZone:(NSZone *)zone { + return sharedHotKeyCenter; +} + +- (id)init { + if (self != sharedHotKeyCenter) { return sharedHotKeyCenter; } + + self = [super init]; + if (self) { + _registeredHotKeys = [[NSMutableSet alloc] init]; + _nextHotKeyID = 1; + } + return self; +} + +- (NSSet *)hotKeysMatching:(BOOL(^)(DDHotKey *hotkey))matcher { + NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { + return matcher(evaluatedObject); + }]; + return [_registeredHotKeys filteredSetUsingPredicate:predicate]; +} + +- (BOOL)hasRegisteredHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags { + return [self hotKeysMatching:^BOOL(DDHotKey *hotkey) { + return hotkey.keyCode == keyCode && hotkey.modifierFlags == flags; + }].count > 0; +} + +- (DDHotKey *)_registerHotKey:(DDHotKey *)hotKey { + if ([_registeredHotKeys containsObject:hotKey]) { + return hotKey; + } + + EventHotKeyID keyID; + keyID.signature = 'htk1'; + keyID.id = _nextHotKeyID; + + EventHotKeyRef carbonHotKey; + UInt32 flags = DDCarbonModifierFlagsFromCocoaModifiers([hotKey modifierFlags]); + OSStatus err = RegisterEventHotKey([hotKey keyCode], flags, keyID, GetEventDispatcherTarget(), 0, &carbonHotKey); + + //error registering hot key + if (err != 0) { return nil; } + + NSValue *refValue = [NSValue valueWithPointer:carbonHotKey]; + [hotKey setHotKeyRef:refValue]; + [hotKey setHotKeyID:_nextHotKeyID]; + + _nextHotKeyID++; + [_registeredHotKeys addObject:hotKey]; + + return hotKey; +} + +- (DDHotKey *)registerHotKey:(DDHotKey *)hotKey { + return [self _registerHotKey:hotKey]; +} + +- (void)unregisterHotKey:(DDHotKey *)hotKey { + NSValue *hotKeyRef = [hotKey hotKeyRef]; + if (hotKeyRef) { + EventHotKeyRef carbonHotKey = (EventHotKeyRef)[hotKeyRef pointerValue]; + UnregisterEventHotKey(carbonHotKey); + [hotKey setHotKeyRef:nil]; + } + + [_registeredHotKeys removeObject:hotKey]; +} + +- (DDHotKey *)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task { + //we can't add a new hotkey if something already has this combo + if ([self hasRegisteredHotKeyWithKeyCode:keyCode modifierFlags:flags]) { return NO; } + + DDHotKey *newHotKey = [[DDHotKey alloc] init]; + [newHotKey _setTask:task]; + [newHotKey _setKeyCode:keyCode]; + [newHotKey _setModifierFlags:flags]; + + return [self _registerHotKey:newHotKey]; +} + +- (DDHotKey *)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags target:(id)target action:(SEL)action object:(id)object { + //we can't add a new hotkey if something already has this combo + if ([self hasRegisteredHotKeyWithKeyCode:keyCode modifierFlags:flags]) { return NO; } + + //build the hotkey object: + DDHotKey *newHotKey = [[DDHotKey alloc] init]; + [newHotKey _setTarget:target]; + [newHotKey _setAction:action]; + [newHotKey _setObject:object]; + [newHotKey _setKeyCode:keyCode]; + [newHotKey _setModifierFlags:flags]; + return [self _registerHotKey:newHotKey]; +} + +- (void)unregisterHotKeysMatching:(BOOL(^)(DDHotKey *hotkey))matcher { + //explicitly unregister the hotkey, since relying on the unregistration in -dealloc can be problematic + @autoreleasepool { + NSSet *matches = [self hotKeysMatching:matcher]; + for (DDHotKey *hotKey in matches) { + [self unregisterHotKey:hotKey]; + } + } +} + +- (void)unregisterHotKeysWithTarget:(id)target { + [self unregisterHotKeysMatching:^BOOL(DDHotKey *hotkey) { + return hotkey.target == target; + }]; +} + +- (void)unregisterHotKeysWithTarget:(id)target action:(SEL)action { + [self unregisterHotKeysMatching:^BOOL(DDHotKey *hotkey) { + return hotkey.target == target && sel_isEqual(hotkey.action, action); + }]; +} + +- (void)unregisterHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags { + [self unregisterHotKeysMatching:^BOOL(DDHotKey *hotkey) { + return hotkey.keyCode == keyCode && hotkey.modifierFlags == flags; + }]; +} + +- (void)unregisterAllHotKeys { + NSSet *keys = [_registeredHotKeys copy]; + for (DDHotKey *key in keys) { + [self unregisterHotKey:key]; + } +} + +- (NSSet *)registeredHotKeys { + return [self hotKeysMatching:^BOOL(DDHotKey *hotkey) { + return hotkey.hotKeyRef != NULL; + }]; +} + +@end + +OSStatus dd_hotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) { + @autoreleasepool { + EventHotKeyID hotKeyID; + GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID), NULL, &hotKeyID); + + UInt32 keyID = hotKeyID.id; + + NSSet *matchingHotKeys = [[DDHotKeyCenter sharedHotKeyCenter] hotKeysMatching:^BOOL(DDHotKey *hotkey) { + return hotkey.hotKeyID == keyID; + }]; + if ([matchingHotKeys count] > 1) { NSLog(@"ERROR!"); } + DDHotKey *matchingHotKey = [matchingHotKeys anyObject]; + + NSEvent *event = [NSEvent eventWithEventRef:theEvent]; + NSEvent *keyEvent = [NSEvent keyEventWithType:NSKeyUp + location:[event locationInWindow] + modifierFlags:[event modifierFlags] + timestamp:[event timestamp] + windowNumber:-1 + context:nil + characters:@"" + charactersIgnoringModifiers:@"" + isARepeat:NO + keyCode:[matchingHotKey keyCode]]; + + [matchingHotKey invokeWithEvent:keyEvent]; + } + + return noErr; +} diff --git a/DDHotKey/DDHotKeyTextField.h b/DDHotKey/DDHotKeyTextField.h new file mode 100644 index 0000000..c399d62 --- /dev/null +++ b/DDHotKey/DDHotKeyTextField.h @@ -0,0 +1,20 @@ +/* + DDHotKey -- DDHotKeyTextField.h + + Copyright (c) Dave DeLong + + Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + + The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. + */ + +#import +#import "DDHotKeyCenter.h" + +@interface DDHotKeyTextField : NSTextField + +@property (nonatomic, strong) DDHotKey *hotKey; + +@end + +@interface DDHotKeyTextFieldCell : NSTextFieldCell @end \ No newline at end of file diff --git a/DDHotKey/DDHotKeyTextField.m b/DDHotKey/DDHotKeyTextField.m new file mode 100644 index 0000000..4f3da2b --- /dev/null +++ b/DDHotKey/DDHotKeyTextField.m @@ -0,0 +1,138 @@ +/* + DDHotKey -- DDHotKeyTextField.m + + Copyright (c) Dave DeLong + + Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + + The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. + */ + +#import + +#import "DDHotKeyTextField.h" +#import "DDHotKeyUtilities.h" + +@interface DDHotKeyTextFieldEditor : NSTextView + +@property (nonatomic, weak) DDHotKeyTextField *hotKeyField; + +@end + +static DDHotKeyTextFieldEditor *DDFieldEditor(void); +static DDHotKeyTextFieldEditor *DDFieldEditor(void) { + static DDHotKeyTextFieldEditor *editor; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + editor = [[DDHotKeyTextFieldEditor alloc] initWithFrame:NSMakeRect(0, 0, 100, 32)]; + [editor setFieldEditor:YES]; + }); + return editor; +} + +@implementation DDHotKeyTextFieldCell + +- (NSTextView *)fieldEditorForView:(NSView *)view { + if ([view isKindOfClass:[DDHotKeyTextField class]]) { + DDHotKeyTextFieldEditor *editor = DDFieldEditor(); + editor.insertionPointColor = editor.backgroundColor; + editor.hotKeyField = (DDHotKeyTextField *)view; + return editor; + } + return nil; +} + +@end + +@implementation DDHotKeyTextField + ++ (Class)cellClass { + return [DDHotKeyTextFieldCell class]; +} + +- (void)setHotKey:(DDHotKey *)hotKey { + if (_hotKey != hotKey) { + _hotKey = hotKey; + [super setStringValue:[DDStringFromKeyCode(hotKey.keyCode, hotKey.modifierFlags) uppercaseString]]; + } +} + +- (void)setStringValue:(NSString *)aString { + NSLog(@"-[DDHotKeyTextField setStringValue:] is not what you want. Use -[DDHotKeyTextField setHotKey:] instead."); + [super setStringValue:aString]; +} + +- (NSString *)stringValue { + NSLog(@"-[DDHotKeyTextField stringValue] is not what you want. Use -[DDHotKeyTextField hotKey] instead."); + return [super stringValue]; +} + +@end + +@implementation DDHotKeyTextFieldEditor { + BOOL _hasSeenKeyDown; + id _globalMonitor; + DDHotKey *_originalHotKey; +} + +- (void)setHotKeyField:(DDHotKeyTextField *)hotKeyField { + _hotKeyField = hotKeyField; + _originalHotKey = _hotKeyField.hotKey; +} + +- (void)processHotkeyEvent:(NSEvent *)event { + NSUInteger flags = event.modifierFlags; + BOOL hasModifier = (flags & (NSCommandKeyMask | NSAlternateKeyMask | NSControlKeyMask | NSShiftKeyMask | NSFunctionKeyMask)) > 0; + + if (event.type == NSKeyDown) { + _hasSeenKeyDown = YES; + unichar character = [event.charactersIgnoringModifiers characterAtIndex:0]; + + + if (hasModifier == NO && ([[NSCharacterSet newlineCharacterSet] characterIsMember:character] || event.keyCode == kVK_Escape)) { + if (event.keyCode == kVK_Escape) { + self.hotKeyField.hotKey = _originalHotKey; + + NSString *str = DDStringFromKeyCode(_originalHotKey.keyCode, _originalHotKey.modifierFlags); + self.textStorage.mutableString.string = [str uppercaseString]; + } + [self.hotKeyField sendAction:self.hotKeyField.action to:self.hotKeyField.target]; + [self.window makeFirstResponder:nil]; + return; + } + } + + if ((event.type == NSKeyDown || (event.type == NSFlagsChanged && _hasSeenKeyDown == NO)) && hasModifier) { + self.hotKeyField.hotKey = [DDHotKey hotKeyWithKeyCode:event.keyCode modifierFlags:flags task:_originalHotKey.task]; + NSString *str = DDStringFromKeyCode(event.keyCode, flags); + [self.textStorage.mutableString setString:[str uppercaseString]]; + [self.hotKeyField sendAction:self.hotKeyField.action to:self.hotKeyField.target]; + } +} + +- (BOOL)becomeFirstResponder { + BOOL ok = [super becomeFirstResponder]; + if (ok) { + _hasSeenKeyDown = NO; + _globalMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:(NSKeyDownMask | NSFlagsChangedMask) handler:^NSEvent*(NSEvent *event){ + [self processHotkeyEvent:event]; + return nil; + }]; + } + return ok; +} + +- (BOOL)resignFirstResponder { + BOOL ok = [super resignFirstResponder]; + if (ok) { + self.hotKeyField = nil; + if (_globalMonitor) { + [NSEvent removeMonitor:_globalMonitor]; + _globalMonitor = nil; + } + } + + return ok; +} + +@end diff --git a/DDHotKey/DDHotKeyUtilities.h b/DDHotKey/DDHotKeyUtilities.h new file mode 100644 index 0000000..54b25a4 --- /dev/null +++ b/DDHotKey/DDHotKeyUtilities.h @@ -0,0 +1,14 @@ +/* + DDHotKey -- DDHotKeyUtilities.h + + Copyright (c) Dave DeLong + + Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + + The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. + */ + +#import + +extern NSString *DDStringFromKeyCode(unsigned short keyCode, NSUInteger modifiers); +extern UInt32 DDCarbonModifierFlagsFromCocoaModifiers(NSUInteger flags); diff --git a/DDHotKey/DDHotKeyUtilities.m b/DDHotKey/DDHotKeyUtilities.m new file mode 100644 index 0000000..f298a43 --- /dev/null +++ b/DDHotKey/DDHotKeyUtilities.m @@ -0,0 +1,144 @@ +/* + DDHotKey -- DDHotKeyUtilities.m + + Copyright (c) Dave DeLong + + Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + + The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. + */ + +#import "DDHotKeyUtilities.h" +#import + +static NSDictionary *_DDKeyCodeToCharacterMap(void); +static NSDictionary *_DDKeyCodeToCharacterMap(void) { + static NSDictionary *keyCodeMap = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + keyCodeMap = @{ + @(kVK_Return) : @"↩", + @(kVK_Tab) : @"⇥", + @(kVK_Space) : @"⎵", + @(kVK_Delete) : @"⌫", + @(kVK_Escape) : @"⎋", + @(kVK_Command) : @"⌘", + @(kVK_Shift) : @"⇧", + @(kVK_CapsLock) : @"⇪", + @(kVK_Option) : @"⌥", + @(kVK_Control) : @"⌃", + @(kVK_RightShift) : @"⇧", + @(kVK_RightOption) : @"⌥", + @(kVK_RightControl) : @"⌃", + @(kVK_VolumeUp) : @"🔊", + @(kVK_VolumeDown) : @"🔈", + @(kVK_Mute) : @"🔇", + @(kVK_Function) : @"\u2318", + @(kVK_F1) : @"F1", + @(kVK_F2) : @"F2", + @(kVK_F3) : @"F3", + @(kVK_F4) : @"F4", + @(kVK_F5) : @"F5", + @(kVK_F6) : @"F6", + @(kVK_F7) : @"F7", + @(kVK_F8) : @"F8", + @(kVK_F9) : @"F9", + @(kVK_F10) : @"F10", + @(kVK_F11) : @"F11", + @(kVK_F12) : @"F12", + @(kVK_F13) : @"F13", + @(kVK_F14) : @"F14", + @(kVK_F15) : @"F15", + @(kVK_F16) : @"F16", + @(kVK_F17) : @"F17", + @(kVK_F18) : @"F18", + @(kVK_F19) : @"F19", + @(kVK_F20) : @"F20", + // @(kVK_Help) : @"", + @(kVK_ForwardDelete) : @"⌦", + @(kVK_Home) : @"↖", + @(kVK_End) : @"↘", + @(kVK_PageUp) : @"⇞", + @(kVK_PageDown) : @"⇟", + @(kVK_LeftArrow) : @"←", + @(kVK_RightArrow) : @"→", + @(kVK_DownArrow) : @"↓", + @(kVK_UpArrow) : @"↑", + }; + }); + return keyCodeMap; +} + +NSString *DDStringFromKeyCode(unsigned short keyCode, NSUInteger modifiers) { + NSMutableString *final = [NSMutableString stringWithString:@""]; + NSDictionary *characterMap = _DDKeyCodeToCharacterMap(); + + if (modifiers & NSControlKeyMask) { + [final appendString:[characterMap objectForKey:@(kVK_Control)]]; + } + if (modifiers & NSAlternateKeyMask) { + [final appendString:[characterMap objectForKey:@(kVK_Option)]]; + } + if (modifiers & NSShiftKeyMask) { + [final appendString:[characterMap objectForKey:@(kVK_Shift)]]; + } + if (modifiers & NSCommandKeyMask) { + [final appendString:[characterMap objectForKey:@(kVK_Command)]]; + } + + if (keyCode == kVK_Control || keyCode == kVK_Option || keyCode == kVK_Shift || keyCode == kVK_Command) { + return final; + } + + NSString *mapped = [characterMap objectForKey:@(keyCode)]; + if (mapped != nil) { + [final appendString:mapped]; + } else { + + TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); + CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData); + + // Fix crash using non-unicode layouts, such as Chinese or Japanese. + if (!uchr) { + CFRelease(currentKeyboard); + currentKeyboard = TISCopyCurrentASCIICapableKeyboardLayoutInputSource(); + uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData); + } + + const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr); + + if (keyboardLayout) { + UInt32 deadKeyState = 0; + UniCharCount maxStringLength = 255; + UniCharCount actualStringLength = 0; + UniChar unicodeString[maxStringLength]; + + UInt32 keyModifiers = DDCarbonModifierFlagsFromCocoaModifiers(modifiers); + + OSStatus status = UCKeyTranslate(keyboardLayout, + keyCode, kUCKeyActionDown, keyModifiers, + LMGetKbdType(), 0, + &deadKeyState, + maxStringLength, + &actualStringLength, unicodeString); + + if (actualStringLength > 0 && status == noErr) { + NSString *characterString = [NSString stringWithCharacters:unicodeString length:(NSUInteger)actualStringLength]; + + [final appendString:characterString]; + } + } + } + + return final; +} + +UInt32 DDCarbonModifierFlagsFromCocoaModifiers(NSUInteger flags) { + UInt32 newFlags = 0; + if ((flags & NSControlKeyMask) > 0) { newFlags |= controlKey; } + if ((flags & NSCommandKeyMask) > 0) { newFlags |= cmdKey; } + if ((flags & NSShiftKeyMask) > 0) { newFlags |= shiftKey; } + if ((flags & NSAlternateKeyMask) > 0) { newFlags |= optionKey; } + if ((flags & NSAlphaShiftKeyMask) > 0) { newFlags |= alphaLock; } + return newFlags; +} diff --git a/Low Battery Yup d/AppDelegate.h b/Low Battery Yup d/AppDelegate.h index ab9467b..10f6e4b 100644 --- a/Low Battery Yup d/AppDelegate.h +++ b/Low Battery Yup d/AppDelegate.h @@ -10,6 +10,6 @@ @interface AppDelegate : NSObject -@property (assign) IBOutlet NSWindow *window; +- (void)dismissLowBatteryWarning:(NSEvent *)hotKeyEvent; @end diff --git a/Low Battery Yup d/AppDelegate.m b/Low Battery Yup d/AppDelegate.m index 0a7d2a3..51c1626 100644 --- a/Low Battery Yup d/AppDelegate.m +++ b/Low Battery Yup d/AppDelegate.m @@ -7,7 +7,9 @@ // #import "AppDelegate.h" +#import "DDHotKeyCenter.h" #import "Mouse.h" +#import @implementation AppDelegate @@ -17,6 +19,16 @@ } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification +{ + DDHotKeyCenter *hotkey_center = [DDHotKeyCenter sharedHotKeyCenter]; + [hotkey_center registerHotKeyWithKeyCode:kVK_ANSI_0 + modifierFlags:(NSCommandKeyMask | NSAlternateKeyMask | NSShiftKeyMask | NSControlKeyMask) + target:self + action:@selector(dismissLowBatteryWarning:) + object:nil]; +} + +- (void)dismissLowBatteryWarning:(NSEvent *)hotKeyEvent { Mouse *m = [[Mouse alloc] init]; [m moveToLowBatteryOK]; diff --git a/Low Battery Yup d/main.m b/Low Battery Yup d/main.m index a58a570..dbd48d9 100644 --- a/Low Battery Yup d/main.m +++ b/Low Battery Yup d/main.m @@ -7,8 +7,12 @@ // #import +#import "AppDelegate.h" int main(int argc, char *argv[]) { - return NSApplicationMain(argc, (const char **)argv); + AppDelegate *delegate = [[AppDelegate alloc] init]; + [[NSApplication sharedApplication] setDelegate:delegate]; + [NSApp run]; + return EXIT_SUCCESS; } diff --git a/Low Battery Yup.d.xcodeproj/project.pbxproj b/Low Battery Yup.d.xcodeproj/project.pbxproj index 89d2710..c9f332c 100644 --- a/Low Battery Yup.d.xcodeproj/project.pbxproj +++ b/Low Battery Yup.d.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + D123F5A81DDF9D2400A27B7A /* DDHotKeyCenter.m in Sources */ = {isa = PBXBuildFile; fileRef = D123F5A71DDF9D2400A27B7A /* DDHotKeyCenter.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; D18C94A31DDC33CF00E03F87 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D18C94A21DDC33CF00E03F87 /* Cocoa.framework */; }; D18C94AD1DDC33CF00E03F87 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = D18C94AB1DDC33CF00E03F87 /* InfoPlist.strings */; }; D18C94AF1DDC33CF00E03F87 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D18C94AE1DDC33CF00E03F87 /* main.m */; }; @@ -15,6 +16,8 @@ D18C94C51DDC355400E03F87 /* Mouse.m in Sources */ = {isa = PBXBuildFile; fileRef = D18C94C41DDC355400E03F87 /* Mouse.m */; }; D195AE5F1DDC776000A53A18 /* MASShortcut.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D195AE5A1DDC774300A53A18 /* MASShortcut.framework */; }; D195AE601DDC776F00A53A18 /* MASShortcut.framework in Resources */ = {isa = PBXBuildFile; fileRef = D195AE5A1DDC774300A53A18 /* MASShortcut.framework */; }; + D1A37AEE1DE0012C0022434D /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1A37AED1DE0012C0022434D /* Carbon.framework */; }; + D1A37AF11DE001770022434D /* DDHotKeyUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = D1A37AF01DE001770022434D /* DDHotKeyUtilities.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; D1F809E11DDC6AA0001671E9 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D18C94A21DDC33CF00E03F87 /* Cocoa.framework */; }; D1F809E71DDC6AA0001671E9 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = D1F809E51DDC6AA0001671E9 /* InfoPlist.strings */; }; D1F809E91DDC6AA0001671E9 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D1F809E81DDC6AA0001671E9 /* main.m */; }; @@ -48,6 +51,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + D123F5A61DDF9D2400A27B7A /* DDHotKeyCenter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DDHotKeyCenter.h; path = DDHotKey/DDHotKeyCenter.h; sourceTree = ""; }; + D123F5A71DDF9D2400A27B7A /* DDHotKeyCenter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DDHotKeyCenter.m; path = DDHotKey/DDHotKeyCenter.m; sourceTree = ""; }; D18C949E1DDC33CF00E03F87 /* Low Battery Yup.d.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Low Battery Yup.d.app"; sourceTree = BUILT_PRODUCTS_DIR; }; D18C94A21DDC33CF00E03F87 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; D18C94A51DDC33CF00E03F87 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; @@ -63,6 +68,9 @@ D18C94C31DDC355400E03F87 /* Mouse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Mouse.h; sourceTree = ""; }; D18C94C41DDC355400E03F87 /* Mouse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Mouse.m; sourceTree = ""; }; D195AE4F1DDC774200A53A18 /* MASShortcut.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = MASShortcut.xcodeproj; path = MASShortcut/MASShortcut.xcodeproj; sourceTree = ""; }; + D1A37AED1DE0012C0022434D /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; }; + D1A37AEF1DE001770022434D /* DDHotKeyUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DDHotKeyUtilities.h; path = DDHotKey/DDHotKeyUtilities.h; sourceTree = ""; }; + D1A37AF01DE001770022434D /* DDHotKeyUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DDHotKeyUtilities.m; path = DDHotKey/DDHotKeyUtilities.m; sourceTree = ""; }; D1F809DF1DDC6AA0001671E9 /* Low Battery Yup.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Low Battery Yup.app"; sourceTree = BUILT_PRODUCTS_DIR; }; D1F809E41DDC6AA0001671E9 /* Low Battery Yup-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Low Battery Yup-Info.plist"; sourceTree = ""; }; D1F809E61DDC6AA0001671E9 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; @@ -79,6 +87,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + D1A37AEE1DE0012C0022434D /* Carbon.framework in Frameworks */, D18C94A31DDC33CF00E03F87 /* Cocoa.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -95,9 +104,21 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + D123F5A91DDF9D2B00A27B7A /* DDHotKey */ = { + isa = PBXGroup; + children = ( + D123F5A61DDF9D2400A27B7A /* DDHotKeyCenter.h */, + D123F5A71DDF9D2400A27B7A /* DDHotKeyCenter.m */, + D1A37AEF1DE001770022434D /* DDHotKeyUtilities.h */, + D1A37AF01DE001770022434D /* DDHotKeyUtilities.m */, + ); + name = DDHotKey; + sourceTree = ""; + }; D18C94931DDC33CE00E03F87 = { isa = PBXGroup; children = ( + D123F5A91DDF9D2B00A27B7A /* DDHotKey */, D18C94A81DDC33CF00E03F87 /* Low Battery Yup d */, D1F809E21DDC6AA0001671E9 /* Low Battery Yup */, D18C94A11DDC33CF00E03F87 /* Frameworks */, @@ -118,6 +139,7 @@ isa = PBXGroup; children = ( D18C94A21DDC33CF00E03F87 /* Cocoa.framework */, + D1A37AED1DE0012C0022434D /* Carbon.framework */, D18C94A41DDC33CF00E03F87 /* Other Frameworks */, ); name = Frameworks; @@ -321,6 +343,8 @@ D18C94AF1DDC33CF00E03F87 /* main.m in Sources */, D18C94B61DDC33CF00E03F87 /* AppDelegate.m in Sources */, D18C94C51DDC355400E03F87 /* Mouse.m in Sources */, + D123F5A81DDF9D2400A27B7A /* DDHotKeyCenter.m in Sources */, + D1A37AF11DE001770022434D /* DDHotKeyUtilities.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; -- cgit v1.2.3