update key code style

This commit is contained in:
vcaesar 2018-08-13 08:03:52 -04:00
parent 4ac4864a61
commit 50f8df44b3
2 changed files with 62 additions and 70 deletions

View File

@ -62,74 +62,74 @@ struct XSpecialCharacterMapping XSpecialCharacterTable[] = {
#endif #endif
MMKeyCode keyCodeForChar(const char c) MMKeyCode keyCodeForChar(const char c){
{ #if defined(IS_MACOSX)
#if defined(IS_MACOSX) /* OS X does not appear to have a built-in function for this, so instead we
/* OS X does not appear to have a built-in function for this, so instead we * have to write our own. */
* have to write our own. */ static CFMutableDictionaryRef charToCodeDict = NULL;
static CFMutableDictionaryRef charToCodeDict = NULL; CGKeyCode code;
CGKeyCode code; UniChar character = c;
UniChar character = c; CFStringRef charStr = NULL;
CFStringRef charStr = NULL;
/* Generate table of keycodes and characters. */ /* Generate table of keycodes and characters. */
if (charToCodeDict == NULL) { if (charToCodeDict == NULL) {
size_t i; size_t i;
charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault, charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
128, 128,
&kCFCopyStringDictionaryKeyCallBacks, &kCFCopyStringDictionaryKeyCallBacks,
NULL); NULL);
if (charToCodeDict == NULL) return UINT16_MAX; if (charToCodeDict == NULL) return UINT16_MAX;
/* Loop through every keycode (0 - 127) to find its current mapping. */ /* Loop through every keycode (0 - 127) to find its current mapping. */
for (i = 0; i < 128; ++i) { for (i = 0; i < 128; ++i) {
CFStringRef string = createStringForKey((CGKeyCode)i); CFStringRef string = createStringForKey((CGKeyCode)i);
if (string != NULL) { if (string != NULL) {
CFDictionaryAddValue(charToCodeDict, string, (const void *)i); CFDictionaryAddValue(charToCodeDict, string, (const void *)i);
CFRelease(string); CFRelease(string);
}
} }
} }
}
charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1); charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1);
/* Our values may be NULL (0), so we need to use this function. */ /* Our values may be NULL (0), so we need to use this function. */
if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr, if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr,
(const void **)&code)) { (const void **)&code)) {
code = UINT16_MAX; /* Error */ code = UINT16_MAX; /* Error */
} }
CFRelease(charStr); CFRelease(charStr);
return (MMKeyCode)code; return (MMKeyCode)code;
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
return VkKeyScan(c); return VkKeyScan(c);
#elif defined(USE_X11) #elif defined(USE_X11)
MMKeyCode code; MMKeyCode code;
char buf[2]; char buf[2];
buf[0] = c; buf[0] = c;
buf[1] = '\0'; buf[1] = '\0';
code = XStringToKeysym(buf); code = XStringToKeysym(buf);
if (code == NoSymbol) { if (code == NoSymbol) {
/* Some special keys are apparently not handled properly by /* Some special keys are apparently not handled properly by
* XStringToKeysym() on some systems, so search for them instead in our * XStringToKeysym() on some systems, so search for them instead in our
* mapping table. */ * mapping table. */
size_t i; size_t i;
const size_t specialCharacterCount = const size_t specialCharacterCount =
sizeof(XSpecialCharacterTable) / sizeof(XSpecialCharacterTable[0]); sizeof(XSpecialCharacterTable) / sizeof(XSpecialCharacterTable[0]);
for (i = 0; i < specialCharacterCount; ++i) { for (i = 0; i < specialCharacterCount; ++i) {
if (c == XSpecialCharacterTable[i].name) { if (c == XSpecialCharacterTable[i].name) {
code = XSpecialCharacterTable[i].code; code = XSpecialCharacterTable[i].code;
break; break;
}
} }
} }
}
return code; return code;
#endif #endif
} }
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CFStringRef createStringForKey(CGKeyCode keyCode){ CFStringRef createStringForKey(CGKeyCode keyCode){

View File

@ -29,8 +29,7 @@
#endif #endif
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
static io_connect_t _getAuxiliaryKeyDriver(void) static io_connect_t _getAuxiliaryKeyDriver(void){
{
static mach_port_t sEventDrvrRef = 0; static mach_port_t sEventDrvrRef = 0;
mach_port_t masterPort, service, iter; mach_port_t masterPort, service, iter;
kern_return_t kr; kern_return_t kr;
@ -52,13 +51,11 @@ static io_connect_t _getAuxiliaryKeyDriver(void)
#endif #endif
#if defined(IS_WINDOWS) #if defined(IS_WINDOWS)
void win32KeyEvent(int key, MMKeyFlags flags) void win32KeyEvent(int key, MMKeyFlags flags){
{
int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC); int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC);
/* Set the scan code for extended keys */ /* Set the scan code for extended keys */
switch (key) switch (key){
{
case VK_RCONTROL: case VK_RCONTROL:
case VK_SNAPSHOT: /* Print Screen */ case VK_SNAPSHOT: /* Print Screen */
case VK_RMENU: /* Right Alt / Alt Gr */ case VK_RMENU: /* Right Alt / Alt Gr */
@ -106,8 +103,7 @@ void win32KeyEvent(int key, MMKeyFlags flags)
} }
#endif #endif
void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags){
{
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
/* The media keys all have 1000 added to them to help us detect them. */ /* The media keys all have 1000 added to them to help us detect them. */
if (code >= 1000) { if (code >= 1000) {
@ -156,14 +152,12 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags)
#endif #endif
} }
void tapKeyCode(MMKeyCode code, MMKeyFlags flags) void tapKeyCode(MMKeyCode code, MMKeyFlags flags){
{
toggleKeyCode(code, true, flags); toggleKeyCode(code, true, flags);
toggleKeyCode(code, false, flags); toggleKeyCode(code, false, flags);
} }
void toggleKey(char c, const bool down, MMKeyFlags flags) void toggleKey(char c, const bool down, MMKeyFlags flags){
{
MMKeyCode keyCode = keyCodeForChar(c); MMKeyCode keyCode = keyCodeForChar(c);
//Prevent unused variable warning for Mac and Linux. //Prevent unused variable warning for Mac and Linux.
@ -185,15 +179,13 @@ void toggleKey(char c, const bool down, MMKeyFlags flags)
toggleKeyCode(keyCode, down, flags); toggleKeyCode(keyCode, down, flags);
} }
void tapKey(char c, MMKeyFlags flags) void tapKey(char c, MMKeyFlags flags){
{
toggleKey(c, true, flags); toggleKey(c, true, flags);
toggleKey(c, false, flags); toggleKey(c, false, flags);
} }
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
void toggleUnicode(UniChar ch, const bool down) void toggleUnicode(UniChar ch, const bool down){
{
/* This function relies on the convenient /* This function relies on the convenient
* CGEventKeyboardSetUnicodeString(), which allows us to not have to * CGEventKeyboardSetUnicodeString(), which allows us to not have to
* convert characters to a keycode, but does not support adding modifier * convert characters to a keycode, but does not support adding modifier