From 85f6f2e5d4fc3c2b5ad0f65e28273c1da497fcb6 Mon Sep 17 00:00:00 2001 From: Thai Pangsakulyanont Date: Sat, 11 Oct 2014 14:34:05 +0700 Subject: [PATCH] added the code that works. It is tested outside of StepMania --- .../InputHandler/InputHandler_MacOSX_HID.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/arch/InputHandler/InputHandler_MacOSX_HID.cpp b/src/arch/InputHandler/InputHandler_MacOSX_HID.cpp index 668dae06a6..2b86847d09 100644 --- a/src/arch/InputHandler/InputHandler_MacOSX_HID.cpp +++ b/src/arch/InputHandler/InputHandler_MacOSX_HID.cpp @@ -373,6 +373,50 @@ RString InputHandler_MacOSX_HID::GetDeviceSpecificInputString( const DeviceInput return InputHandler::GetDeviceSpecificInputString( di ); } +// Modified from NESControllerInterface of Macifom project, +// used under MIT license from http://macifom.googlecode.com/svn-history/r89/Macifom/trunk/NESControllerInterface.m +// Used under MIT license from http://inquisitivecocoa.com/2009/04/05/key-code-translator/ +static wchar_t KeyCodeToChar(CGKeyCode keyCode, unsigned int modifierFlags) +{ + 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, modifierFlags, + LMGetKbdType(), 0, + &deadKeyState, + maxStringLength, + &actualStringLength, unicodeString); + + if( status != noErr ) + { + fprintf(stderr, "There was an %s error translating from the '%d' key code to a human readable string: %s\n", + GetMacOSStatusErrorString(status), (int)status, GetMacOSStatusCommentString(status)); + } + else if( actualStringLength == 0 ) + { + fprintf(stderr, "Couldn't find a translation for the '%d' key code\n", keyCode); + } + else + { + return unicodeString[0]; + } + } + else + { + fprintf(stderr, "Couldn't find a translation for the '%d' key code\n", keyCode); + } + return 0; +} + wchar_t InputHandler_MacOSX_HID::DeviceButtonToChar( DeviceButton button, bool bUseCurrentKeyModifiers ) { // KeyTranslate maps these keys to a character. They shouldn't be mapped to any character. @@ -402,6 +446,14 @@ wchar_t InputHandler_MacOSX_HID::DeviceButtonToChar( DeviceButton button, bool b return L'\0'; } + // Use Quartz to translate device button to char + UInt8 iMacVirtualKey; + if( KeyboardDevice::DeviceButtonToMacVirtualKey( button, iMacVirtualKey ) ) + { + UInt32 nModifiers = bUseCurrentKeyModifiers ? GetCurrentKeyModifiers() : 0; + return KeyCodeToChar( iMacVirtualKey, nModifiers ); + } + return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers ); }