From d1ad945268bc79b80c8767c42bacca0c520665d9 Mon Sep 17 00:00:00 2001 From: Jack Zhang <954294627@qq.com> Date: Sun, 1 May 2022 19:58:45 +0800 Subject: [PATCH 1/2] Fixed bug : ImGui.IsKeyPressed(uint user_key_index) API didn't work with some native keys. --- src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs b/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs index 829b83b8..8d427916 100644 --- a/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs +++ b/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs @@ -216,9 +216,17 @@ protected virtual void UpdateInput() var mouse = Mouse.GetState(); var keyboard = Keyboard.GetState(); - for (int i = 0; i < _keys.Count; i++) + // This implementation is WRONG, because it only handled ImGuiKey-related native key, and it didn't handle other native keys. + //for (int i = 0; i < _keys.Count; i++) + //{ + // io.KeysDown[_keys[i]] = keyboard.IsKeyDown((Keys)_keys[i]); + //} + + // Here is my implementation, which works fine when you call ImGui.IsKeyPressed(uint user_key_index) API. + var pressedKeys = keyboard.GetPressedKeys(); + foreach (var pressedKey in pressedKeys) { - io.KeysDown[_keys[i]] = keyboard.IsKeyDown((Keys)_keys[i]); + io.KeysDown[(int)pressedKey] = true; } io.KeyShift = keyboard.IsKeyDown(Keys.LeftShift) || keyboard.IsKeyDown(Keys.RightShift); From 128a596e41bdc5283fe5dac282d68656b7024e3b Mon Sep 17 00:00:00 2001 From: Jack Zhang <954294627@qq.com> Date: Sun, 1 May 2022 20:16:45 +0800 Subject: [PATCH 2/2] Fixed bug : ImGui.IsKeyPressed(uint user_key_index) API didn't work with some native keys. --- src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs b/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs index 8d427916..0f9a8be6 100644 --- a/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs +++ b/src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs @@ -223,6 +223,14 @@ protected virtual void UpdateInput() //} // Here is my implementation, which works fine when you call ImGui.IsKeyPressed(uint user_key_index) API. + + // Set all of the XNA native keys' state to NotKeyDown. + for (var key = Keys.None; key <= Keys.OemEnlW; key++) + { + io.KeysDown[(int)key] = false; + } + + // Set io.KeysDown[nativeKeyIndex] from XNA keyboard state. var pressedKeys = keyboard.GetPressedKeys(); foreach (var pressedKey in pressedKeys) {