mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-23 18:39:23 +00:00
Extracted AutotypeParser
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@class KPKEntry;
|
||||
|
||||
/**
|
||||
* The Autotype command reperesent a capsualted Action that was determined by interpreting
|
||||
* Autotype field for a given entry. This is a class cluster and schould be considered the sole
|
||||
@@ -16,8 +18,6 @@
|
||||
@interface MPAutotypeCommand : NSObject
|
||||
|
||||
@property (readonly, copy) NSString *commandString;
|
||||
|
||||
+ (NSArray *)commandsForCommandString:(NSString *)commands;
|
||||
/**
|
||||
* Determines the Keycode for the given keyboard layout for the supplied character
|
||||
*
|
||||
@@ -37,9 +37,14 @@
|
||||
*/
|
||||
- (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags;
|
||||
|
||||
/**
|
||||
* Convenience message to be sent for executing a simple paste command
|
||||
*/
|
||||
- (void)sendPasteKeyCode;
|
||||
|
||||
/**
|
||||
* Exectues the Autotype Command. This will be called by the autotype daemon.
|
||||
*/
|
||||
- (void)execute;
|
||||
- (void)executeWithEntry:(KPKEntry *)entry;
|
||||
|
||||
@end
|
||||
|
||||
@@ -11,143 +11,8 @@
|
||||
|
||||
#import <Carbon/Carbon.h>
|
||||
|
||||
/*
|
||||
|
||||
Autotype workflow:
|
||||
|
||||
run copy/paste with content
|
||||
|
||||
special keys:
|
||||
|
||||
Tab {TAB}
|
||||
Enter {ENTER} or ~
|
||||
Arrow Up {UP}
|
||||
Arrow Down {DOWN}
|
||||
Arrow Left {LEFT}
|
||||
Arrow Right {RIGHT}
|
||||
Insert {INSERT} or {INS}
|
||||
Delete {DELETE} or {DEL}
|
||||
Home {HOME}
|
||||
End {END}
|
||||
Page Up {PGUP}
|
||||
Page Down {PGDN}
|
||||
Backspace {BACKSPACE}, {BS} or {BKSP}
|
||||
Break {BREAK}
|
||||
Caps-Lock {CAPSLOCK}
|
||||
Escape {ESC}
|
||||
Windows Key {WIN} (equ. to {LWIN})
|
||||
Windows Key: left, right {LWIN}, {RWIN}
|
||||
Apps / Menu {APPS}
|
||||
Help {HELP}
|
||||
Numlock {NUMLOCK}
|
||||
Print Screen {PRTSC}
|
||||
Scroll Lock {SCROLLLOCK}
|
||||
F1 - F16 {F1} - {F16}
|
||||
Numeric Keypad + {ADD}
|
||||
Numeric Keypad - {SUBTRACT}
|
||||
Numeric Keypad * {MULTIPLY}
|
||||
Numeric Keypad / {DIVIDE}
|
||||
Numeric Keypad 0 to 9 {NUMPAD0} to {NUMPAD9}
|
||||
Shift +
|
||||
Ctrl ^
|
||||
Alt %
|
||||
+ {+}
|
||||
^ {^}
|
||||
% {%}
|
||||
~ {~}
|
||||
(, ) {(}, {)}
|
||||
[, ] {[}, {]}
|
||||
{, } {{}, {}}
|
||||
|
||||
special commands:
|
||||
|
||||
{DELAY X} Delays X milliseconds.
|
||||
{CLEARFIELD} Clears the contents of the edit control that currently has the focus (only single-line edit controls).
|
||||
{VKEY X}
|
||||
*/
|
||||
|
||||
@implementation MPAutotypeCommand
|
||||
|
||||
+ (NSArray *)commandsForCommandString:(NSString *)commands {
|
||||
NSUInteger commandIndex = 0;
|
||||
CGEventFlags modiferKeys = 0;
|
||||
while(commandIndex <= [commands length]) {
|
||||
/* Modifier Keys
|
||||
Shift +
|
||||
Ctrl ^
|
||||
Alt %
|
||||
Enter ~ // No modifier!
|
||||
*/
|
||||
NSString *currentCommans = [commands substringFromIndex:commandIndex];
|
||||
NSCharacterSet *modifierKeySet = [NSCharacterSet characterSetWithCharactersInString:@"+^%"];
|
||||
NSRange modifierRange = [currentCommans rangeOfCharacterFromSet:modifierKeySet options:NSCaseInsensitiveSearch range:NSMakeRange(0, 1)];
|
||||
if(modifierRange.length != 0 && modifierRange.location == 0) {
|
||||
/* starts with a special key */
|
||||
if([currentCommans hasPrefix:@"+"]) {
|
||||
modiferKeys |= kCGEventFlagMaskAlphaShift;
|
||||
}
|
||||
if([currentCommans hasPrefix:@"^"]) {
|
||||
modiferKeys |= kCGEventFlagMaskControl;
|
||||
}
|
||||
if([currentCommans hasPrefix:@"%"]) {
|
||||
modiferKeys = kCGEventFlagMaskAlternate;
|
||||
}
|
||||
/* move the index and continue */
|
||||
commandIndex++;
|
||||
continue;
|
||||
}
|
||||
if([currentCommans hasPrefix:@"{"]) {
|
||||
NSRange closeBracket = [currentCommans rangeOfString:@"}"];
|
||||
if(closeBracket.length == 0) {
|
||||
NSLog(@"Syntax error in Autotype Sequence %@ at index: %ld", commands, commandIndex);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
/* Search on to another bracket or a special key */
|
||||
|
||||
/* Command Keys
|
||||
Tab {TAB}
|
||||
Enter {ENTER} or ~
|
||||
Arrow Up {UP}
|
||||
Arrow Down {DOWN}
|
||||
Arrow Left {LEFT}
|
||||
Arrow Right {RIGHT}
|
||||
Insert {INSERT} or {INS}
|
||||
Delete {DELETE} or {DEL}
|
||||
Home {HOME}
|
||||
End {END}
|
||||
Page Up {PGUP}
|
||||
Page Down {PGDN}
|
||||
Backspace {BACKSPACE}, {BS} or {BKSP}
|
||||
Break {BREAK}
|
||||
Caps-Lock {CAPSLOCK}
|
||||
Escape {ESC}
|
||||
Windows Key {WIN} (equ. to {LWIN})
|
||||
Windows Key: left, right {LWIN}, {RWIN}
|
||||
Apps / Menu {APPS}
|
||||
Help {HELP}
|
||||
Numlock {NUMLOCK}
|
||||
Print Screen {PRTSC}
|
||||
Scroll Lock {SCROLLLOCK}
|
||||
F1 - F16 {F1} - {F16}
|
||||
Numeric Keypad + {ADD}
|
||||
Numeric Keypad - {SUBTRACT}
|
||||
Numeric Keypad * {MULTIPLY}
|
||||
Numeric Keypad / {DIVIDE}
|
||||
Numeric Keypad 0 to 9 {NUMPAD0} to {NUMPAD9}
|
||||
+ {+}
|
||||
^ {^}
|
||||
% {%}
|
||||
~ {~}
|
||||
(, ) {(}, {)}
|
||||
[, ] {[}, {]}
|
||||
{, } {{}, {}}
|
||||
|
||||
*/
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags {
|
||||
CGEventRef pressKey = CGEventCreateKeyboardEvent (NULL, keyCode, YES);
|
||||
CGEventRef releaseKey = CGEventCreateKeyboardEvent (NULL, keyCode, NO);
|
||||
@@ -260,8 +125,8 @@
|
||||
return (CGKeyCode)virtualKeyCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
- (void)execute {}
|
||||
- (void)executeWithEntry:(KPKEntry *)entry {
|
||||
NSAssert(NO, @"Not Implemented");
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
15
MacPass/MPAutotypeParser.h
Normal file
15
MacPass/MPAutotypeParser.h
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// MPAutotypeParser.h
|
||||
// MacPass
|
||||
//
|
||||
// Created by Michael Starke on 28/11/13.
|
||||
// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface MPAutotypeParser : NSObject
|
||||
|
||||
+ (NSArray *)commandsForCommandString:(NSString *)commands;
|
||||
|
||||
@end
|
||||
163
MacPass/MPAutotypeParser.m
Normal file
163
MacPass/MPAutotypeParser.m
Normal file
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// MPAutotypeParser.m
|
||||
// MacPass
|
||||
//
|
||||
// Created by Michael Starke on 28/11/13.
|
||||
// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPAutotypeParser.h"
|
||||
|
||||
#import "NSString+Placeholder.h"
|
||||
|
||||
NSString *const kMPAutotypeKeyShift = @"+";
|
||||
NSString *const kMPAutotypeKeyControl = @"^";
|
||||
NSString *const kMPAutotypeKeyAlt = @"%";
|
||||
NSString *const kMPAutotypeKeyEnter = @"~";
|
||||
NSString *const kMPAutptypeCommandEnter = @"{ENTER}";
|
||||
|
||||
@implementation MPAutotypeParser
|
||||
|
||||
/*
|
||||
Tab {TAB}
|
||||
Enter {ENTER} or ~
|
||||
Arrow Up {UP}
|
||||
Arrow Down {DOWN}
|
||||
Arrow Left {LEFT}
|
||||
Arrow Right {RIGHT}
|
||||
Insert {INSERT} or {INS}
|
||||
Delete {DELETE} or {DEL}
|
||||
Home {HOME}
|
||||
End {END}
|
||||
Page Up {PGUP}
|
||||
Page Down {PGDN}
|
||||
Backspace {BACKSPACE}, {BS} or {BKSP}
|
||||
Break {BREAK}
|
||||
Caps-Lock {CAPSLOCK}
|
||||
Escape {ESC}
|
||||
Windows Key {WIN} (equ. to {LWIN})
|
||||
Windows Key: left, right {LWIN}, {RWIN}
|
||||
Apps / Menu {APPS}
|
||||
Help {HELP}
|
||||
Numlock {NUMLOCK}
|
||||
Print Screen {PRTSC}
|
||||
Scroll Lock {SCROLLLOCK}
|
||||
F1 - F16 {F1} - {F16}
|
||||
Numeric Keypad + {ADD}
|
||||
Numeric Keypad - {SUBTRACT}
|
||||
Numeric Keypad * {MULTIPLY}
|
||||
Numeric Keypad / {DIVIDE}
|
||||
Numeric Keypad 0 to 9 {NUMPAD0} to {NUMPAD9}
|
||||
Shift +
|
||||
Ctrl ^
|
||||
Alt %
|
||||
+ {+}
|
||||
^ {^}
|
||||
% {%}
|
||||
~ {~}
|
||||
(, ) {(}, {)}
|
||||
[, ] {[}, {]}
|
||||
{, } {{}, {}}
|
||||
|
||||
special commands:
|
||||
|
||||
{DELAY X} Delays X milliseconds.
|
||||
{CLEARFIELD} Clears the contents of the edit control that currently has the focus (only single-line edit controls).
|
||||
{VKEY X}
|
||||
*/
|
||||
+ (NSArray *)commandsForCommandString:(NSString *)commands {
|
||||
NSUInteger commandIndex = 0;
|
||||
CGEventFlags modiferKeys = 0;
|
||||
while(commandIndex <= [commands length]) {
|
||||
/* Modifier Keys
|
||||
Shift +
|
||||
Ctrl ^
|
||||
Alt %
|
||||
*/
|
||||
NSString *currentCommands = [commands substringFromIndex:commandIndex];
|
||||
NSCharacterSet *modifierKeySet = [NSCharacterSet characterSetWithCharactersInString:@"+^%"];
|
||||
NSRange modifierRange = [currentCommands rangeOfCharacterFromSet:modifierKeySet options:NSCaseInsensitiveSearch range:NSMakeRange(0, 1)];
|
||||
if(modifierRange.length != 0 && modifierRange.location == 0) {
|
||||
/* starts with a special key */
|
||||
if([currentCommands hasPrefix:kMPAutotypeKeyShift]) {
|
||||
modiferKeys |= kCGEventFlagMaskAlphaShift;
|
||||
}
|
||||
if([currentCommands hasPrefix:kMPAutotypeKeyControl]) {
|
||||
modiferKeys |= kCGEventFlagMaskControl;
|
||||
}
|
||||
if([currentCommands hasPrefix:kMPAutotypeKeyAlt]) {
|
||||
modiferKeys = kCGEventFlagMaskAlternate;
|
||||
}
|
||||
/* move the index and continue */
|
||||
commandIndex++;
|
||||
continue;
|
||||
}
|
||||
if([currentCommands hasPrefix:@"{"]) {
|
||||
/* Commands reset the modifiers */
|
||||
modiferKeys = 0;
|
||||
NSRange closeBracket = [currentCommands rangeOfString:@"}"];
|
||||
if(closeBracket.length == 0) {
|
||||
NSLog(@"Syntax error in Autotype Sequence %@ at index: %ld", commands, commandIndex);
|
||||
return nil;
|
||||
}
|
||||
NSString *singleCommand = [currentCommands substringWithRange:NSMakeRange(0, closeBracket.location)];
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
}
|
||||
/* Search on to another bracket or a special key */
|
||||
|
||||
/* Command Keys
|
||||
Tab {TAB}
|
||||
Enter {ENTER} or ~
|
||||
Arrow Up {UP}
|
||||
Arrow Down {DOWN}
|
||||
Arrow Left {LEFT}
|
||||
Arrow Right {RIGHT}
|
||||
Insert {INSERT} or {INS}
|
||||
Delete {DELETE} or {DEL}
|
||||
Home {HOME}
|
||||
End {END}
|
||||
Page Up {PGUP}
|
||||
Page Down {PGDN}
|
||||
Backspace {BACKSPACE}, {BS} or {BKSP}
|
||||
Break {BREAK}
|
||||
Caps-Lock {CAPSLOCK}
|
||||
Escape {ESC}
|
||||
Windows Key {WIN} (equ. to {LWIN})
|
||||
Windows Key: left, right {LWIN}, {RWIN}
|
||||
Apps / Menu {APPS}
|
||||
Help {HELP}
|
||||
Numlock {NUMLOCK}
|
||||
Print Screen {PRTSC}
|
||||
Scroll Lock {SCROLLLOCK}
|
||||
F1 - F16 {F1} - {F16}
|
||||
Numeric Keypad + {ADD}
|
||||
Numeric Keypad - {SUBTRACT}
|
||||
Numeric Keypad * {MULTIPLY}
|
||||
Numeric Keypad / {DIVIDE}
|
||||
Numeric Keypad 0 to 9 {NUMPAD0} to {NUMPAD9}
|
||||
+ {+}
|
||||
^ {^}
|
||||
% {%}
|
||||
~ {~}
|
||||
(, ) {(}, {)}
|
||||
[, ] {[}, {]}
|
||||
{, } {{}, {}} {LCURL}, {RCURL}
|
||||
|
||||
*/
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSString *)_normalizeCommands:(NSString *)commandString {
|
||||
/* Cache normalized Commands? */
|
||||
NSMutableString *mutableCommand = [commandString mutableCopy];
|
||||
[mutableCommand replaceOccurrencesOfString:kMPAutotypeKeyEnter withString:kMPAutptypeCommandEnter options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableCommand length])];
|
||||
[mutableCommand replaceOccurrencesOfString:@"{{}" withString:@"{LCURL}" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableCommand length])];
|
||||
[mutableCommand replaceOccurrencesOfString:@"{}}" withString:@"{RCURL}" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableCommand length])];
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -9,18 +9,26 @@
|
||||
#import "MPAutotypePaste.h"
|
||||
#import "MPPasteBoardController.h"
|
||||
|
||||
#import <Carbon/Carbon.h>
|
||||
#import "NSString+Placeholder.h"
|
||||
|
||||
@implementation MPAutotypePaste
|
||||
|
||||
/**
|
||||
* Simple copy paste action
|
||||
*/
|
||||
- (void)execute {
|
||||
- (void)executeWithEntry:(KPKEntry *)entry {
|
||||
if([self.commandString length] > 0) {
|
||||
MPPasteBoardController *controller = [MPPasteBoardController defaultController];
|
||||
[controller copyObjects:@[self.commandString]];
|
||||
[self sendPressKey:kVK_ANSI_V modifierFlags:kCGEventFlagMaskCommand];
|
||||
if([self.commandString isPlaceholder]) {
|
||||
BOOL didReplace;
|
||||
NSString *evaluatedPlaceholder = [self.commandString evaluatePlaceholderWithEntry:entry didReplace:&didReplace];
|
||||
[controller copyObjects:@[evaluatedPlaceholder]];
|
||||
}
|
||||
else {
|
||||
[controller copyObjects:@[self.commandString]];
|
||||
}
|
||||
/* Find the correct key code! */
|
||||
[self sendPasteKeyCode];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user