Hotkey is stored in defaults

This commit is contained in:
michael starke
2014-08-08 18:34:38 +02:00
parent a4edd4a5c2
commit 55d5695676
6 changed files with 65 additions and 17 deletions

View File

@@ -11,6 +11,6 @@
@interface DDHotKey (Keydata) @interface DDHotKey (Keydata)
- (NSData *)keyData; - (NSData *)keyData;
- (instancetype)initWithKeyData:(NSData *)data taks:(DDHotKeyTask)task;
- (instancetype)initWithKeyData:(NSData *)data; - (instancetype)initWithKeyData:(NSData *)data;
@end @end

View File

@@ -11,10 +11,19 @@
@implementation DDHotKey (Keydata) @implementation DDHotKey (Keydata)
- (instancetype)initWithKeyData:(NSData *)data { - (instancetype)initWithKeyData:(NSData *)data {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; self = [self initWithKeyData:data taks:nil];
unsigned short keyCode = [unarchiver decodeIntForKey:NSStringFromSelector(@selector(keyCode))]; return self;
NSUInteger modiferFlags = [unarchiver decodeIntegerForKey:NSStringFromSelector(@selector(modifierFlags))]; }
self = [DDHotKey hotKeyWithKeyCode:keyCode modifierFlags:modiferFlags task:nil];
- (instancetype)initWithKeyData:(NSData *)data taks:(DDHotKeyTask)task{
NSUInteger modifierFlags;
unsigned short keyCode;
if([self _getKeyCode:&keyCode modifierFlags:&modifierFlags fromData:data]) {
self = [DDHotKey hotKeyWithKeyCode:keyCode modifierFlags:modifierFlags task:task];
}
else {
self = nil;
}
return self; return self;
} }
@@ -27,4 +36,14 @@
return [data copy]; return [data copy];
} }
- (BOOL)_getKeyCode:(unsigned short *)keyCode modifierFlags:(NSUInteger *)modifierFlags fromData:(NSData *)data {
if(keyCode == NULL || modifierFlags == NULL || data == nil) {
return NO;
}
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
*keyCode = [unarchiver decodeIntForKey:NSStringFromSelector(@selector(keyCode))];
*modifierFlags = [unarchiver decodeIntegerForKey:NSStringFromSelector(@selector(modifierFlags))];
return YES;
}
@end @end

View File

