Permalink
Browse files

glfwGetKeyName(int key) - Cocoa version working (adds dep to Carbon f…

…ramework).
  • Loading branch information...
1 parent 6edcc75 commit 9c68e11f2fee44b867bf4d2291399a1915bfd08d Doug Binks committed Aug 14, 2013
Showing with 235 additions and 5 deletions.
  1. +4 −2 CMakeLists.txt
  2. +231 −3 src/cocoa_window.m
View
@@ -343,13 +343,15 @@ if (_GLFW_COCOA AND _GLFW_NSGL)
find_library(COCOA_FRAMEWORK Cocoa)
find_library(IOKIT_FRAMEWORK IOKit)
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation)
+ find_library(CARBON_FRAMEWORK Carbon)
list(APPEND glfw_LIBRARIES ${COCOA_FRAMEWORK}
${OPENGL_gl_LIBRARY}
${IOKIT_FRAMEWORK}
- ${CORE_FOUNDATION_FRAMEWORK})
+ ${CORE_FOUNDATION_FRAMEWORK}
+ ${CARBON_FRAMEWORK})
set(GLFW_PKG_DEPS "")
- set(GLFW_PKG_LIBS "-framework Cocoa -framework OpenGL -framework IOKit -framework CoreFoundation")
+ set(GLFW_PKG_LIBS "-framework Cocoa -framework OpenGL -framework IOKit -framework CoreFoundation -framework Carbon")
endif()
#--------------------------------------------------------------------
View
@@ -29,6 +29,8 @@
// Needed for _NSGetProgname
#include <crt_externs.h>
+// Need for _glfwPlatformGetKeyName TIS* functions
+#include <Carbon/Carbon.h>
// Enter fullscreen mode
//
@@ -1094,10 +1096,236 @@ void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
}
}
-const char*_glfwPlatformGetKeyName(int key)
+const char* _glfwPlatformGetKeyName(int key)
{
- // TODO: Implement.
- return NULL;
+ // First try to translate virtual key mac os x style
+ UInt16 vKey = 0;
+ switch(key)
+ {
+ case GLFW_KEY_A: vKey = 0; break;
+ case GLFW_KEY_S: vKey = 1; break;
+ case GLFW_KEY_D: vKey = 2; break;
+ case GLFW_KEY_F: vKey = 3; break;
+ case GLFW_KEY_H: vKey = 4; break;
+ case GLFW_KEY_G: vKey = 5; break;
+ case GLFW_KEY_Z: vKey = 6; break;
+ case GLFW_KEY_X: vKey = 7; break;
+ case GLFW_KEY_C: vKey = 8; break;
+ case GLFW_KEY_V: vKey = 9; break;
+ case GLFW_KEY_GRAVE_ACCENT: vKey = 10; break;
+ case GLFW_KEY_B: vKey = 11; break;
+ case GLFW_KEY_Q: vKey = 12; break;
+ case GLFW_KEY_W: vKey = 13; break;
+ case GLFW_KEY_E: vKey = 14; break;
+ case GLFW_KEY_R: vKey = 15; break;
+ case GLFW_KEY_Y: vKey = 16; break;
+ case GLFW_KEY_T: vKey = 17; break;
+ case GLFW_KEY_1: vKey = 18; break;
+ case GLFW_KEY_2: vKey = 19; break;
+ case GLFW_KEY_3: vKey = 20; break;
+ case GLFW_KEY_4: vKey = 21; break;
+ case GLFW_KEY_6: vKey = 22; break;
+ case GLFW_KEY_5: vKey = 23; break;
+ case GLFW_KEY_EQUAL: vKey = 24; break;
+ case GLFW_KEY_9: vKey = 25; break;
+ case GLFW_KEY_7: vKey = 26; break;
+ case GLFW_KEY_MINUS: vKey = 27; break;
+ case GLFW_KEY_8: vKey = 28; break;
+ case GLFW_KEY_0: vKey = 29; break;
+ case GLFW_KEY_RIGHT_BRACKET: vKey = 30; break;
+ case GLFW_KEY_O: vKey = 31; break;
+ case GLFW_KEY_U: vKey = 32; break;
+ case GLFW_KEY_LEFT_BRACKET: vKey = 33; break;
+ case GLFW_KEY_I: vKey = 34; break;
+ case GLFW_KEY_P: vKey = 35; break;
+ case GLFW_KEY_ENTER: vKey = 36; break;
+ case GLFW_KEY_L: vKey = 37; break;
+ case GLFW_KEY_J: vKey = 38; break;
+ case GLFW_KEY_APOSTROPHE: vKey = 39; break;
+ case GLFW_KEY_K: vKey = 40; break;
+ case GLFW_KEY_SEMICOLON: vKey = 41; break;
+ case GLFW_KEY_BACKSLASH: vKey = 42; break;
+ case GLFW_KEY_COMMA: vKey = 43; break;
+ case GLFW_KEY_SLASH: vKey = 44; break;
+ case GLFW_KEY_N: vKey = 45; break;
+ case GLFW_KEY_M: vKey = 46; break;
+ case GLFW_KEY_PERIOD: vKey = 47; break;
+ case GLFW_KEY_TAB: vKey = 48; break;
+ case GLFW_KEY_SPACE: vKey = 49; break;
+ case GLFW_KEY_WORLD_1: vKey = 50; break;
+ }
+
+ if (vKey)
+ {
+ // get the current keyboard, need to do this every time
+ // in case keyboard has changed
+ TISInputSourceRef tisInputSource = TISCopyCurrentKeyboardInputSource();
+ CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(tisInputSource, kTISPropertyUnicodeKeyLayoutData);
+ const UCKeyboardLayout *kbLayoutUC = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
+
+ if (kbLayoutUC)
+ {
+ UInt32 deadKeyState = 0;
+ UniCharCount maxStringLength = 255;
+ UniCharCount actualStringLength = 0;
+ UniChar unicodeString[maxStringLength];
+
+ OSStatus status = UCKeyTranslate(kbLayoutUC,
+ vKey, kUCKeyActionDown, 0,
+ LMGetKbdType(), 0,
+ &deadKeyState,
+ maxStringLength,
+ &actualStringLength, unicodeString);
+
+ if (actualStringLength == 0 && deadKeyState)
+ {
+ status = UCKeyTranslate(kbLayoutUC,
+ kVK_Space, kUCKeyActionDown, 0,
+ LMGetKbdType(), 0,
+ &deadKeyState,
+ maxStringLength,
+ &actualStringLength, unicodeString);
+ }
+ if (actualStringLength > 0 && status == noErr)
+ {
+ NSString* tempNS = [[NSString stringWithCharacters:unicodeString length:(NSUInteger)actualStringLength] uppercaseString];
+ char* pTodoFree = strdup([tempNS UTF8String]);
+ return pTodoFree;
+ }
+ }
+ }
+
+
+ // Fallback to hard coded
+ switch (key)
+ {
+ // Printable keys
+ case GLFW_KEY_A: return "A";
+ case GLFW_KEY_B: return "B";
+ case GLFW_KEY_C: return "C";
+ case GLFW_KEY_D: return "D";
+ case GLFW_KEY_E: return "E";
+ case GLFW_KEY_F: return "F";
+ case GLFW_KEY_G: return "G";
+ case GLFW_KEY_H: return "H";
+ case GLFW_KEY_I: return "I";
+ case GLFW_KEY_J: return "J";
+ case GLFW_KEY_K: return "K";
+ case GLFW_KEY_L: return "L";
+ case GLFW_KEY_M: return "M";
+ case GLFW_KEY_N: return "N";
+ case GLFW_KEY_O: return "O";
+ case GLFW_KEY_P: return "P";
+ case GLFW_KEY_Q: return "Q";
+ case GLFW_KEY_R: return "R";
+ case GLFW_KEY_S: return "S";
+ case GLFW_KEY_T: return "T";
+ case GLFW_KEY_U: return "U";
+ case GLFW_KEY_V: return "V";
+ case GLFW_KEY_W: return "W";
+ case GLFW_KEY_X: return "X";
+ case GLFW_KEY_Y: return "Y";
+ case GLFW_KEY_Z: return "Z";
+ case GLFW_KEY_1: return "1";
+ case GLFW_KEY_2: return "2";
+ case GLFW_KEY_3: return "3";
+ case GLFW_KEY_4: return "4";
+ case GLFW_KEY_5: return "5";
+ case GLFW_KEY_6: return "6";
+ case GLFW_KEY_7: return "7";
+ case GLFW_KEY_8: return "8";
+ case GLFW_KEY_9: return "9";
+ case GLFW_KEY_0: return "0";
+ case GLFW_KEY_SPACE: return "SPACE";
+ case GLFW_KEY_MINUS: return "MINUS";
+ case GLFW_KEY_EQUAL: return "EQUAL";
+ case GLFW_KEY_LEFT_BRACKET: return "LEFT BRACKET";
+ case GLFW_KEY_RIGHT_BRACKET: return "RIGHT BRACKET";
+ case GLFW_KEY_BACKSLASH: return "BACKSLASH";
+ case GLFW_KEY_SEMICOLON: return "SEMICOLON";
+ case GLFW_KEY_APOSTROPHE: return "APOSTROPHE";
+ case GLFW_KEY_GRAVE_ACCENT: return "GRAVE ACCENT";
+ case GLFW_KEY_COMMA: return "COMMA";
+ case GLFW_KEY_PERIOD: return "PERIOD";
+ case GLFW_KEY_SLASH: return "SLASH";
+ case GLFW_KEY_WORLD_1: return "WORLD 1";
+ case GLFW_KEY_WORLD_2: return "WORLD 2";
+
+ // Function keys
+ case GLFW_KEY_ESCAPE: return "ESCAPE";
+ case GLFW_KEY_F1: return "F1";
+ case GLFW_KEY_F2: return "F2";
+ case GLFW_KEY_F3: return "F3";
+ case GLFW_KEY_F4: return "F4";
+ case GLFW_KEY_F5: return "F5";
+ case GLFW_KEY_F6: return "F6";
+ case GLFW_KEY_F7: return "F7";
+ case GLFW_KEY_F8: return "F8";
+ case GLFW_KEY_F9: return "F9";
+ case GLFW_KEY_F10: return "F10";
+ case GLFW_KEY_F11: return "F11";
+ case GLFW_KEY_F12: return "F12";
+ case GLFW_KEY_F13: return "F13";
+ case GLFW_KEY_F14: return "F14";
+ case GLFW_KEY_F15: return "F15";
+ case GLFW_KEY_F16: return "F16";
+ case GLFW_KEY_F17: return "F17";
+ case GLFW_KEY_F18: return "F18";
+ case GLFW_KEY_F19: return "F19";
+ case GLFW_KEY_F20: return "F20";
+ case GLFW_KEY_F21: return "F21";
+ case GLFW_KEY_F22: return "F22";
+ case GLFW_KEY_F23: return "F23";
+ case GLFW_KEY_F24: return "F24";
+ case GLFW_KEY_F25: return "F25";
+ case GLFW_KEY_UP: return "UP";
+ case GLFW_KEY_DOWN: return "DOWN";
+ case GLFW_KEY_LEFT: return "LEFT";
+ case GLFW_KEY_RIGHT: return "RIGHT";
+ case GLFW_KEY_LEFT_SHIFT: return "LEFT SHIFT";
+ case GLFW_KEY_RIGHT_SHIFT: return "RIGHT SHIFT";
+ case GLFW_KEY_LEFT_CONTROL: return "LEFT CONTROL";
+ case GLFW_KEY_RIGHT_CONTROL: return "RIGHT CONTROL";
+ case GLFW_KEY_LEFT_ALT: return "LEFT ALT";
+ case GLFW_KEY_RIGHT_ALT: return "RIGHT ALT";
+ case GLFW_KEY_TAB: return "TAB";
+ case GLFW_KEY_ENTER: return "ENTER";
+ case GLFW_KEY_BACKSPACE: return "BACKSPACE";
+ case GLFW_KEY_INSERT: return "INSERT";
+ case GLFW_KEY_DELETE: return "DELETE";
+ case GLFW_KEY_PAGE_UP: return "PAGE UP";
+ case GLFW_KEY_PAGE_DOWN: return "PAGE DOWN";
+ case GLFW_KEY_HOME: return "HOME";
+ case GLFW_KEY_END: return "END";
+ case GLFW_KEY_KP_0: return "KEYPAD 0";
+ case GLFW_KEY_KP_1: return "KEYPAD 1";
+ case GLFW_KEY_KP_2: return "KEYPAD 2";
+ case GLFW_KEY_KP_3: return "KEYPAD 3";
+ case GLFW_KEY_KP_4: return "KEYPAD 4";
+ case GLFW_KEY_KP_5: return "KEYPAD 5";
+ case GLFW_KEY_KP_6: return "KEYPAD 6";
+ case GLFW_KEY_KP_7: return "KEYPAD 7";
+ case GLFW_KEY_KP_8: return "KEYPAD 8";
+ case GLFW_KEY_KP_9: return "KEYPAD 9";
+ case GLFW_KEY_KP_DIVIDE: return "KEYPAD DIVIDE";
+ case GLFW_KEY_KP_MULTIPLY: return "KEYPAD MULTPLY";
+ case GLFW_KEY_KP_SUBTRACT: return "KEYPAD SUBTRACT";
+ case GLFW_KEY_KP_ADD: return "KEYPAD ADD";
+ case GLFW_KEY_KP_DECIMAL: return "KEYPAD DECIMAL";
+ case GLFW_KEY_KP_EQUAL: return "KEYPAD EQUAL";
+ case GLFW_KEY_KP_ENTER: return "KEYPAD ENTER";
+ case GLFW_KEY_PRINT_SCREEN: return "PRINT SCREEN";
+ case GLFW_KEY_NUM_LOCK: return "NUM LOCK";
+ case GLFW_KEY_CAPS_LOCK: return "CAPS LOCK";
+ case GLFW_KEY_SCROLL_LOCK: return "SCROLL LOCK";
+ case GLFW_KEY_PAUSE: return "PAUSE";
+ case GLFW_KEY_LEFT_SUPER: return "LEFT SUPER";
+ case GLFW_KEY_RIGHT_SUPER: return "RIGHT SUPER";
+ case GLFW_KEY_MENU: return "MENU";
+ case GLFW_KEY_UNKNOWN: return "UNKNOWN";
+
+ default: return NULL;
+ }
}
//////////////////////////////////////////////////////////////////////////

0 comments on commit 9c68e11

Please sign in to comment.