@@ -4160,16 +4160,15 @@ static void UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt)
4160
4160
rt->Entries.swap(rt->EntriesNext); // Swap new and old indexes
4161
4161
}
4162
4162
4163
- // [Internal] Do not use directly (should read io.KeyMods instead)
4164
- static ImGuiKeyChord GetMergedModsFromBools ()
4163
+ // [Internal] Do not use directly
4164
+ static ImGuiKeyChord GetMergedModsFromKeys ()
4165
4165
{
4166
- ImGuiContext& g = *GImGui;
4167
- ImGuiKeyChord key_chord = 0;
4168
- if (g.IO.KeyCtrl) { key_chord |= ImGuiMod_Ctrl; }
4169
- if (g.IO.KeyShift) { key_chord |= ImGuiMod_Shift; }
4170
- if (g.IO.KeyAlt) { key_chord |= ImGuiMod_Alt; }
4171
- if (g.IO.KeySuper) { key_chord |= ImGuiMod_Super; }
4172
- return key_chord;
4166
+ ImGuiKeyChord mods = 0;
4167
+ if (ImGui::IsKeyDown(ImGuiMod_Ctrl)) { mods |= ImGuiMod_Ctrl; }
4168
+ if (ImGui::IsKeyDown(ImGuiMod_Shift)) { mods |= ImGuiMod_Shift; }
4169
+ if (ImGui::IsKeyDown(ImGuiMod_Alt)) { mods |= ImGuiMod_Alt; }
4170
+ if (ImGui::IsKeyDown(ImGuiMod_Super)) { mods |= ImGuiMod_Super; }
4171
+ return mods;
4173
4172
}
4174
4173
4175
4174
static void ImGui::UpdateKeyboardInputs()
@@ -4244,13 +4243,22 @@ static void ImGui::UpdateKeyboardInputs()
4244
4243
#endif
4245
4244
#endif
4246
4245
4247
- // Synchronize io.KeyMods with individual modifiers io.KeyXXX bools, update aliases
4248
- io.KeyMods = GetMergedModsFromBools();
4246
+ // Update aliases
4249
4247
for (int n = 0; n < ImGuiMouseButton_COUNT; n++)
4250
4248
UpdateAliasKey(MouseButtonToKey(n), io.MouseDown[n], io.MouseDown[n] ? 1.0f : 0.0f);
4251
4249
UpdateAliasKey(ImGuiKey_MouseWheelX, io.MouseWheelH != 0.0f, io.MouseWheelH);
4252
4250
UpdateAliasKey(ImGuiKey_MouseWheelY, io.MouseWheel != 0.0f, io.MouseWheel);
4253
4251
4252
+ // Synchronize io.KeyMods and io.KeyXXX values.
4253
+ // - New backends (1.87+): send io.AddKeyEvent(ImGuiMod_XXX) -> -> (here) deriving io.KeyMods + io.KeyXXX from key array.
4254
+ // - Legacy backends: set io.KeyXXX bools -> (above) set key array from io.KeyXXX -> (here) deriving io.KeyMods + io.KeyXXX from key array.
4255
+ // So with legacy backends the 4 values will do a unnecessary back-and-forth but it makes the code simpler and future facing.
4256
+ io.KeyMods = GetMergedModsFromKeys();
4257
+ io.KeyCtrl = (io.KeyMods & ImGuiMod_Ctrl) != 0;
4258
+ io.KeyShift = (io.KeyMods & ImGuiMod_Shift) != 0;
4259
+ io.KeyAlt = (io.KeyMods & ImGuiMod_Alt) != 0;
4260
+ io.KeySuper = (io.KeyMods & ImGuiMod_Super) != 0;
4261
+
4254
4262
// Clear gamepad data if disabled
4255
4263
if ((io.BackendFlags & ImGuiBackendFlags_HasGamepad) == 0)
4256
4264
for (int i = ImGuiKey_Gamepad_BEGIN; i < ImGuiKey_Gamepad_END; i++)
@@ -8485,15 +8493,6 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs)
8485
8493
key_changed = true;
8486
8494
key_changed_mask.SetBit(key_data_index);
8487
8495
8488
- if (key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super)
8489
- {
8490
- if (key == ImGuiMod_Ctrl) { io.KeyCtrl = key_data->Down; }
8491
- if (key == ImGuiMod_Shift) { io.KeyShift = key_data->Down; }
8492
- if (key == ImGuiMod_Alt) { io.KeyAlt = key_data->Down; }
8493
- if (key == ImGuiMod_Super) { io.KeySuper = key_data->Down; }
8494
- io.KeyMods = GetMergedModsFromBools();
8495
- }
8496
-
8497
8496
// Allow legacy code using io.KeysDown[GetKeyIndex()] with new backends
8498
8497
#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO
8499
8498
io.KeysDown[key_data_index] = key_data->Down;
@@ -8775,7 +8774,7 @@ static void ImGui::ErrorCheckEndFrameSanityChecks()
8775
8774
// send key release events mid-frame. This would normally trigger this assertion and lead to sheared inputs.
8776
8775
// We silently accommodate for this case by ignoring the case where all io.KeyXXX modifiers were released (aka key_mod_flags == 0),
8777
8776
// while still correctly asserting on mid-frame key press events.
8778
- const ImGuiKeyChord key_mods = GetMergedModsFromBools ();
8777
+ const ImGuiKeyChord key_mods = GetMergedModsFromKeys ();
8779
8778
IM_ASSERT((key_mods == 0 || g.IO.KeyMods == key_mods) && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods");
8780
8779
IM_UNUSED(key_mods);
8781
8780
0 commit comments