Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect input counts over one frame to avoid missing inputs within the same frame #3383

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions examples/imgui_impl_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,15 @@ IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARA
return 0;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (wParam < 256)
io.KeysDown[wParam] = 1;
// Win32 sets bit 30 if the key is repeating. We handle that internally so we
// only want the first down.
if ((wParam < 256) && !(lParam & (1 << 30)))
io.KeysDown[wParam]++;
return 0;
case WM_KEYUP:
case WM_SYSKEYUP:
if (wParam < 256)
io.KeysDown[wParam] = 0;
io.KeysUp[wParam]++;
return 0;
case WM_CHAR:
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.
Expand Down
14 changes: 12 additions & 2 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3758,8 +3758,18 @@ void ImGui::NewFrame()
// Synchronize io.KeyMods with individual modifiers io.KeyXXX bools
g.IO.KeyMods = GetMergedKeyModFlags();
memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));
for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)
g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f;
for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) {
g.IO.KeysDown[i] -= g.IO.KeysUpPrev[i];
if(g.IO.KeysDown[i])
if(g.IO.KeysDownDuration[i] < 0.0f)
g.IO.KeysDownDuration[i] = 0.0f;
else
g.IO.KeysDownDuration[i] += g.IO.DeltaTime;
else
g.IO.KeysDownDuration[i] = -1.0f;
}
memcpy(g.IO.KeysUpPrev, g.IO.KeysUp, sizeof(g.IO.KeysUp));
memset(g.IO.KeysUp, 0, sizeof(g.IO.KeysUp));

// Update gamepad/keyboard navigation
NavUpdate();
Expand Down
23 changes: 13 additions & 10 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1525,16 +1525,19 @@ struct ImGuiIO
// Input - Fill before calling NewFrame()
//------------------------------------------------------------------

ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.)
bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API.
float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text.
float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends.
bool KeyCtrl; // Keyboard modifier pressed: Control
bool KeyShift; // Keyboard modifier pressed: Shift
bool KeyAlt; // Keyboard modifier pressed: Alt
bool KeySuper; // Keyboard modifier pressed: Cmd/Super/Windows
bool KeysDown[512]; // Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys).
float NavInputs[ImGuiNavInput_COUNT]; // Gamepad inputs. Cleared back to zero by EndFrame(). Keyboard keys will be auto-mapped and be written here by NewFrame().
ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.)
bool MouseDown[5]; // Mouse buttons: 0 = left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API.
bool MouseUp[5]; // Mouse buttons: 0 = left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API.
float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text.
float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends.
bool KeyCtrl; // Keyboard modifier pressed: Control
bool KeyShift; // Keyboard modifier pressed: Shift
bool KeyAlt; // Keyboard modifier pressed: Alt
bool KeySuper; // Keyboard modifier pressed: Cmd/Super/Windows
unsigned char KeysDown[512]; // Number of keyboard key inputs that are pressed since last frame (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys).
unsigned char KeysUp[512]; // Number of keyboard up inputs since last frame
unsigned char KeysUpPrev[512]; // Number of keyboard up inputs since last frame - 1
float NavInputs[ImGuiNavInput_COUNT]; // Gamepad inputs. Cleared back to zero by EndFrame(). Keyboard keys will be auto-mapped and be written here by NewFrame().

// Functions
IMGUI_API void AddInputCharacter(unsigned int c); // Queue new character input
Expand Down