aboutsummaryrefslogtreecommitdiffstats
path: root/DDHotKey/DDHotKeyTextField.m
diff options
context:
space:
mode:
authorTeddy Wing2016-11-18 22:54:19 -0500
committerTeddy Wing2016-11-18 22:54:19 -0500
commit678f8ca1a961eb9125aa82b7d2277fda4db7f146 (patch)
treeaad4444f63e18aa9b8e10e05874f0612c4f93e7a /DDHotKey/DDHotKeyTextField.m
parent7677b59543a8d2edc9a28f77f26ebb02492e2b5c (diff)
downloadLow-Battery-Yup-678f8ca1a961eb9125aa82b7d2277fda4db7f146.tar.bz2
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.
Diffstat (limited to 'DDHotKey/DDHotKeyTextField.m')
-rw-r--r--DDHotKey/DDHotKeyTextField.m138
1 files changed, 138 insertions, 0 deletions
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 <http://www.davedelong.com>
+
+ 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 <Carbon/Carbon.h>
+
+#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