Skip to content

Commit d542aa0

Browse files
ocornutbojosos
authored andcommitted
Backends: Win32: Fixed keyboard modifiers being reported when host window doesn't have focus. (ocornut#2622)
1 parent a6e5635 commit d542aa0

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

backends/imgui_impl_win32.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ typedef DWORD (WINAPI *PFN_XInputGetState)(DWORD, XINPUT_STATE*);
3434

3535
// CHANGELOG
3636
// (minor and older changes stripped away, please see git history for details)
37+
// 2021-08-02: Inputs: Fixed keyboard modifiers being reported when host window doesn't have focus.
3738
// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using TrackMouseEvent() to receive WM_MOUSELEAVE events).
3839
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
39-
// 2021-06-08: Fix ImGui_ImplWin32_EnableDpiAwareness() and ImGui_ImplWin32_GetDpiScaleForMonitor() to handle Windows 8.1/10 features without a manifest (per-monitor DPI, and properly calls SetProcessDpiAwareness() on 8.1).
40+
// 2021-06-08: Fixed ImGui_ImplWin32_EnableDpiAwareness() and ImGui_ImplWin32_GetDpiScaleForMonitor() to handle Windows 8.1/10 features without a manifest (per-monitor DPI, and properly calls SetProcessDpiAwareness() on 8.1).
4041
// 2021-03-23: Inputs: Clearing keyboard down array when losing focus (WM_KILLFOCUS).
4142
// 2021-02-18: Added ImGui_ImplWin32_EnableAlphaCompositing(). Non Visual Studio users will need to link with dwmapi.lib (MinGW/gcc: use -ldwmapi).
4243
// 2021-02-17: Fixed ImGui_ImplWin32_EnableDpiAwareness() attempting to get SetProcessDpiAwareness from shcore.dll on Windows 8 whereas it is only supported on Windows 8.1.
@@ -366,13 +367,6 @@ void ImGui_ImplWin32_NewFrame()
366367
io.DeltaTime = (float)(current_time - bd->Time) / bd->TicksPerSecond;
367368
bd->Time = current_time;
368369

369-
// Read keyboard modifiers inputs
370-
io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
371-
io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
372-
io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
373-
io.KeySuper = false;
374-
// io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.
375-
376370
// Update OS mouse position
377371
ImGui_ImplWin32_UpdateMousePos();
378372

@@ -470,17 +464,24 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARA
470464
io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
471465
return 0;
472466
case WM_KEYDOWN:
473-
case WM_SYSKEYDOWN:
474-
if (wParam < 256)
475-
io.KeysDown[wParam] = 1;
476-
return 0;
477467
case WM_KEYUP:
468+
case WM_SYSKEYDOWN:
478469
case WM_SYSKEYUP:
470+
{
471+
bool down = (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN);
479472
if (wParam < 256)
480-
io.KeysDown[wParam] = 0;
473+
io.KeysDown[wParam] = down;
474+
if (wParam == VK_CONTROL)
475+
io.KeyCtrl = down;
476+
if (wParam == VK_SHIFT)
477+
io.KeyShift = down;
478+
if (wParam == VK_MENU)
479+
io.KeyAlt = down;
481480
return 0;
481+
}
482482
case WM_KILLFOCUS:
483483
memset(io.KeysDown, 0, sizeof(io.KeysDown));
484+
io.KeyCtrl = io.KeyShift = io.KeyAlt = io.KeySuper = false;
484485
return 0;
485486
case WM_CHAR:
486487
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.

docs/CHANGELOG.txt

+1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ Other Changes:
368368
- Backends: Win32: IME functions are disabled by default for non-Visual Studio compilers (MinGW etc.). Enable with
369369
'#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS' for those compilers. Undo change from 1.82. (#2590, #738, #4185, #4301)
370370
- Backends: Win32: Mouse position is correctly reported when the host window is hovered but not focused. (#2445, #2696, #3751, #4377)
371+
- Backends: Fixed keyboard modifiers being reported when host window doesn't have focus. (#2622)
371372
- Backends: GLFW: Mouse position is correctly reported when the host window is hovered but not focused. (#3751, #4377, #2445)
372373
(backend now uses glfwSetCursorEnterCallback(). If you called ImGui_ImplGlfw_InitXXX with install_callbacks=false, you will
373374
need to install this callback and forward the data to the backend via ImGui_ImplGlfw_CursorEnterCallback).

0 commit comments

Comments
 (0)