From e20e6419fc9f3f10913f3f3fa7e83320062aba5f Mon Sep 17 00:00:00 2001 From: michael starke Date: Thu, 21 Aug 2014 00:29:46 +0200 Subject: [PATCH] {DELAY} now get corret values assinged --- MacPass/MPAutotypeCommand.m | 43 ++++++++++++++++----- MacPassTests/KPKTestAutotypeNormalization.m | 3 ++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/MacPass/MPAutotypeCommand.m b/MacPass/MPAutotypeCommand.m index 8a24bd9d..64f73d10 100644 --- a/MacPass/MPAutotypeCommand.m +++ b/MacPass/MPAutotypeCommand.m @@ -166,24 +166,47 @@ + (void)appendCommandForString:(NSString *)commandString toCommands:(NSMutableArray *)commands activeModifer:(CGEventFlags)flags { if(nil == commandString) { - return; + return; // Nothing to parse } - /* TODO: Test for special Commands */ - /* TODO: fall back to paste if nothing matches */ - - NSString *delayPrefix = [[NSString alloc] initWithFormat:@"{%@", kKPKAutotypeDelay]; + /* Simple Special Press */ NSNumber *keyCodeNumber = [self keypressCommands][commandString]; - if(nil != keyCodeNumber) { CGKeyCode keyCode = [keyCodeNumber keyCodeValue]; [commands addObject:[[MPAutotypeKeyPress alloc] initWithModifierMask:flags keyCode:keyCode]]; + return; // Done } - else if([kKPKAutotypeClearField isEqualToString:commandString]) { + /* Clearfield */ + if([kKPKAutotypeClearField isEqualToString:commandString]) { [commands addObject:[[MPAutotypeClear alloc] init]]; + return; // Done } - else if([commandString hasPrefix:delayPrefix]){ - [commands addObject:[[MPAutotypeDelay alloc] initWithDelay:5]]; - /* TODO: find the delay */ + // TODO: add {APPLICATION } + /* Delay */ + NSString *delayPattern = [[NSString alloc] initWithFormat:@"\\{(%@|%@|)[:|=]+([0-9])+\\}", + kKPKAutotypeDelay, + kKPKAutotypeVirtualKey/*, + kKPKAutotypeVirtualExtendedKey, + kKPKAutotypeVirtualNonExtendedKey*/]; + NSRegularExpression *delayRegExp = [[NSRegularExpression alloc] initWithPattern:delayPattern options:NSRegularExpressionCaseInsensitive error:0]; + NSAssert(delayRegExp, @"Regex for delay should work!"); + NSTextCheckingResult *result = [delayRegExp firstMatchInString:commandString options:0 range:NSMakeRange(0, [commandString length])]; + if(result && (result.numberOfRanges == 3)) { + NSString *command = [commandString substringWithRange:[result rangeAtIndex:1]]; + NSString *valueString = [commandString substringWithRange:[result rangeAtIndex:2]]; + NSScanner *numberScanner = [[NSScanner alloc] initWithString:valueString]; + NSInteger value; + if([numberScanner scanInteger:&value]) { + if([kKPKAutotypeDelay isEqualToString:command]) { + [commands addObject:[[MPAutotypeDelay alloc] initWithDelay:value]]; + return; // Done + } + else if([kKPKAutotypeVirtualKey isEqualToString:command]) { + // TODO add key + } + } + else { + NSLog(@"Unable to determine delay!"); + } } else { [self appendPasteCommandForContent:commandString toCommands:commands]; diff --git a/MacPassTests/KPKTestAutotypeNormalization.m b/MacPassTests/KPKTestAutotypeNormalization.m index b601694f..294f60f5 100644 --- a/MacPassTests/KPKTestAutotypeNormalization.m +++ b/MacPassTests/KPKTestAutotypeNormalization.m @@ -56,6 +56,9 @@ XCTAssert([commands[2] isKindOfClass:[MPAutotypePaste class]]); XCTAssert([commands[3] isKindOfClass:[MPAutotypeKeyPress class]]); + context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"{DELAY=5}{VKEY-NX 1}{VKEY-EX 200}{DELAY 5}{VKEY 300}"]; + commands = [MPAutotypeCommand commandsForContext:context]; + context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:@"^T{USERNAME}%+^{TAB}Whoo{PASSWORD}{ENTER}"]; commands = [MPAutotypeCommand commandsForContext:context];