From c730c0214abfe851555df5793d4aca82782be1d0 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 25 Jan 2021 20:29:22 +0100 Subject: Fix input handling for Windows Windows generate keydown events for the last pressed key only If we are pressing two key together this is troublesome as we don't get notified we are pressing two keys Fixed by updating an array of key states on key down/release and generating event while the key state is pressed --- src/window.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'src/window.cpp') diff --git a/src/window.cpp b/src/window.cpp index 273d7e9..840d570 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -227,6 +227,17 @@ window::~window() {} void window::poll_events() { MSG msg = {}; + // Generate keydown events pressed keys + for (size_t virtual_key = KEYS::SHIFT; virtual_key < KEYS::MAX_KEYS; virtual_key++) { + if (keyboard[virtual_key]) { + events.push_back({ + .key = (KEYS)virtual_key, + .type = EVENT_TYPES::KEY_PRESS, + }); + } + } + + while (PeekMessage(&msg, win_handle, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); @@ -255,26 +266,22 @@ LRESULT window::message_handler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lpar } case WM_KEYDOWN: { - auto key_press_event = event { - .key = (KEYS)wparam, - .type = EVENT_TYPES::KEY_PRESS, - }; - keyboard[wparam] = true; - events.push_back(key_press_event); + events.push_back({ + .key = (KEYS)wparam, + .type = EVENT_TYPES::KEY_PRESS, + }); break; } case WM_KEYUP: { - auto key_press_event = event { - .key = (KEYS)wparam, - .type = EVENT_TYPES::KEY_RELEASE, - }; - keyboard[wparam] = false; - events.push_back(key_press_event); + events.push_back({ + .key = (KEYS)wparam, + .type = EVENT_TYPES::KEY_RELEASE, + }); break; } -- cgit v1.3