Collection of modifiers and subsequent key presses implemented

Extended tests for command creation
This commit is contained in:
michael starke
2014-03-21 01:56:45 +01:00
parent 6446e2ab94
commit 21ffd01f2b
4 changed files with 64 additions and 35 deletions

View File

@@ -107,21 +107,35 @@
} }
}]; }];
NSUInteger lastLocation = 0; NSUInteger lastLocation = 0;
MPAutotypeKeyPress *keyPress; CGEventFlags collectedModifers = 0;
for(NSValue *rangeValue in commandRanges) { for(NSValue *rangeValue in commandRanges) {
NSRange commandRange = [rangeValue rangeValue]; NSRange commandRange = [rangeValue rangeValue];
/* All non-commands will get translated into paste commands */ /* All non-commands will get translated into paste commands */
if(commandRange.location > lastLocation) { if(commandRange.location > lastLocation) {
/* If there were modifiers we need to use the next single stroke and make update the modifier command */ /* If there were modifiers we need to use the next single stroke and make update the modifier command */
if(keyPress) { if(collectedModifers) {
NSString *modifiedKey = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, 1)];
MPAutotypeKeyPress *press = [[MPAutotypeKeyPress alloc] initWithModifierMask:collectedModifers character:modifiedKey];
if(press) {
[commands addObject:press];
} }
collectedModifers = 0;
lastLocation++;
}
NSRange pasteRange = NSMakeRange(lastLocation, commandRange.location - lastLocation);
if(pasteRange.length > 0) {
NSString *pasteValue = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, commandRange.location - lastLocation)]; NSString *pasteValue = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, commandRange.location - lastLocation)];
// Determin if it's amodifier key, and collect them! // Determin if it's amodifier key, and collect them!
[self appendPasteCommandForContent:pasteValue toCommands:commands]; [self appendPasteCommandForContent:pasteValue toCommands:commands];
} }
}
/* Test for modifer Key */
NSString *commandString = [context.evaluatedCommand substringWithRange:commandRange]; NSString *commandString = [context.evaluatedCommand substringWithRange:commandRange];
[self appendCommandForString:commandString toCommands:commands keyPressCommand:&keyPress]; /* append commands for non-modifer keys */
if(![self updateModifierMask:&collectedModifers forCommand:commandString]) {
[self appendCommandForString:commandString toCommands:commands activeModifer:collectedModifers];
collectedModifers = 0; // Reset the modifers;
}
lastLocation = commandRange.location + commandRange.length; lastLocation = commandRange.location + commandRange.length;
} }
return commands; return commands;
@@ -134,42 +148,31 @@
} }
} }
+ (void)appendCommandForString:(NSString *)commandString toCommands:(NSMutableArray *)commands keyPressCommand:(MPAutotypeKeyPress **)keyPress { + (void)appendCommandForString:(NSString *)commandString toCommands:(NSMutableArray *)commands activeModifer:(CGEventFlags)flags {
if(!commandString) { if(!commandString) {
return; return;
} }
NSNumber *flagNumber = [self modifierCommands][commandString];
NSNumber *keyCodeNumber = [self keypressCommands][commandString]; NSNumber *keyCodeNumber = [self keypressCommands][commandString];
/* modifier key */ if(keyCodeNumber) {
if(flagNumber) { CGKeyCode keyCode = [keyCodeNumber keyCodeValue];
if(keyPress == NULL) { [commands addObject:[[MPAutotypeKeyPress alloc] initWithModifierMask:flags keyCode:keyCode]];
return; // We need a pointer to return the keypress! }
}
+ (BOOL)updateModifierMask:(CGEventFlags *)mask forCommand:(NSString *)commandString {
NSAssert(mask != NULL, @"Input pointer missing!");
if(mask == NULL) {
return NO;
}
NSNumber *flagNumber = [self modifierCommands][commandString];
if(!flagNumber) {
return NO; // No modifier key, just leave
} }
CGEventFlags flags = [flagNumber eventFlagsValue]; CGEventFlags flags = [flagNumber eventFlagsValue];
if(*keyPress == nil) { *mask |= flags;
*keyPress = [[MPAutotypeKeyPress alloc] initWithModifierMask:flags keyCode:0]; return YES;
[commands addObject:*keyPress];
}
else {
NSAssert([*keyPress isKindOfClass:[MPAutotypeKeyPress class]], @"Invalid command supplied");
(*keyPress).modifierMask |= flags;
}
}
/* key press */
else if(keyCodeNumber) {
CGKeyCode keyCode = [keyCodeNumber keyCodeValue];
/* we have no modifers collected */
if(keyPress != NULL && keyPress == nil) {
*keyPress = [[MPAutotypeKeyPress alloc] initWithModifierMask:0 keyCode:keyCode];
[commands addObject:*keyPress];
}
/* modifers collected, set keycode */
else if(keyPress) {
(*keyPress).keyCode = keyCode;
}
}
} }
- (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags { - (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags {

View File

@@ -17,5 +17,6 @@
@property (assign) CGKeyCode keyCode; @property (assign) CGKeyCode keyCode;
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code; - (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code;
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask character:(NSString *)character;
@end @end

View File

@@ -20,6 +20,17 @@
return self; return self;
} }
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask character:(NSString *)character {
CGKeyCode mappedKey = [MPKeyMapper keyCodeForCharacter:character];
if(mappedKey == kMPUnknownKeyCode) {
self = nil;
}
else {
self = [self initWithModifierMask:modiferMask keyCode:mappedKey];
}
return self;
}
- (void)execute { - (void)execute {
if(![self isValid]) { if(![self isValid]) {
return; // no valid command. Stop. return; // no valid command. Stop.

View File

@@ -10,6 +10,8 @@
#import "NSString+Commands.h" #import "NSString+Commands.h"
#import "MPAutotypeCommand.h" #import "MPAutotypeCommand.h"
#import "MPAutotypeContext.h" #import "MPAutotypeContext.h"
#import "MPAutotypePaste.h"
#import "MPAutotypeKeyPress.h"
#import "KPKEntry.h" #import "KPKEntry.h"
@@ -47,5 +49,17 @@
MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{USERNAME}{TAB}{PASSWORD}{ENTER}"]; MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{USERNAME}{TAB}{PASSWORD}{ENTER}"];
NSArray *commands = [MPAutotypeCommand commandsForContext:context]; NSArray *commands = [MPAutotypeCommand commandsForContext:context];
XCTAssert([commands count] == 4);
XCTAssert([commands[0] isKindOfClass:[MPAutotypePaste class]]);
XCTAssert([commands[1] isKindOfClass:[MPAutotypeKeyPress class]]);
XCTAssert([commands[2] isKindOfClass:[MPAutotypePaste class]]);
XCTAssert([commands[3] isKindOfClass:[MPAutotypeKeyPress class]]);
context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"^T{USERNAME}%+^{TAB}Whoo{PASSWORD}{ENTER}"];
commands = [MPAutotypeCommand commandsForContext:context];
XCTAssert([commands count] == 5);
} }
@end @end