WinRT: added initial SDL_TEXTINPUT support
authorDavid Ludwig <dludwig@pobox.com>
Wed, 26 Nov 2014 21:34:15 -0500
changeset 9240dc4ad21d8596
parent 9239 4a4293b8a37f
child 9241 4900e838abbc
WinRT: added initial SDL_TEXTINPUT support

Further support regarding IME and on-screen keyboards is pending, some of
which might not be 100% compatible with other platforms, given WinRT platform
restrictions. An MSDN article at http://msdn.microsoft.com/en-us/library/windows/apps/hh465404.aspx
indicates that on-screen keyboard display requires that the user first tap on
a Windows/XAML text control.

This change adds basic SDL_TEXTINPUT support, with input coming from hardware
keyboards, at a minimum, and without the need for XAML integration (which is
still pending integration into SDL, by and large).
src/core/winrt/SDL_winrtapp_direct3d.cpp
src/core/winrt/SDL_winrtapp_direct3d.h
src/video/winrt/SDL_winrtevents_c.h
src/video/winrt/SDL_winrtkeyboard.cpp
     1.1 --- a/src/core/winrt/SDL_winrtapp_direct3d.cpp	Tue Nov 25 22:37:12 2014 +0100
     1.2 +++ b/src/core/winrt/SDL_winrtapp_direct3d.cpp	Wed Nov 26 21:34:15 2014 -0500
     1.3 @@ -365,6 +365,9 @@
     1.4      window->KeyUp +=
     1.5          ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
     1.6  
     1.7 +    window->CharacterReceived +=
     1.8 +        ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &SDL_WinRTApp::OnCharacterReceived);
     1.9 +
    1.10  #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    1.11      HardwareButtons::BackPressed +=
    1.12          ref new EventHandler<BackPressedEventArgs^>(this, &SDL_WinRTApp::OnBackButtonPressed);
    1.13 @@ -703,6 +706,11 @@
    1.14      WINRT_ProcessKeyUpEvent(args);
    1.15  }
    1.16  
    1.17 +void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args)
    1.18 +{
    1.19 +    WINRT_ProcessCharacterReceivedEvent(args);
    1.20 +}
    1.21 +
    1.22  #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    1.23  void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
    1.24  {
     2.1 --- a/src/core/winrt/SDL_winrtapp_direct3d.h	Tue Nov 25 22:37:12 2014 +0100
     2.2 +++ b/src/core/winrt/SDL_winrtapp_direct3d.h	Wed Nov 26 21:34:15 2014 -0500
     2.3 @@ -69,6 +69,7 @@
     2.4      void OnMouseMoved(Windows::Devices::Input::MouseDevice^ mouseDevice, Windows::Devices::Input::MouseEventArgs^ args);
     2.5      void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);
     2.6      void OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);
     2.7 +    void OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args);
     2.8  
     2.9  #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    2.10      void OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args);
     3.1 --- a/src/video/winrt/SDL_winrtevents_c.h	Tue Nov 25 22:37:12 2014 +0100
     3.2 +++ b/src/video/winrt/SDL_winrtevents_c.h	Wed Nov 26 21:34:15 2014 -0500
     3.3 @@ -63,6 +63,7 @@
     3.4  /* Keyboard */
     3.5  extern void WINRT_ProcessKeyDownEvent(Windows::UI::Core::KeyEventArgs ^args);
     3.6  extern void WINRT_ProcessKeyUpEvent(Windows::UI::Core::KeyEventArgs ^args);
     3.7 +extern void WINRT_ProcessCharacterReceivedEvent(Windows::UI::Core::CharacterReceivedEventArgs ^args);
     3.8  
     3.9  /* XAML Thread Management */
    3.10  extern void WINRT_CycleXAMLThread();
     4.1 --- a/src/video/winrt/SDL_winrtkeyboard.cpp	Tue Nov 25 22:37:12 2014 +0100
     4.2 +++ b/src/video/winrt/SDL_winrtkeyboard.cpp	Wed Nov 26 21:34:15 2014 -0500
     4.3 @@ -365,4 +365,22 @@
     4.4      SDL_SendKeyboardKey(SDL_RELEASED, sdlScancode);
     4.5  }
     4.6  
     4.7 +void
     4.8 +WINRT_ProcessCharacterReceivedEvent(Windows::UI::Core::CharacterReceivedEventArgs ^args)
     4.9 +{
    4.10 +    wchar_t src_ucs2[2];
    4.11 +    char dest_utf8[16];
    4.12 +    int result;
    4.13 +
    4.14 +    /* Setup src */
    4.15 +    src_ucs2[0] = args->KeyCode;
    4.16 +    src_ucs2[1] = L'\0';
    4.17 +
    4.18 +    /* Convert the text, then send an SDL_TEXTINPUT event. */
    4.19 +    result = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&src_ucs2, -1, (LPSTR)dest_utf8, sizeof(dest_utf8), NULL, NULL);
    4.20 +    if (result > 0) {
    4.21 +        SDL_SendKeyboardText(dest_utf8);
    4.22 +    }
    4.23 +}
    4.24 +
    4.25  #endif // SDL_VIDEO_DRIVER_WINRT