Improved Windows keyboard input

- added handling of syskey-messages (alt-Key now generates Key events)
- right and left variants of keys now generate seperate events (e.g. left shift and right shift are seperate keys now instead of simply being "shift")
- added method to get the localized name of a Key
This commit is contained in:
Simon Lübeß
2021-03-14 17:59:13 +01:00
parent cfff2cb738
commit 2ad45edef5
3 changed files with 80 additions and 4 deletions
@@ -11,6 +11,8 @@ using GlitchyEngine.Math;
using GlitchyEngine.Renderer;
using static System.Windows;
using internal GlitchyEngine;
namespace GlitchyEngine
{
/// The windows specific Window implementation
@@ -338,14 +340,16 @@ namespace GlitchyEngine
//// Keyboard input
////
case WM_KEYDOWN:
case WM_KEYDOWN, WM_SYSKEYDOWN:
{
var event = scope KeyPressedEvent((Key)wParam, (int32)lParam & 0xFFFF);
Key key = WindowMessageToKey(wParam, lParam);
var event = scope KeyPressedEvent(key, (int32)lParam & 0xFFFF);
window._eventCallback(event);
}
case WM_KEYUP:
case WM_KEYUP, WM_SYSKEYUP:
{
var event = scope KeyReleasedEvent((Key)wParam);
Key key = WindowMessageToKey(wParam, lParam);
var event = scope KeyReleasedEvent(key);
window._eventCallback(event);
}
@@ -472,6 +476,29 @@ namespace GlitchyEngine
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
/**
* Converts the wParam and lParam of a WM_KEYDOWN or WM_KEYUP message to the corresponding engine keycode.
*/
static Key WindowMessageToKey(WPARAM wParam, LPARAM lParam)
{
Key key = Key.FromWindowsKeyCode((.)wParam);
var scancode = (lParam & 0x00ff0000) >> 16;
bool isExtended = (lParam & 0x01000000) > 0;
switch(key)
{
case .Shift:
return Key.FromWindowsKeyCode((.)MapVirtualKeyW((.)scancode, MAPVK_VSC_TO_VK_EX));
case .Control:
return isExtended ? .RightControl : .LeftControl;
case .Alt:
return isExtended ? .RightAlt : .LeftAlt;
default:
return key;
}
}
public override void Update()
{
Message message = .();