Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions CodingKeys/KeyCodeConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ + (NSDictionary *)fixKeys {
static CGKeyCode keyCodeForChar(const char c) {
static CFMutableDictionaryRef charToCodeDict = NULL;
CGKeyCode code;
const void *codeValue = NULL;
UniChar character = c;
CFStringRef charStr = NULL;

Expand All @@ -95,27 +96,40 @@ static CGKeyCode keyCodeForChar(const char c) {
}

charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1);

if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr, (const void **)&code)) {
if (charStr == NULL) {
return UINT16_MAX;
}

if (CFDictionaryGetValueIfPresent(charToCodeDict, charStr, &codeValue)) {
code = (CGKeyCode)(uintptr_t)codeValue;
} else {
code = UINT16_MAX;
}

CFRelease(charStr);

return code;
}

static CFStringRef createStringForKey(CGKeyCode keyCode) {
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
if (currentKeyboard == NULL) {
return NULL;
}

CFDataRef layoutData = TISGetInputSourceProperty(currentKeyboard,
kTISPropertyUnicodeKeyLayoutData);

if (layoutData == NULL) {
CFRelease(currentKeyboard);
return NULL;
}

const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);

UInt32 keysDown = 0;
UniChar chars[4];
UniCharCount realLength;

UCKeyTranslate(keyboardLayout,
keyCode,
kUCKeyActionDisplay,
Expand All @@ -127,7 +141,7 @@ static CFStringRef createStringForKey(CGKeyCode keyCode) {
&realLength,
chars);
CFRelease(currentKeyboard);

return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1);
}

Expand Down