WinRT: added key event support for most WinRT/VirtualKey-based keys
authorDavid Ludwig <dludwig@pobox.com>
Sun, 04 Nov 2012 09:02:58 -0500
changeset 83360b9aa42be7ae
parent 8335 4f41add2ff00
child 8337 4a67a3cca43d
child 8338 53c8cafb945a
WinRT: added key event support for most WinRT/VirtualKey-based keys
src/video/windowsrt/SDL_WinRTApp.cpp
src/video/windowsrt/SDL_WinRTApp.h
     1.1 --- a/src/video/windowsrt/SDL_WinRTApp.cpp	Mon Oct 29 23:32:13 2012 -0400
     1.2 +++ b/src/video/windowsrt/SDL_WinRTApp.cpp	Sun Nov 04 09:02:58 2012 -0500
     1.3 @@ -6,6 +6,7 @@
     1.4  #include "SDL_stdinc.h"
     1.5  #include "../SDL_sysvideo.h"
     1.6  #include "../../events/SDL_mouse_c.h"
     1.7 +#include "../../events/SDL_keyboard_c.h"
     1.8  #include "SDL_events.h"
     1.9  #include "SDL_log.h"
    1.10  }
    1.11 @@ -78,6 +79,13 @@
    1.12  	window->PointerMoved +=
    1.13  		ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &SDL_WinRTApp::OnPointerMoved);
    1.14  
    1.15 +    window->KeyDown +=
    1.16 +		ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyDown);
    1.17 +
    1.18 +	window->KeyUp +=
    1.19 +		ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &SDL_WinRTApp::OnKeyUp);
    1.20 +
    1.21 +
    1.22  	m_renderer->Initialize(CoreWindow::GetForCurrentThread());
    1.23  }
    1.24  
    1.25 @@ -158,6 +166,223 @@
    1.26      }
    1.27  }
    1.28  
    1.29 +static SDL_Scancode WinRT_Keycodes[] = {
    1.30 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.None -- 0
    1.31 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.LeftButton -- 1
    1.32 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.RightButton -- 2
    1.33 +    SDL_SCANCODE_CANCEL, // VirtualKey.Cancel -- 3
    1.34 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.MiddleButton -- 4
    1.35 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.XButton1 -- 5
    1.36 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.XButton2 -- 6
    1.37 +    SDL_SCANCODE_UNKNOWN, // -- 7
    1.38 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Back -- 8  (maybe SDL_SCANCODE_AC_BACK ?)
    1.39 +    SDL_SCANCODE_TAB, // VirtualKey.Tab -- 9
    1.40 +    SDL_SCANCODE_UNKNOWN, // -- 10
    1.41 +    SDL_SCANCODE_UNKNOWN, // -- 11
    1.42 +    SDL_SCANCODE_CLEAR, // VirtualKey.Clear -- 12
    1.43 +    SDL_SCANCODE_RETURN, // VirtualKey.Enter -- 13
    1.44 +    SDL_SCANCODE_UNKNOWN, // -- 14
    1.45 +    SDL_SCANCODE_UNKNOWN, // -- 15
    1.46 +    SDL_SCANCODE_LSHIFT, // VirtualKey.Shift -- 16
    1.47 +    SDL_SCANCODE_LCTRL, // VirtualKey.Control -- 17
    1.48 +    SDL_SCANCODE_MENU, // VirtualKey.Menu -- 18
    1.49 +    SDL_SCANCODE_PAUSE, // VirtualKey.Pause -- 19
    1.50 +    SDL_SCANCODE_CAPSLOCK, // VirtualKey.CapitalLock -- 20
    1.51 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Kana or VirtualKey.Hangul -- 21
    1.52 +    SDL_SCANCODE_UNKNOWN, // -- 22
    1.53 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Junja -- 23
    1.54 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Final -- 24
    1.55 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Hanja or VirtualKey.Kanji -- 25
    1.56 +    SDL_SCANCODE_UNKNOWN, // -- 26
    1.57 +    SDL_SCANCODE_ESCAPE, // VirtualKey.Escape -- 27
    1.58 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Convert -- 28
    1.59 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.NonConvert -- 29
    1.60 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Accept -- 30
    1.61 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.ModeChange -- 31  (maybe SDL_SCANCODE_MODE ?)
    1.62 +    SDL_SCANCODE_SPACE, // VirtualKey.Space -- 32
    1.63 +    SDL_SCANCODE_PAGEUP, // VirtualKey.PageUp -- 33
    1.64 +    SDL_SCANCODE_PAGEDOWN, // VirtualKey.PageDown -- 34
    1.65 +    SDL_SCANCODE_END, // VirtualKey.End -- 35
    1.66 +    SDL_SCANCODE_HOME, // VirtualKey.Home -- 36
    1.67 +    SDL_SCANCODE_LEFT, // VirtualKey.Left -- 37
    1.68 +    SDL_SCANCODE_UP, // VirtualKey.Up -- 38
    1.69 +    SDL_SCANCODE_RIGHT, // VirtualKey.Right -- 39
    1.70 +    SDL_SCANCODE_DOWN, // VirtualKey.Down -- 40
    1.71 +    SDL_SCANCODE_SELECT, // VirtualKey.Select -- 41
    1.72 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Print -- 42  (maybe SDL_SCANCODE_PRINTSCREEN ?)
    1.73 +    SDL_SCANCODE_EXECUTE, // VirtualKey.Execute -- 43
    1.74 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Snapshot -- 44
    1.75 +    SDL_SCANCODE_INSERT, // VirtualKey.Insert -- 45
    1.76 +    SDL_SCANCODE_DELETE, // VirtualKey.Delete -- 46
    1.77 +    SDL_SCANCODE_HELP, // VirtualKey.Help -- 47
    1.78 +    SDL_SCANCODE_0, // VirtualKey.Number0 -- 48
    1.79 +    SDL_SCANCODE_1, // VirtualKey.Number1 -- 49
    1.80 +    SDL_SCANCODE_2, // VirtualKey.Number2 -- 50
    1.81 +    SDL_SCANCODE_3, // VirtualKey.Number3 -- 51
    1.82 +    SDL_SCANCODE_4, // VirtualKey.Number4 -- 52
    1.83 +    SDL_SCANCODE_5, // VirtualKey.Number5 -- 53
    1.84 +    SDL_SCANCODE_6, // VirtualKey.Number6 -- 54
    1.85 +    SDL_SCANCODE_7, // VirtualKey.Number7 -- 55
    1.86 +    SDL_SCANCODE_8, // VirtualKey.Number8 -- 56
    1.87 +    SDL_SCANCODE_9, // VirtualKey.Number9 -- 57
    1.88 +    SDL_SCANCODE_UNKNOWN, // -- 58
    1.89 +    SDL_SCANCODE_UNKNOWN, // -- 59
    1.90 +    SDL_SCANCODE_UNKNOWN, // -- 60
    1.91 +    SDL_SCANCODE_UNKNOWN, // -- 61
    1.92 +    SDL_SCANCODE_UNKNOWN, // -- 62
    1.93 +    SDL_SCANCODE_UNKNOWN, // -- 63
    1.94 +    SDL_SCANCODE_UNKNOWN, // -- 64
    1.95 +    SDL_SCANCODE_A, // VirtualKey.A -- 65
    1.96 +    SDL_SCANCODE_B, // VirtualKey.B -- 66
    1.97 +    SDL_SCANCODE_C, // VirtualKey.C -- 67
    1.98 +    SDL_SCANCODE_D, // VirtualKey.D -- 68
    1.99 +    SDL_SCANCODE_E, // VirtualKey.E -- 69
   1.100 +    SDL_SCANCODE_F, // VirtualKey.F -- 70
   1.101 +    SDL_SCANCODE_G, // VirtualKey.G -- 71
   1.102 +    SDL_SCANCODE_H, // VirtualKey.H -- 72
   1.103 +    SDL_SCANCODE_I, // VirtualKey.I -- 73
   1.104 +    SDL_SCANCODE_J, // VirtualKey.J -- 74
   1.105 +    SDL_SCANCODE_K, // VirtualKey.K -- 75
   1.106 +    SDL_SCANCODE_L, // VirtualKey.L -- 76
   1.107 +    SDL_SCANCODE_M, // VirtualKey.M -- 77
   1.108 +    SDL_SCANCODE_N, // VirtualKey.N -- 78
   1.109 +    SDL_SCANCODE_O, // VirtualKey.O -- 79
   1.110 +    SDL_SCANCODE_P, // VirtualKey.P -- 80
   1.111 +    SDL_SCANCODE_Q, // VirtualKey.Q -- 81
   1.112 +    SDL_SCANCODE_R, // VirtualKey.R -- 82
   1.113 +    SDL_SCANCODE_S, // VirtualKey.S -- 83
   1.114 +    SDL_SCANCODE_T, // VirtualKey.T -- 84
   1.115 +    SDL_SCANCODE_U, // VirtualKey.U -- 85
   1.116 +    SDL_SCANCODE_V, // VirtualKey.V -- 86
   1.117 +    SDL_SCANCODE_W, // VirtualKey.W -- 87
   1.118 +    SDL_SCANCODE_X, // VirtualKey.X -- 88
   1.119 +    SDL_SCANCODE_Y, // VirtualKey.Y -- 89
   1.120 +    SDL_SCANCODE_Z, // VirtualKey.Z -- 90
   1.121 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.LeftWindows -- 91  (maybe SDL_SCANCODE_APPLICATION or SDL_SCANCODE_LGUI ?)
   1.122 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.RightWindows -- 92  (maybe SDL_SCANCODE_APPLICATION or SDL_SCANCODE_RGUI ?)
   1.123 +    SDL_SCANCODE_APPLICATION, // VirtualKey.Application -- 93
   1.124 +    SDL_SCANCODE_UNKNOWN, // -- 94
   1.125 +    SDL_SCANCODE_SLEEP, // VirtualKey.Sleep -- 95
   1.126 +    SDL_SCANCODE_KP_0, // VirtualKey.NumberPad0 -- 96
   1.127 +    SDL_SCANCODE_KP_1, // VirtualKey.NumberPad1 -- 97
   1.128 +    SDL_SCANCODE_KP_2, // VirtualKey.NumberPad2 -- 98
   1.129 +    SDL_SCANCODE_KP_3, // VirtualKey.NumberPad3 -- 99
   1.130 +    SDL_SCANCODE_KP_4, // VirtualKey.NumberPad4 -- 100
   1.131 +    SDL_SCANCODE_KP_5, // VirtualKey.NumberPad5 -- 101
   1.132 +    SDL_SCANCODE_KP_6, // VirtualKey.NumberPad6 -- 102
   1.133 +    SDL_SCANCODE_KP_7, // VirtualKey.NumberPad7 -- 103
   1.134 +    SDL_SCANCODE_KP_8, // VirtualKey.NumberPad8 -- 104
   1.135 +    SDL_SCANCODE_KP_9, // VirtualKey.NumberPad9 -- 105
   1.136 +    SDL_SCANCODE_KP_MULTIPLY, // VirtualKey.Multiply -- 106
   1.137 +    SDL_SCANCODE_KP_PLUS, // VirtualKey.Add -- 107
   1.138 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Separator -- 108
   1.139 +    SDL_SCANCODE_KP_MINUS, // VirtualKey.Subtract -- 109
   1.140 +    SDL_SCANCODE_UNKNOWN, // VirtualKey.Decimal -- 110  (maybe SDL_SCANCODE_DECIMALSEPARATOR, SDL_SCANCODE_KP_DECIMAL, or SDL_SCANCODE_KP_PERIOD ?)
   1.141 +    SDL_SCANCODE_KP_DIVIDE, // VirtualKey.Divide -- 111
   1.142 +    SDL_SCANCODE_F1, // VirtualKey.F1 -- 112
   1.143 +    SDL_SCANCODE_F2, // VirtualKey.F2 -- 113
   1.144 +    SDL_SCANCODE_F3, // VirtualKey.F3 -- 114
   1.145 +    SDL_SCANCODE_F4, // VirtualKey.F4 -- 115
   1.146 +    SDL_SCANCODE_F5, // VirtualKey.F5 -- 116
   1.147 +    SDL_SCANCODE_F6, // VirtualKey.F6 -- 117
   1.148 +    SDL_SCANCODE_F7, // VirtualKey.F7 -- 118
   1.149 +    SDL_SCANCODE_F8, // VirtualKey.F8 -- 119
   1.150 +    SDL_SCANCODE_F9, // VirtualKey.F9 -- 120
   1.151 +    SDL_SCANCODE_F10, // VirtualKey.F10 -- 121
   1.152 +    SDL_SCANCODE_F11, // VirtualKey.F11 -- 122
   1.153 +    SDL_SCANCODE_F12, // VirtualKey.F12 -- 123
   1.154 +    SDL_SCANCODE_F13, // VirtualKey.F13 -- 124
   1.155 +    SDL_SCANCODE_F14, // VirtualKey.F14 -- 125
   1.156 +    SDL_SCANCODE_F15, // VirtualKey.F15 -- 126
   1.157 +    SDL_SCANCODE_F16, // VirtualKey.F16 -- 127
   1.158 +    SDL_SCANCODE_F17, // VirtualKey.F17 -- 128
   1.159 +    SDL_SCANCODE_F18, // VirtualKey.F18 -- 129
   1.160 +    SDL_SCANCODE_F19, // VirtualKey.F19 -- 130
   1.161 +    SDL_SCANCODE_F20, // VirtualKey.F20 -- 131
   1.162 +    SDL_SCANCODE_F21, // VirtualKey.F21 -- 132
   1.163 +    SDL_SCANCODE_F22, // VirtualKey.F22 -- 133
   1.164 +    SDL_SCANCODE_F23, // VirtualKey.F23 -- 134
   1.165 +    SDL_SCANCODE_F24, // VirtualKey.F24 -- 135
   1.166 +    SDL_SCANCODE_UNKNOWN, // -- 136
   1.167 +    SDL_SCANCODE_UNKNOWN, // -- 137
   1.168 +    SDL_SCANCODE_UNKNOWN, // -- 138
   1.169 +    SDL_SCANCODE_UNKNOWN, // -- 139
   1.170 +    SDL_SCANCODE_UNKNOWN, // -- 140
   1.171 +    SDL_SCANCODE_UNKNOWN, // -- 141
   1.172 +    SDL_SCANCODE_UNKNOWN, // -- 142
   1.173 +    SDL_SCANCODE_UNKNOWN, // -- 143
   1.174 +    SDL_SCANCODE_NUMLOCKCLEAR, // VirtualKey.NumberKeyLock -- 144
   1.175 +    SDL_SCANCODE_SCROLLLOCK, // VirtualKey.Scroll -- 145
   1.176 +    SDL_SCANCODE_UNKNOWN, // -- 146
   1.177 +    SDL_SCANCODE_UNKNOWN, // -- 147
   1.178 +    SDL_SCANCODE_UNKNOWN, // -- 148
   1.179 +    SDL_SCANCODE_UNKNOWN, // -- 149
   1.180 +    SDL_SCANCODE_UNKNOWN, // -- 150
   1.181 +    SDL_SCANCODE_UNKNOWN, // -- 151
   1.182 +    SDL_SCANCODE_UNKNOWN, // -- 152
   1.183 +    SDL_SCANCODE_UNKNOWN, // -- 153
   1.184 +    SDL_SCANCODE_UNKNOWN, // -- 154
   1.185 +    SDL_SCANCODE_UNKNOWN, // -- 155
   1.186 +    SDL_SCANCODE_UNKNOWN, // -- 156
   1.187 +    SDL_SCANCODE_UNKNOWN, // -- 157
   1.188 +    SDL_SCANCODE_UNKNOWN, // -- 158
   1.189 +    SDL_SCANCODE_UNKNOWN, // -- 159
   1.190 +    SDL_SCANCODE_LSHIFT, // VirtualKey.LeftShift -- 160
   1.191 +    SDL_SCANCODE_RSHIFT, // VirtualKey.RightShift -- 161
   1.192 +    SDL_SCANCODE_LCTRL, // VirtualKey.LeftControl -- 162
   1.193 +    SDL_SCANCODE_RCTRL, // VirtualKey.RightControl -- 163
   1.194 +    SDL_SCANCODE_MENU, // VirtualKey.LeftMenu -- 164
   1.195 +    SDL_SCANCODE_MENU, // VirtualKey.RightMenu -- 165
   1.196 +};
   1.197 +
   1.198 +static SDL_Scancode
   1.199 +TranslateKeycode(int keycode)
   1.200 +{
   1.201 +    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
   1.202 +    if (keycode < SDL_arraysize(WinRT_Keycodes)) {
   1.203 +        scancode = WinRT_Keycodes[keycode];
   1.204 +    }
   1.205 +    if (scancode == SDL_SCANCODE_UNKNOWN) {
   1.206 +        SDL_Log("WinRT TranslateKeycode, unknown keycode=%d\n", (int)keycode);
   1.207 +    }
   1.208 +    return scancode;
   1.209 +}
   1.210 +
   1.211 +void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
   1.212 +{
   1.213 +#if 0
   1.214 +    SDL_Log("key down, handled=%s, ext?=%s, released?=%s, menu key down?=%s, repeat count=%d, scan code=%d, was down?=%s, vkey=%d\n",
   1.215 +        (args->Handled ? "1" : "0"),
   1.216 +        (args->KeyStatus.IsExtendedKey ? "1" : "0"),
   1.217 +        (args->KeyStatus.IsKeyReleased ? "1" : "0"),
   1.218 +        (args->KeyStatus.IsMenuKeyDown ? "1" : "0"),
   1.219 +        args->KeyStatus.RepeatCount,
   1.220 +        args->KeyStatus.ScanCode,
   1.221 +        (args->KeyStatus.WasKeyDown ? "1" : "0"),
   1.222 +        args->VirtualKey);
   1.223 +    //args->Handled = true;
   1.224 +    //VirtualKey vkey = args->VirtualKey;
   1.225 +#endif
   1.226 +    SDL_SendKeyboardKey(SDL_PRESSED, TranslateKeycode((int)args->VirtualKey));
   1.227 +}
   1.228 +
   1.229 +void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
   1.230 +{
   1.231 +#if 0
   1.232 +    SDL_Log("key up, handled=%s, ext?=%s, released?=%s, menu key down?=%s, repeat count=%d, scan code=%d, was down?=%s, vkey=%d\n",
   1.233 +        (args->Handled ? "1" : "0"),
   1.234 +        (args->KeyStatus.IsExtendedKey ? "1" : "0"),
   1.235 +        (args->KeyStatus.IsKeyReleased ? "1" : "0"),
   1.236 +        (args->KeyStatus.IsMenuKeyDown ? "1" : "0"),
   1.237 +        args->KeyStatus.RepeatCount,
   1.238 +        args->KeyStatus.ScanCode,
   1.239 +        (args->KeyStatus.WasKeyDown ? "1" : "0"),
   1.240 +        args->VirtualKey);
   1.241 +    //args->Handled = true;
   1.242 +#endif
   1.243 +    SDL_SendKeyboardKey(SDL_RELEASED, TranslateKeycode((int)args->VirtualKey));
   1.244 +}
   1.245 +
   1.246  void SDL_WinRTApp::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
   1.247  {
   1.248  	CoreWindow::GetForCurrentThread()->Activate();
     2.1 --- a/src/video/windowsrt/SDL_WinRTApp.h	Mon Oct 29 23:32:13 2012 -0400
     2.2 +++ b/src/video/windowsrt/SDL_WinRTApp.h	Sun Nov 04 09:02:58 2012 -0500
     2.3 @@ -39,6 +39,8 @@
     2.4  	void OnPointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args);
     2.5  	void OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args);
     2.6  	void OnPointerMoved(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args);
     2.7 +    void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);
     2.8 +    void OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);
     2.9  
    2.10  private:
    2.11  	CubeRenderer^ m_renderer;