UWP: Use Windows.UI.Core.CoreDispatcher.AcceleratorKeyActivated event for keyboard

Only in this case we can see Left Alt and Right Alt keys.

Stole the idea from DirectXTK12 repo:
https://github.com/microsoft/DirectXTK12/blob/main/Src/Keyboard.cpp
This commit is contained in:
Dimitriy Ryazantcev
2023-11-28 20:23:57 +02:00
committed by Sam Lantinga
parent 309ea2d5f9
commit 9a206adbee
4 changed files with 28 additions and 65 deletions

View File

@@ -296,11 +296,8 @@ void SDL_WinRTApp::SetWindow(CoreWindow ^ window)
ref new TypedEventHandler<MouseDevice ^, MouseEventArgs ^>(this, &SDL_WinRTApp::OnMouseMoved); ref new TypedEventHandler<MouseDevice ^, MouseEventArgs ^>(this, &SDL_WinRTApp::OnMouseMoved);
#endif #endif
window->KeyDown += window->Dispatcher->AcceleratorKeyActivated +=
ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &SDL_WinRTApp::OnKeyDown); ref new TypedEventHandler<CoreDispatcher ^, AcceleratorKeyEventArgs ^>(this, &SDL_WinRTApp::OnAcceleratorKeyActivated);
window->KeyUp +=
ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &SDL_WinRTApp::OnKeyUp);
window->CharacterReceived += window->CharacterReceived +=
ref new TypedEventHandler<CoreWindow ^, CharacterReceivedEventArgs ^>(this, &SDL_WinRTApp::OnCharacterReceived); ref new TypedEventHandler<CoreWindow ^, CharacterReceivedEventArgs ^>(this, &SDL_WinRTApp::OnCharacterReceived);
@@ -720,14 +717,9 @@ void SDL_WinRTApp::OnMouseMoved(MouseDevice ^ mouseDevice, MouseEventArgs ^ args
WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args); WINRT_ProcessMouseMovedEvent(WINRT_GlobalSDLWindow, args);
} }
void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) void SDL_WinRTApp::OnAcceleratorKeyActivated(Windows::UI::Core::CoreDispatcher ^ sender, Windows::UI::Core::AcceleratorKeyEventArgs ^ args)
{ {
WINRT_ProcessKeyDownEvent(args); WINRT_ProcessAcceleratorKeyActivated(args);
}
void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args)
{
WINRT_ProcessKeyUpEvent(args);
} }
void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::CharacterReceivedEventArgs ^ args) void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::CharacterReceivedEventArgs ^ args)

View File

@@ -71,8 +71,7 @@ ref class SDL_WinRTApp sealed : public Windows::ApplicationModel::Core::IFramewo
void OnPointerEntered(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args); void OnPointerEntered(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args);
void OnPointerExited(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args); void OnPointerExited(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args);
void OnMouseMoved(Windows::Devices::Input::MouseDevice ^ mouseDevice, Windows::Devices::Input::MouseEventArgs ^ args); void OnMouseMoved(Windows::Devices::Input::MouseDevice ^ mouseDevice, Windows::Devices::Input::MouseEventArgs ^ args);
void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args); void OnAcceleratorKeyActivated(Windows::UI::Core::CoreDispatcher ^ sender, Windows::UI::Core::AcceleratorKeyEventArgs ^ args);
void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args);
void OnCharacterReceived(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::CharacterReceivedEventArgs ^ args); void OnCharacterReceived(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::CharacterReceivedEventArgs ^ args);
#if NTDDI_VERSION >= NTDDI_WIN10 #if NTDDI_VERSION >= NTDDI_WIN10

View File

@@ -63,8 +63,7 @@ extern void WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::U
extern void WINRT_ProcessMouseMovedEvent(SDL_Window *window, Windows::Devices::Input::MouseEventArgs ^ args); extern void WINRT_ProcessMouseMovedEvent(SDL_Window *window, Windows::Devices::Input::MouseEventArgs ^ args);
/* Keyboard */ /* Keyboard */
extern void WINRT_ProcessKeyDownEvent(Windows::UI::Core::KeyEventArgs ^ args); extern void WINRT_ProcessAcceleratorKeyActivated(Windows::UI::Core::AcceleratorKeyEventArgs ^ args);
extern void WINRT_ProcessKeyUpEvent(Windows::UI::Core::KeyEventArgs ^ args);
extern void WINRT_ProcessCharacterReceivedEvent(Windows::UI::Core::CharacterReceivedEventArgs ^ args); extern void WINRT_ProcessCharacterReceivedEvent(Windows::UI::Core::CharacterReceivedEventArgs ^ args);
#if NTDDI_VERSION >= NTDDI_WIN10 #if NTDDI_VERSION >= NTDDI_WIN10

View File

