Skip to content

Commit cfae5ac

Browse files
committed
Backends: make ImGui_ImplSDL2_KeyEventToImGuiKey(), ImGui_ImplSDL3_KeyEventToImGuiKey(), ImGui_ImplGlfw_KeyToImGuiKey(), ImGui_ImplWin32_KeyEventToImGuiKey(), ImGui_ImplAllegro5_KeyCodeToImGuiKey(), ImGui_ImplOSX_KeyCodeToImGuiKey(), non-static. (#7997)
Backends: Win32: Refactor ImGui_ImplWin32_KeyEventToImGuiKey() logic. Ref #7672
1 parent f7ba645 commit cfae5ac

6 files changed

+25
-18
lines changed

backends/imgui_impl_allegro5.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ static void ImGui_ImplAllegro5_SetClipboardText(ImGuiContext*, const char* text)
310310
}
311311
#endif
312312

313-
static ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code)
313+
// Not static to allow third-party code to use that if they want to (but undocumented)
314+
ImGuiKey ImGui_ImplAllegro5_KeyCodeToImGuiKey(int key_code)
314315
{
315316
switch (key_code)
316317
{

backends/imgui_impl_glfw.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,12 @@ static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData()
185185
}
186186

187187
// Functions
188-
static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key)
188+
189+
// Not static to allow third-party code to use that if they want to (but undocumented)
190+
ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int keycode, int scancode)
189191
{
190-
switch (key)
192+
IM_UNUSED(scancode);
193+
switch (keycode)
191194
{
192195
case GLFW_KEY_TAB: return ImGuiKey_Tab;
193196
case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow;
@@ -355,6 +358,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo
355358
io.AddMouseWheelEvent((float)xoffset, (float)yoffset);
356359
}
357360

361+
// FIXME: should this be baked into ImGui_ImplGlfw_KeyToImGuiKey()? then what about the values passed to io.SetKeyEventNativeData()?
358362
static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode)
359363
{
360364
#if GLFW_HAS_GETKEYNAME && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3)
@@ -402,7 +406,7 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, i
402406
keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode);
403407

404408
ImGuiIO& io = ImGui::GetIO();
405-
ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode);
409+
ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode, scancode);
406410
io.AddKeyEvent(imgui_key, (action == GLFW_PRESS));
407411
io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code)
408412
}

backends/imgui_impl_osx.mm

+3-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ - (void)onApplicationBecomeInactive:(NSNotification*)aNotification
259259
@end
260260

261261
// Functions
262-
static ImGuiKey ImGui_ImplOSX_KeyCodeToImGuiKey(int key_code)
262+
263+
// Not static to allow third-party code to use that if they want to (but undocumented)
264+
ImGuiKey ImGui_ImplOSX_KeyCodeToImGuiKey(int key_code)
263265
{
264266
switch (key_code)
265267
{

backends/imgui_impl_sdl2.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImG
180180
}
181181
}
182182

183-
static ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
183+
// Not static to allow third-party code to use that if they want to (but undocumented)
184+
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
184185
{
185186
IM_UNUSED(scancode);
186187
switch (keycode)

backends/imgui_impl_sdl3.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* view
159159
}
160160
}
161161

162-
static ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
162+
// Not static to allow third-party code to use that if they want to (but undocumented)
163+
ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
163164
{
164165
// Keypad doesn't have individual key values in SDL3
165166
switch (scancode)

backends/imgui_impl_win32.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,14 @@ void ImGui_ImplWin32_NewFrame()
419419
ImGui_ImplWin32_UpdateGamepads();
420420
}
421421

422-
// There is no distinct VK_xxx for keypad enter, instead it is VK_RETURN + KF_EXTENDED, we assign it an arbitrary value to make code more readable (VK_ codes go up to 255)
423-
#define IM_VK_KEYPAD_ENTER (VK_RETURN + 256)
424-
425422
// Map VK_xxx to ImGuiKey_xxx.
426-
static ImGuiKey ImGui_ImplWin32_VirtualKeyToImGuiKey(WPARAM wParam)
423+
// Not static to allow third-party code to use that if they want to (but undocumented)
424+
ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam)
427425
{
426+
// There is no distinct VK_xxx for keypad enter, instead it is VK_RETURN + KF_EXTENDED.
427+
if ((wParam == VK_RETURN) && (HIWORD(lParam) & KF_EXTENDED))
428+
return ImGuiKey_KeypadEnter;
429+
428430
switch (wParam)
429431
{
430432
case VK_TAB: return ImGuiKey_Tab;
@@ -473,7 +475,6 @@ static ImGuiKey ImGui_ImplWin32_VirtualKeyToImGuiKey(WPARAM wParam)
473475
case VK_MULTIPLY: return ImGuiKey_KeypadMultiply;
474476
case VK_SUBTRACT: return ImGuiKey_KeypadSubtract;
475477
case VK_ADD: return ImGuiKey_KeypadAdd;
476-
case IM_VK_KEYPAD_ENTER: return ImGuiKey_KeypadEnter;
477478
case VK_LSHIFT: return ImGuiKey_LeftShift;
478479
case VK_LCONTROL: return ImGuiKey_LeftCtrl;
479480
case VK_LMENU: return ImGuiKey_LeftAlt;
@@ -692,12 +693,9 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARA
692693
// Submit modifiers
693694
ImGui_ImplWin32_UpdateKeyModifiers();
694695

695-
// Obtain virtual key code
696-
// (keypad enter doesn't have its own... VK_RETURN with KF_EXTENDED flag means keypad enter, see IM_VK_KEYPAD_ENTER definition for details, it is mapped to ImGuiKey_KeyPadEnter.)
697-
int vk = (int)wParam;
698-
if ((wParam == VK_RETURN) && (HIWORD(lParam) & KF_EXTENDED))
699-
vk = IM_VK_KEYPAD_ENTER;
700-
const ImGuiKey key = ImGui_ImplWin32_VirtualKeyToImGuiKey(vk);
696+
// Obtain virtual key code and convert to ImGuiKey
697+
const ImGuiKey key = ImGui_ImplWin32_KeyEventToImGuiKey(wParam, lParam);
698+
const int vk = (int)wParam;
701699
const int scancode = (int)LOBYTE(HIWORD(lParam));
702700

703701
// Special behavior for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit the key down event.

0 commit comments

Comments
 (0)