Skip to content

Commit

Permalink
Allow mapping special keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Luflosi committed Aug 26, 2019
1 parent d84e22f commit 7a0a67e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions glfw/cocoa_window.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <float.h>
#include <string.h>

#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */

// Needed for _NSGetProgname
#include <crt_externs.h>

Expand Down Expand Up @@ -349,6 +352,43 @@ static int translateKey(unsigned int key, bool apply_keymap)
return _glfw.ns.keycodes[key];
}

static NSString* keyCodeToString(CGKeyCode keyCode)
{
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
CFDataRef uchr =
(CFDataRef)TISGetInputSourceProperty(currentKeyboard,
kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);

if (keyboardLayout) {
UInt32 deadKeyState = 0;
UniCharCount maxStringLength = 255;
UniCharCount actualStringLength = 0;
UniChar unicodeString[maxStringLength];

OSStatus status = UCKeyTranslate(keyboardLayout,
keyCode, kUCKeyActionDown, 0,
LMGetKbdType(), 0,
&deadKeyState,
maxStringLength,
&actualStringLength, unicodeString);

if (actualStringLength == 0 && deadKeyState) {
status = UCKeyTranslate(keyboardLayout,
kVK_Space, kUCKeyActionDown, 0,
LMGetKbdType(), 0,
&deadKeyState,
maxStringLength,
&actualStringLength, unicodeString);
}
if (actualStringLength > 0 && status == noErr)
return [[NSString stringWithCharacters:unicodeString
length:(NSUInteger)actualStringLength] lowercaseString];
}

return nil;
}

static void
display_reconfigured(CGDirectDisplayID display UNUSED, CGDisplayChangeSummaryFlags flags, void *userInfo UNUSED)
{
Expand Down Expand Up @@ -922,6 +962,10 @@ - (void)keyDown:(NSEvent *)event
const int key = translateKey(scancode, true);
const bool process_text = !window->ns.textInputFilterCallback || window->ns.textInputFilterCallback(key, mods, scancode, flags) != 1;
const bool previous_has_marked_text = [self hasMarkedText];
NSString *key_new = keyCodeToString(scancode);
NSData *key_bytes = [key_new dataUsingEncoding:NSUTF8StringEncoding];
uint8_t *key_raw_btes = (uint8_t *)[key_bytes bytes];
NSLog(@">%s<\t>%lu<\t>%u<\t>%@<", _glfwGetKeyName(key), [key_new length], key_raw_btes[0], key_new);
[self unmarkText];
_glfw.ns.text[0] = 0;
if (!_glfw.ns.unicodeData) {
Expand Down
2 changes: 1 addition & 1 deletion glfw/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def init_env(env, pkg_config, at_least_version, test_compile, module='x11'):
ans.ldpaths.extend(pkg_config(dep, '--libs'))

elif module == 'cocoa':
for f in 'Cocoa IOKit CoreFoundation CoreVideo'.split():
for f in 'Cocoa IOKit CoreFoundation CoreVideo Carbon'.split():
ans.ldpaths.extend(('-framework', f))

elif module == 'wayland':
Expand Down

0 comments on commit 7a0a67e

Please sign in to comment.