@@ -34,16 +34,16 @@ extern "C" {
#include "../../events/SDL_keyboard_c.h" #include "../../events/SDL_keyboard_c.h"
} }
static SDL_Scancode WINRT_TranslateKeycode(Windows::UI::Core::KeyEventArgs ^ args) static SDL_Scancode WINRT_TranslateKeycode(Windows::System::VirtualKey virtualKey, const Windows::UI::Core::CorePhysicalKeyStatus& keyStatus)
{ {
SDL_Scancode code; SDL_Scancode code;
Uint8 index; Uint8 index;
Uint16 scanCode = args->KeyStatus.ScanCode | (args->KeyStatus.IsExtendedKey ? 0xe000 : 0); Uint16 scanCode = keyStatus.ScanCode | (keyStatus.IsExtendedKey ? 0xe000 : 0);
/* Pause/Break key have a special scan code with 0xe1 prefix /* Pause/Break key have a special scan code with 0xe1 prefix
* that is not properly reported under UWP. * that is not properly reported under UWP.
* Use Pause scan code that is used in Win32. */ * Use Pause scan code that is used in Win32. */
if (args->VirtualKey == Windows::System::VirtualKey::Pause) { if (virtualKey == Windows::System::VirtualKey::Pause) {
scanCode = 0xe046; scanCode = 0xe046;
} }
@@ -54,55 +54,28 @@ static SDL_Scancode WINRT_TranslateKeycode(Windows::UI::Core::KeyEventArgs ^ arg
return code; return code;
} }
void WINRT_ProcessKeyDownEvent(Windows::UI::Core::KeyEventArgs ^ args) void WINRT_ProcessAcceleratorKeyActivated(Windows::UI::Core::AcceleratorKeyEventArgs ^ args)
{ {
SDL_Scancode sdlScancode = WINRT_TranslateKeycode(args); using namespace Windows::UI::Core;
#if 0 Uint8 state;
SDL_Keycode keycode = SDL_GetKeyFromScancode(sdlScancode); SDL_Scancode code;
SDL_Log("key down, handled=%s, ext?=%s, released?=%s, menu key down?=%s, "
"repeat count=%d, native scan code=0x%x, was down?=%s, vkey=%d, " switch (args->EventType) {
"sdl scan code=%d (%s), sdl key code=%d (%s)\n", case CoreAcceleratorKeyEventType::SystemKeyDown:
(args->Handled ? "1" : "0"), case CoreAcceleratorKeyEventType::KeyDown:
(args->KeyStatus.IsExtendedKey ? "1" : "0"), state = SDL_PRESSED;
(args->KeyStatus.IsKeyReleased ? "1" : "0"), break;
(args->KeyStatus.IsMenuKeyDown ? "1" : "0"), case CoreAcceleratorKeyEventType::SystemKeyUp:
args->KeyStatus.RepeatCount, case CoreAcceleratorKeyEventType::KeyUp:
args->KeyStatus.ScanCode, state = SDL_RELEASED;
(args->KeyStatus.WasKeyDown ? "1" : "0"), break;
args->VirtualKey, default:
sdlScancode, return;
SDL_GetScancodeName(sdlScancode),
keycode,
SDL_GetKeyName(keycode));
//args->Handled = true;
#endif
SDL_SendKeyboardKey(0, SDL_PRESSED, sdlScancode);
} }
void WINRT_ProcessKeyUpEvent(Windows::UI::Core::KeyEventArgs ^ args) code = WINRT_TranslateKeycode(args->VirtualKey, args->KeyStatus);
{ SDL_SendKeyboardKey(0, state, code);
SDL_Scancode sdlScancode = WINRT_TranslateKeycode(args);
#if 0
SDL_Keycode keycode = SDL_GetKeyFromScancode(sdlScancode);
SDL_Log("key up, handled=%s, ext?=%s, released?=%s, menu key down?=%s, "
"repeat count=%d, native scan code=0x%x, was down?=%s, vkey=%d, "
"sdl scan code=%d (%s), sdl key code=%d (%s)\n",
(args->Handled ? "1" : "0"),
(args->KeyStatus.IsExtendedKey ? "1" : "0"),
(args->KeyStatus.IsKeyReleased ? "1" : "0"),
(args->KeyStatus.IsMenuKeyDown ? "1" : "0"),
args->KeyStatus.RepeatCount,
args->KeyStatus.ScanCode,
(args->KeyStatus.WasKeyDown ? "1" : "0"),
args->VirtualKey,
sdlScancode,
SDL_GetScancodeName(sdlScancode),
keycode,
SDL_GetKeyName(keycode));
//args->Handled = true;
#endif
SDL_SendKeyboardKey(0, SDL_RELEASED, sdlScancode);
} }
void WINRT_ProcessCharacterReceivedEvent(Windows::UI::Core::CharacterReceivedEventArgs ^ args) void WINRT_ProcessCharacterReceivedEvent(Windows::UI::Core::CharacterReceivedEventArgs ^ args)