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
New IO keyboard/mouse/gamepad event API (1.87) recap #4921
Comments
|
Yes, this is a bug. It should be |
|
[OT] Not sure if somebody's interested... but when I saw the new "keyboard rectangle" in imgui_demo.cpp, I've soon tried to "extend" it. Here is what I've done so far [gist link]. |
|
I love it. I'm also so picky creature that I differentiate between ANSI and ISO layouts. :) |
|
IMPORTANT: If you have updated backends between January 10 and January 27 All backends have been updated. This only applies to you if you updated standard or custom backend between January 10 and January 27. If you use standard backends, simply update. If you use a custom backend change code above. |
|
...as far as I can see, in the glfw backend, when we "translate untranslated" keys, numbers in the keypad (= numpad) are converted to (or detected as) normal numbers of the main keyboard block (...well at least in my custom glfw backend). |
|
Not expected behavior; will fix soon! Thank you! |
|
What's the recommended way to remap keys in the new API? From what I can tell, it looks like doing so after this change requires customizing the backend, whereas previously that could be done simply by modifying io.KeyMap to point to a different io.KeysDown? |
|
@PeterJohnson What/why were you remapping before? Everything in |
|
For my use case, there is another application running on the system that has a global (system-wide) listener on the spacebar and Enter keys. While it's relatively easy to avoid the use of spacebar for numeric entry, the use of Enter for committing changes to InputText-based controls is hardcoded, so the easiest workaround was to provide users a way to remap another key (e.g. right ctrl) to the Enter key. Is not acceptable for my use case to commit the value for every keypress. I agree this is a relatively unusual use case and modifying the backend (GLFW in my case) to support key remap is probably the right answer. |
|
Ah yeah, the driver station? I haven't tested it super thoroughly, but this might be good enough for your use-case: //...
ImGui::NewFrame();
if (ImGui::IsKeyPressed(ImGuiKey_RightCtrl))
{
io.AddKeyEvent(ImGuiKey_Enter, true);
io.AddKeyEvent(ImGuiKey_Enter, false);
}
//...Note that this will break using right control for shortcuts. (IE: Right Control + V to paste.) If you want to truly remap it you could install your own Another alternative would be to manipulate (Or like you said you could just modify the GLFW backend, but then you have to deal with merging changes all the time.) (If you want me to elaborate on one of these alternatives it would be better to open a separate issue.) |
|
@ocornut its really cool update but what about key/mouse intercompatibility #4858 (comment) ? |
We discovered a backward-compatibility regression in 1.87
Reads via We'll see if we can come up with a fix. But changing your app to use IsKeyDown() will fix things. |
Fixed by commit 5659db5. |
…4921, ocornut#4858) + Snuck in unrelated comments and removed the "fill once" comment (ocornut#5043)
…ified key enum system. Backends: update most. (ocornut#4921, ocornut#4858) Sorry this is an unusual breaking but since we are WIP it is a good time to make a correction.
…n favor of unified key enum system. (ocornut#4921, ocornut#4858)
…4921, ocornut#4858) + Snuck in unrelated comments and removed the "fill once" comment (ocornut#5043)
… InputText() is not active. (ocornut#4921, ocornut#4858)
|
@ocornut I think that the doc about porting (for instance)
should mention clearly the added constraint
which is now only present as source code assertion |
Thanks for the notice. You are right and I'll add a few comments here and there but I'm not sure they have much value in comparison with the existing assert which makes this explicit for the few users who would be affected. |


EDIT 2022/02/29: We discovered a backward-compatibility regression in 18700:
io.KeysDown[]won't work with new backends.io.KeysDown[GetKeyIndex[XXX])will buffer overflow in old and new backends.Reads via
IsKeyDown()function are always working, this only affect direct array access This was fixed in 18708 (commit 5659db5)(This is a followup on #4858 now that things have settled)
We (@thedmd @ocornut) have completely revamped IO API in 1.87 (the way backend communicate with dear imgui to submit inputs)
All backends have been updated to use new API.
Both user code and old backends should be backward compatible (apart from a few rare edge cases) but we encourage you to transition.
Additions:
io.AddKeyEvent(),io.AddKeyAnalogEvent(),removed jan 27,io.AddKeyModsEvent()io.AddMousePosEvent(),io.AddMouseButtonEvent(),io.AddMouseWheelEvent(),io.SetKeyEventNativeData()(for legacy support).ImGuiKeynow contains a full keyset (e.g.ImGuiKey_F1) this allow using full range of keyboard inputs in a portable manner, which was previously impossible as we encouraged use of native indices. This will make it easier for people to share code/extensions publicly or across multiple codebases.ImGuiKeyalso contains gamepad axises/buttonsImGuiKey_GamepadDpadUp) unifying input sources.io.ConfigInputTrickleEventQueue == false) which massively improve input experience with low framerate.ImGuiKey_Awill be submitted when pressing the key which user would need to press to emit an "A" character, allowing for translated shortcuts.Those changes will constitute the bulk of 1.87.
If you want to help, please upgrade today :)
Transition Guide
If you are using a standard backend (RECOMMENDED) just update it to latest!
If you have are using a custom backend:
Keyboard:
io.KeyMap[],io.KeysDown[]-> backend should callio.AddKeyEvent()io.AddKeyEvent()and you want legacy user code to still work with legacy indices, also callio.SetKeyEventNativeData().IsKeyPressed(MY_NATIVE_KEY_XXX)-> callIsKeyPressed(ImGuiKey_XXX)(OLD CODE WILL STILL WORK*)IsKeyPressed(GetKeyIndex(ImGuiKey_XXX))-> callIsKeyPressed(ImGuiKey_XXX)(OLD CODE WILL STILL WORK*)int user_key_indexnow takeImGuiKey key:IsKeyDown(),IsKeyPressed(),IsKeyReleased(),GetKeyPressedAmount().GetKeyName()helper function.GetKeyIndex(): it is now unnecessary and will now return the same value.(* until
IMGUI_DISABLE_OBSOLETE_KEYIOis set. In a few versions,IMGUI_DISABLE_OBSOLETE_FUNCTIONSwill automatically set it)(* and unless the backend forgot to call
io.SetKeyEventNativeData())Mouse:
io.MousePos-> backend should callio.AddMousePosEvent()io.MouseDown[]-> backend should callio.AddMouseButtonEvent()io.MouseWheel,io.MouseWheelH-> backend should callio.AddMouseWheelEvent()io.MouseHoveredViewport-> backend should callio.AddMouseViewportEvent()Gamepad/Nav:
io.NavInputs[]-> backend should callio.AddKeyEvent()/io.AddKeyAnalogEvent()withImGuiKey_GamepadXXXvalues.What is input queue trickling?
TL;DR; if you submit "Key A went down, then went up, then went down, then went up" during the same Dear ImGui frame, previously events would be missed and it was the application/backend responsibility to submit those events wisely. We moved that responsibility to the core library now. Because most of Dear ImGui code and widgets are reading inputs levels (rather than events), we spread those events over multiple frames when necessary. This makes dear imgui easier to use at low framerate (e.g. 10 FPS).
Backends polling inputs vs handling events
One gotcha is that if you submit events it is preferable to submit then in the right order.
If you transition to new API make sure you are submitting events in the order they come from your OS / platform layer. Normally this should be the default. But because we are punks, our old backends had a tendency to be polling some input state instead of reacting to events, which effectively "lost" their order (e.g. which have changed first, Mouse Position or Mouse Button state?). We have now transitioned all standard backends to be reacting on events rather than polling. If you used your own backend maybe you already behaved better.
Demo
You can see pressed keys in the demo + drawing a small section of a keyboard for no other reason that it being fun.

Reference changes (e.g. to platform backends)
ALWAYS REFER TO FINAL VERSION AS SOME MINOR STUFF MAY HAVE BEEN RENAMED/CHANGED SINCE THE INTERMEDIARY COMMITS. Those references commits are here to help understanding what changed, if you need to update a custom backend.
Core lib
Win32 backend
GLFW backend
SDL backend
Other backends
List of keys
Questions?
(Will be updated over time)
Q: It is ok to call
io.AddXXXEvent()functions every frame with unchanged data?A: Yes.
The text was updated successfully, but these errors were encountered: