public
Description:
Homepage:
Clone URL: git://github.com/jessegrosjean/quickcursor.git
quickcursor / PTKeyCodeTranslator.m
100644 73 lines (54 sloc) 1.902 kb
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
//
// PTKeyCodeTranslator.m
// Chercher
//
// Created by Finlay Dobbie on Sat Oct 11 2003.
// Copyright (c) 2003 Cliché Software. All rights reserved.
//
 
#import "PTKeyCodeTranslator.h"
 
 
@implementation PTKeyCodeTranslator
 
+ (id)currentTranslator
{
    static PTKeyCodeTranslator *current = nil;
    TISInputSourceRef currentLayout = TISCopyCurrentKeyboardLayoutInputSource();
 
    if (current == nil) {
        current = [[PTKeyCodeTranslator alloc] initWithKeyboardLayout:currentLayout];
    } else if ([current keyboardLayout] != currentLayout) {
        [current release];
        current = [[PTKeyCodeTranslator alloc] initWithKeyboardLayout:currentLayout];
    }
 
CFRelease(currentLayout);
 
    return current;
}
 
- (id)initWithKeyboardLayout:(TISInputSourceRef)aLayout
{
    if ((self = [super init]) != nil) {
        keyboardLayout = aLayout;
 
CFRetain(keyboardLayout);
 
        CFDataRef uchr = TISGetInputSourceProperty( keyboardLayout , kTISPropertyUnicodeKeyLayoutData );
        uchrData = ( const UCKeyboardLayout* )CFDataGetBytePtr(uchr);
    }
 
    return self;
}
 
- (void)dealloc
{
CFRelease(keyboardLayout);
 
[super dealloc];
}
 
- (NSString *)translateKeyCode:(short)keyCode {
    UniCharCount maxStringLength = 4, actualStringLength;
    UniChar unicodeString[4];
    UCKeyTranslate( uchrData, keyCode, kUCKeyActionDisplay, 0, LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit, &deadKeyState, maxStringLength, &actualStringLength, unicodeString );
    return [NSString stringWithCharacters:unicodeString length:1];
}
 
- (TISInputSourceRef)keyboardLayout {
    return keyboardLayout;
}
 
- (NSString *)description {
    NSString *kind;
    kind = @"uchr";
 
    NSString *layoutName;
    layoutName = TISGetInputSourceProperty( keyboardLayout, kTISPropertyLocalizedName );
    return [NSString stringWithFormat:@"PTKeyCodeTranslator layout=%@ (%@)", layoutName, kind];
}
 
@end