/
Telefrag.m
78 lines (61 loc) · 2.07 KB
/
Telefrag.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Telefrag.m
// Telefrag
//
// Created by chendo on 17/08/09.
// Copyright 2009 chendo. All rights reserved.
//
#import "Telefrag.h"
#import "MethodSwizzle.h"
@implementation TPEventTapsController (Telefrag)
- (void) _sendEventToListener_s:(CGEventRef)event{
CGEventType type = CGEventGetType(event);
if (type == kCGEventKeyDown || type == kCGEventKeyUp) {
CGKeyCode keyCode = CGEventGetIntegerValueField( event, kCGKeyboardEventKeycode );
int charCode = (int)keyCode;
int mappedKeycode = [Telefrag getMappedKeycode:charCode];
if (mappedKeycode != -1) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, mappedKeycode);
}
}
[self _sendEventToListener_s:event];
}
@end
@implementation Telefrag : NSObject
static NSDictionary *moddedKeymap;
+ (void) load {
NSLog(@"telefrag loaded");
if (MethodSwizzle(NSClassFromString(@"TPEventTapsController"), @selector(_sendEventToListener:), @selector(_sendEventToListener_s:))) {
NSLog(@"telefrag swizzled TFEventTapsController");
}
}
+ (int) getMappedKeycode:(int) keyCode {
if (moddedKeymap == NULL) {
[self loadKeymap];
}
NSString *key = [NSString stringWithFormat:@"%d", keyCode];
NSNumber *value;
if (value = (NSNumber *)[moddedKeymap objectForKey:key]) {
return [value integerValue];
}
else {
return -1;
}
}
+ (void) loadKeymap {
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle bundleForClass:self] pathForResource:@"Keymap" ofType:@"plist"];
if (plistPath == NULL) {
NSLog(@"Keymap.plist not found; have you made the symlink yet?");
return;
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
moddedKeymap = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
[moddedKeymap retain];
NSLog(@"keymap loaded: %@", moddedKeymap);
}
@end