@@ -8,7 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@class KPKEntry; @class DDHotKey;
/** /**
* The autotype daemon is repsonsible for registering the globa hotkey and to perform any autotype actions * The autotype daemon is repsonsible for registering the globa hotkey and to perform any autotype actions
@@ -17,6 +17,7 @@
@property (strong) IBOutlet NSWindow *matchSelectionWindow; @property (strong) IBOutlet NSWindow *matchSelectionWindow;
@property (weak) IBOutlet NSPopUpButton *matchSelectionButton; @property (weak) IBOutlet NSPopUpButton *matchSelectionButton;
@property (readonly, strong) DDHotKey *registredHotKey;
- (IBAction)executeAutotypeWithSelectedMatch:(id)sender; - (IBAction)executeAutotypeWithSelectedMatch:(id)sender;
- (IBAction)cancelAutotypeSelection:(id)sender; - (IBAction)cancelAutotypeSelection:(id)sender;

View File

@@ -21,6 +21,7 @@
#import "KPKEntry.h" #import "KPKEntry.h"
#import "DDHotKeyCenter.h" #import "DDHotKeyCenter.h"
#import "DDHotKey+Keydata.h"
#import <Carbon/Carbon.h> #import <Carbon/Carbon.h>
NSString *const kMPWindowTitleKey = @"windowTitle"; NSString *const kMPWindowTitleKey = @"windowTitle";
@@ -28,12 +29,14 @@ NSString *const kMPApplciationNameKey = @"applicationName";
/* /*
Enable to activate autotype Enable to activate autotype
*/
#define MP_AUTOTYPE #define MP_AUTOTYPE
*/
@interface MPAutotypeDaemon () @interface MPAutotypeDaemon ()
@property (nonatomic, assign) BOOL enabled; @property (nonatomic, assign) BOOL enabled;
@property (nonatomic, copy) NSData *hotKeyData;
@property (strong) DDHotKey *registredHotKey;
@property (copy) NSString *targetApplicationName; @property (copy) NSString *targetApplicationName;
@property (copy) NSString *targetWindowTitle; @property (copy) NSString *targetWindowTitle;
@@ -52,12 +55,18 @@ NSString *const kMPApplciationNameKey = @"applicationName";
toObject:[NSUserDefaultsController sharedUserDefaultsController] toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEnableGlobalAutotype] withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEnableGlobalAutotype]
options:nil]; options:nil];
[self bind:NSStringFromSelector(@selector(hotKeyData))
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyGlobalAutotypeKeyDataKey]
options:nil];
} }
return self; return self;
} }
- (void)dealloc { - (void)dealloc {
[self unbind:NSStringFromSelector(@selector(enabled))]; [self unbind:NSStringFromSelector(@selector(enabled))];
[self unbind:NSStringFromSelector(@selector(hotKeyData))];
} }
#pragma mark - #pragma mark -
@@ -72,6 +81,21 @@ NSString *const kMPApplciationNameKey = @"applicationName";
} }
} }
- (void)setHotKeyData:(NSData *)hotKeyData {
if(![_hotKeyData isEqualToData:hotKeyData]) {
#ifdef MP_AUTOTYPE
[self _unregisterHotKey];
#endif
_hotKeyData = [hotKeyData copy];
#ifdef MP_AUTOTYPE
if(self.enabled) {
[self _registerHotKey];
}
#endif
}
}
#pragma mark - #pragma mark -
#pragma mark Actions #pragma mark Actions
@@ -100,12 +124,12 @@ NSString *const kMPApplciationNameKey = @"applicationName";
if(useCurrentWindowAndApplication) { if(useCurrentWindowAndApplication) {
[self _updateTargetApplicationAndWindow]; [self _updateTargetApplicationAndWindow];
} }
MPDocument *document = [self _findAutotypeDocument]; MPDocument *document = [self _findAutotypeDocument];
if(!document) { if(!document) {
return; // nothing to do return; // nothing to do
} }
MPAutotypeContext *context = [self _autotypeContextInDocument:document forWindowTitle:self.targetWindowTitle]; MPAutotypeContext *context = [self _autotypeContextInDocument:document forWindowTitle:self.targetWindowTitle];
[self _performAutotypeForContext:context]; [self _performAutotypeForContext:context];
} }
@@ -167,15 +191,19 @@ NSString *const kMPApplciationNameKey = @"applicationName";
#pragma mark Hotkey Registration #pragma mark Hotkey Registration
- (void)_registerHotKey { - (void)_registerHotKey {
[[DDHotKeyCenter sharedHotKeyCenter] registerHotKeyWithKeyCode:kVK_ANSI_M if(nil == self.hotKeyData) {
modifierFlags:(NSCommandKeyMask | NSAlternateKeyMask ) return; // No hotkey data defined, so nothign to do
target:self }
action:@selector(_didPressHotKey) __weak MPAutotypeDaemon *welf = self;
object:nil]; DDHotKey *storedHotkey = [[DDHotKey alloc] initWithKeyData:self.hotKeyData taks:^(NSEvent *event) {
[welf _didPressHotKey];
}];
self.registredHotKey = [[DDHotKeyCenter sharedHotKeyCenter] registerHotKey:storedHotkey];
} }
- (void)_unregisterHotKey { - (void)_unregisterHotKey {
[[DDHotKeyCenter sharedHotKeyCenter] unregisterHotKeysWithTarget:self action:@selector(_didPressHotKey)]; [[DDHotKeyCenter sharedHotKeyCenter] unregisterHotKey:self.registredHotKey];
self.registredHotKey = nil;
} }
- (NSDictionary *)_frontMostApplicationInfoDict { - (NSDictionary *)_frontMostApplicationInfoDict {

View File

@@ -49,7 +49,7 @@ APPKIT_EXTERN NSString *const kMPSettingsKeyRememberKeyFilesForDatabases;
/* Autotype */ /* Autotype */
APPKIT_EXTERN NSString *const kMPSettingsKeySendCommandForControlKey; APPKIT_EXTERN NSString *const kMPSettingsKeySendCommandForControlKey;
APPKIT_EXTERN NSString *const kMPSettingsKeyEnableGlobalAutotype; APPKIT_EXTERN NSString *const kMPSettingsKeyEnableGlobalAutotype;
APPKIT_EXTERN NSString *const kMPSettingsKeyGlobalAutotypeKeyKey; APPKIT_EXTERN NSString *const kMPSettingsKeyGlobalAutotypeKeyDataKey;
APPKIT_EXTERN NSString *const kMPSettingsKeyDocumentsAutotypeFixNoteWasShown; APPKIT_EXTERN NSString *const kMPSettingsKeyDocumentsAutotypeFixNoteWasShown;
/* Search */ /* Search */

View File

@@ -35,7 +35,7 @@ NSString *const kMPSettingsKeyRememberKeyFilesForDatabases = @"Rememb
NSString *const kMPSettingsKeySendCommandForControlKey = @"SendCommandKeyForControlKey"; NSString *const kMPSettingsKeySendCommandForControlKey = @"SendCommandKeyForControlKey";
NSString *const kMPSettingsKeyEnableGlobalAutotype = @"EnableGlobalAutotype"; NSString *const kMPSettingsKeyEnableGlobalAutotype = @"EnableGlobalAutotype";
NSString *const kMPSettingsKeyGlobalAutotypeKeyKey = @"GlobalAutotypeKey"; NSString *const kMPSettingsKeyGlobalAutotypeKeyDataKey = @"GlobalAutotypeKeyDataKey";
NSString *const kMPSettingsKeyDocumentsAutotypeFixNoteWasShown = @"DocumentsAutotypeFixNoteWasShown"; NSString *const kMPSettingsKeyDocumentsAutotypeFixNoteWasShown = @"DocumentsAutotypeFixNoteWasShown";
NSString *const kMPSettingsKeyEntrySearchFilterMode = @"EntrySearchFilterMode"; NSString *const kMPSettingsKeyEntrySearchFilterMode = @"EntrySearchFilterMode";