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)
- (NSData *)keyData;
- (instancetype)initWithKeyData:(NSData *)data taks:(DDHotKeyTask)task;
- (instancetype)initWithKeyData:(NSData *)data;
@end

View File

@@ -11,10 +11,19 @@
@implementation DDHotKey (Keydata)
- (instancetype)initWithKeyData:(NSData *)data {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
unsigned short keyCode = [unarchiver decodeIntForKey:NSStringFromSelector(@selector(keyCode))];
NSUInteger modiferFlags = [unarchiver decodeIntegerForKey:NSStringFromSelector(@selector(modifierFlags))];
self = [DDHotKey hotKeyWithKeyCode:keyCode modifierFlags:modiferFlags task:nil];
self = [self initWithKeyData:data taks:nil];
return self;
}
- (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;
}
@@ -27,4 +36,14 @@
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

View File

@@ -8,7 +8,7 @@
#import <Foundation/Foundation.h>
@class KPKEntry;
@class DDHotKey;
/**
* 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 (weak) IBOutlet NSPopUpButton *matchSelectionButton;
@property (readonly, strong) DDHotKey *registredHotKey;
- (IBAction)executeAutotypeWithSelectedMatch:(id)sender;
- (IBAction)cancelAutotypeSelection:(id)sender;

View File

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

View File

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

View File

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