mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-24 06:19:45 +00:00
Interims commit. Broken. Doesn compile
This commit is contained in:
@@ -41,61 +41,136 @@ NSString *const kMPAutotypeCommandNumlock = @"{NUMLOCK}";
|
||||
NSString *const kMPAutotypeCommandPrintScreen = @"{PRTSC}";
|
||||
NSString *const kMPAutotypeCommandScrollLock = @"{SCROLLLOCK}";
|
||||
NSString *const kMPAutotypeCommandF1 = @"{F1}";
|
||||
|
||||
uint16_t const kMPUnknownKeyCode = UINT16_MAX;
|
||||
|
||||
/*
|
||||
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}
|
||||
|
||||
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 %
|
||||
+ {+}
|
||||
^ {^}
|
||||
% {%}
|
||||
~ {~}
|
||||
(, ) {(}, {)}
|
||||
[, ] {[}, {]}
|
||||
{, } {{}, {}}
|
||||
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 %
|
||||
+ {+}
|
||||
^ {^}
|
||||
% {%}
|
||||
~ {~}
|
||||
(, ) {(}, {)}
|
||||
[, ] {[}, {]}
|
||||
{, } {{}, {}}
|
||||
*/
|
||||
|
||||
|
||||
@implementation MPAutotypeCommand
|
||||
|
||||
+ (NSString *)stringForKey:(CGKeyCode)keyCode {
|
||||
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
|
||||
CFDataRef layoutData = TISGetInputSourceProperty(currentKeyboard,kTISPropertyUnicodeKeyLayoutData);
|
||||
CFRelease(currentKeyboard);
|
||||
|
||||
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
|
||||
/*
|
||||
Fallback for non-unicode Keyboards taken from to SRKeyCodeTransformer.m
|
||||
Copyright 2006-2007 Contributors. All rights reserved.
|
||||
License: BSD
|
||||
Contributors: David Dauer, Jesper, Jamie Kirkpatrick
|
||||
*/
|
||||
if(!keyboardLayout) {
|
||||
currentKeyboard = TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
|
||||
layoutData = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
|
||||
CFRelease(currentKeyboard);
|
||||
}
|
||||
|
||||
UInt32 keysDown = 0;
|
||||
UniChar chars[4];
|
||||
UniCharCount realLength;
|
||||
|
||||
UCKeyTranslate(keyboardLayout,
|
||||
keyCode,
|
||||
kUCKeyActionDisplay,
|
||||
0,
|
||||
LMGetKbdType(),
|
||||
kUCKeyTranslateNoDeadKeysBit,
|
||||
&keysDown,
|
||||
sizeof(chars) / sizeof(chars[0]),
|
||||
&realLength,
|
||||
chars);
|
||||
|
||||
return CFBridgingRelease(CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1));
|
||||
}
|
||||
|
||||
+ (CGKeyCode)keyCodeForCharacter:(NSString *)character {
|
||||
static NSMutableDictionary *keyboardCodeDictionary;
|
||||
|
||||
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
|
||||
NSString *localizedName = CFBridgingRelease(TISGetInputSourceProperty(currentKeyboard, kTISPropertyLocalizedName));
|
||||
|
||||
if(keyboardCodeDictionary == nil) {
|
||||
/* Input source should not change that much while we are running */
|
||||
keyboardCodeDictionary = [[NSMutableDictionary alloc] initWithCapacity:2];
|
||||
}
|
||||
NSDictionary *charToCodeDict = keyboardCodeDictionary[localizedName];
|
||||
if(nil == keyboardCodeDictionary[localizedName]) {
|
||||
/* We need 128 places for this dict */
|
||||
charToCodeDict = [[NSMutableDictionary alloc] initWithCapacity:128];
|
||||
|
||||
/* Loop through every keycode (0 - 127) to find its current mapping. */
|
||||
for(CGKeyCode keyCode = 0; keyCode < 128; ++keyCode) {
|
||||
NSString *string = [self stringForKey:keyCode];
|
||||
if(string != nil) {
|
||||
((NSMutableDictionary *)charToCodeDict)[string] = @(keyCode);
|
||||
}
|
||||
}
|
||||
keyboardCodeDictionary[localizedName] = [[NSDictionary alloc] initWithDictionary:charToCodeDict];
|
||||
}
|
||||
/* Add mapping */
|
||||
/* Generate table of keycodes and characters. */
|
||||
|
||||
NSString *singleCharacter = [[character substringToIndex:1] lowercaseString];
|
||||
if(charToCodeDict[singleCharacter]) {
|
||||
return [charToCodeDict[singleCharacter] integerValue];
|
||||
}
|
||||
return kMPUnknownKeyCode;
|
||||
}
|
||||
|
||||
- (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags {
|
||||
CGEventRef pressKey = CGEventCreateKeyboardEvent (NULL, keyCode, YES);
|
||||
CGEventRef releaseKey = CGEventCreateKeyboardEvent (NULL, keyCode, NO);
|
||||
|
||||
/* The modifer Masks might be set, reset them */
|
||||
CGEventSetFlags(pressKey,0);
|
||||
CGEventSetFlags(releaseKey, 0);
|
||||
/* Set the modifiers to the ones we want */
|
||||
CGEventSetFlags(pressKey,flags);
|
||||
CGEventSetFlags(releaseKey, flags);
|
||||
|
||||
/* Send the event */
|
||||
CGEventPost(kCGSessionEventTap, pressKey);
|
||||
CGEventPost(kCGSessionEventTap, releaseKey);
|
||||
|
||||
@@ -103,104 +178,15 @@ Alt %
|
||||
CFRelease(releaseKey);
|
||||
}
|
||||
|
||||
/*
|
||||
cody by Joe Turner http://www.cocoabuilder.com/archive/cocoa/242992-detect-keyboard-layout-for-cgkeycodes.html#243168
|
||||
*/
|
||||
- (CGKeyCode)keyCodeForKeyboard:(const UCKeyboardLayout *)uchrHeader character:(NSString *)character {
|
||||
if ([character isEqualToString:@"RETURN"]) return kVK_Return;
|
||||
if ([character isEqualToString:@"TAB"]) return kVK_Tab;
|
||||
if ([character isEqualToString:@"SPACE"]) return kVK_Space;
|
||||
if ([character isEqualToString:@"DELETE"]) return kVK_Delete;
|
||||
if ([character isEqualToString:@"ESCAPE"]) return kVK_Escape;
|
||||
if ([character isEqualToString:@"F5"]) return kVK_F5;
|
||||
if ([character isEqualToString:@"F6"]) return kVK_F6;
|
||||
if ([character isEqualToString:@"F7"]) return kVK_F7;
|
||||
if ([character isEqualToString:@"F3"]) return kVK_F3;
|
||||
if ([character isEqualToString:@"F8"]) return kVK_F8;
|
||||
if ([character isEqualToString:@"F9"]) return kVK_F9;
|
||||
if ([character isEqualToString:@"F11"]) return kVK_F11;
|
||||
if ([character isEqualToString:@"F13"]) return kVK_F13;
|
||||
if ([character isEqualToString:@"F16"]) return kVK_F16;
|
||||
if ([character isEqualToString:@"F14"]) return kVK_F14;
|
||||
if ([character isEqualToString:@"F10"]) return kVK_F10;
|
||||
if ([character isEqualToString:@"F12"]) return kVK_F12;
|
||||
if ([character isEqualToString:@"F15"]) return kVK_F15;
|
||||
if ([character isEqualToString:@"HELP"]) return kVK_Help;
|
||||
if ([character isEqualToString:@"HOME"]) return kVK_Home;
|
||||
if ([character isEqualToString:@"PAGE UP"]) return kVK_PageUp;
|
||||
if ([character isEqualToString:@"FORWARD DELETE"]) return kVK_ForwardDelete;
|
||||
if ([character isEqualToString:@"F4"]) return kVK_F4;
|
||||
if ([character isEqualToString:@"END"]) return kVK_End;
|
||||
if ([character isEqualToString:@"F2"]) return kVK_F2;
|
||||
if ([character isEqualToString:@"PAGE DOWN"]) return kVK_PageDown;
|
||||
if ([character isEqualToString:@"F1"]) return kVK_F1;
|
||||
if ([character isEqualToString:@"LEFT"]) return kVK_LeftArrow;
|
||||
if ([character isEqualToString:@"RIGHT"]) return kVK_RightArrow;
|
||||
if ([character isEqualToString:@"DOWN"]) return kVK_DownArrow;
|
||||
if ([character isEqualToString:@"UP"]) return kVK_UpArrow;
|
||||
|
||||
UTF16Char theCharacter = [character characterAtIndex:0];
|
||||
long i, j, k;
|
||||
unsigned char *uchrData = (unsigned char *)uchrHeader;
|
||||
UCKeyboardTypeHeader *uchrTable = uchrHeader->keyboardTypeList;
|
||||
BOOL found = NO;
|
||||
UInt16 virtualKeyCode;
|
||||
|
||||
for (i = 0; i < (uchrHeader->keyboardTypeCount) && !found; i++) {
|
||||
UCKeyToCharTableIndex *uchrKeyIX;
|
||||
UCKeyStateRecordsIndex *stateRecordsIndex;
|
||||
|
||||
if (uchrTable[i].keyStateRecordsIndexOffset != 0 ) {
|
||||
stateRecordsIndex = (UCKeyStateRecordsIndex *) (((unsigned char*)uchrData) + (uchrTable[i].keyStateRecordsIndexOffset));
|
||||
|
||||
if ((stateRecordsIndex->keyStateRecordsIndexFormat) != kUCKeyStateRecordsIndexFormat) {
|
||||
stateRecordsIndex = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
stateRecordsIndex = NULL;
|
||||
}
|
||||
|
||||
uchrKeyIX = (UCKeyToCharTableIndex *)(((unsigned char *)uchrData) + (uchrTable[i].keyToCharTableIndexOffset));
|
||||
|
||||
if (kUCKeyToCharTableIndexFormat == (uchrKeyIX-> keyToCharTableIndexFormat)) {
|
||||
for (j = 0; j < (uchrKeyIX->keyToCharTableCount) && !found; j++) {
|
||||
UCKeyOutput *keyToCharData = (UCKeyOutput *) ( ((unsigned char*)uchrData) + (uchrKeyIX->keyToCharTableOffsets[j]) );
|
||||
|
||||
for (k = 0; k < (uchrKeyIX->keyToCharTableSize) && !found; k++) {
|
||||
if (((keyToCharData[k]) & kUCKeyOutputTestForIndexMask) == kUCKeyOutputStateIndexMask) {
|
||||
long theIndex = (kUCKeyOutputGetIndexMask & keyToCharData[k]);
|
||||
|
||||
if (stateRecordsIndex != NULL && theIndex <= stateRecordsIndex-> keyStateRecordCount) {
|
||||
UCKeyStateRecord *theStateRecord = (UCKeyStateRecord *) (((unsigned char *) uchrData) + (stateRecordsIndex-> keyStateRecordOffsets[theIndex]));
|
||||
|
||||
if ((theStateRecord->stateZeroCharData) == theCharacter) {
|
||||
virtualKeyCode = k;
|
||||
found = YES;
|
||||
}
|
||||
} else {
|
||||
if ((keyToCharData[k]) == theCharacter) {
|
||||
virtualKeyCode = k;
|
||||
found = YES;
|
||||
}
|
||||
}
|
||||
} else if (((keyToCharData[k]) & kUCKeyOutputTestForIndexMask) == kUCKeyOutputSequenceIndexMask) {
|
||||
} else if ( (keyToCharData[k]) == 0xFFFE || (keyToCharData[k]) == 0xFFFF ) {
|
||||
} else {
|
||||
if ((keyToCharData[k]) == theCharacter) {
|
||||
virtualKeyCode = k;
|
||||
found = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
- (void)sendPasteKeyCode {
|
||||
CGKeyCode keyCode = [MPAutotypeCommand keyCodeForCharacter:@"V"];
|
||||
if(keyCode == kMPUnknownKeyCode) {
|
||||
return; // We did not find a mapping for "V"
|
||||
}
|
||||
return (CGKeyCode)virtualKeyCode;
|
||||
[self sendPressKey:keyCode modifierFlags:kCGEventFlagMaskCommand];
|
||||
}
|
||||
|
||||
- (void)executeWithEntry:(KPKEntry *)entry {
|
||||
- (void)execute {
|
||||
NSAssert(NO, @"Not Implemented");
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user