You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Best way to cancel current drag-drop operation with Escape key in Dear ImGui? #9071
I'm just looking for the most idiomatic way to cancel the current drag-drop operation with the Escape key. I searched the issues for about 15 minutes but surprisingly haven't found anything. Currently, Escape doesn't cancel the current drag-drop operation like it does in Windows, so I'm guessing there's some additional code or configuration needed.
But mostly I believe this has been overlooked and we can consider introducing that by default in the codebase, with perhaps an opt-out flag. I dislike having to use IsKeyPressed() in here. Due to a sum of internal factors the right way to use input-routing in this situation would be to do:
Which might be very slightly more correct for some oddball case that is not going to happen, so I'd simply suggest using IsKeyPressed() which is both simpler and more likely to just work everywhere.
ClearDragDrop is currently an internal function (which I'd need to import since my current .NET wrapper doesn't import internals), so maybe the semantics of Escape -> Cancel Drag Drop should just be built into Dear ImGui without the user needing to code anything?
Certainly, but the earlier requires you to add a few line to import, and the later requires me to spend some time designing the feature to ensure it is viable :) I will definitively do it eventually but in the meanwhile you may have a workaround available.
Alright cool, already up and working on my side with just a few lines of import code! Should I close this issue or leave open until you do the built-in version?
I'm concerned as that adds an additional undocumented operation (undocumented inside ui).
... But never mind, without a change in the library, there does seem a workaround to disable the behavior - to exchange away ImGui::GetKeyData(ImGuiKey_Escape)->Down before NewFrame (and restore after it). (This seems to affect NavUpdateCancelRequest, but since the project already disables keyboard navigation, this isn't a issue.)
I'm concerned as that adds an additional undocumented operation (undocumented inside ui).
It seems reasonable that Escape cancels an operation just as it does cancel many others.
The problem here for me is that because there’s an active id, the input routing score is unusually high and less obvious to override (vs other use of Escape for eg Nav).
... But never mind, without a change in the library, there does seem a workaround to disable the behavior - to exchange away ImGui::GetKeyData(ImGuiKey_Escape)->Down before NewFrame
I think ideally the library could centralize keyboard shortcuts the same way as styles and colors (in the current defaults), so users can selectively turn off them (ImGuiKey_None) or use different ones. (How to document may become a headache, may simply explain they are the default settings.)
I don't think the guide can possibly reflect all configuration options nor the possibility that many things may be overridden or disabled and be completely dynamic or local to a given window/applet/tool. It's merely a starting guide for "default settings" instead.
It doesn't seem too crazy or surprising that Escape would cancel an action, it already does it for 6 other things at least.
I think ideally the library could centralize keyboard shortcuts the same way as styles and colors (in the current defaults), so users can selectively turn off them (ImGuiKey_None) or use different ones.
You can use Shortcut() and input routing to disable most shortcuts locally or globally already.
Allowing to change shortcuts would be too difficult and open more problem IMHO. Many aspects of the user interface inputs are very precisely tweaked. It's better to favor cross-app consistency and not facilitate code from different sources to operate neatly.
I honesty don't know what may be done better here.
I have reworked the drag and drop logic to make it more logical to override.
You can do:
if (ImGui::GetDragDropPayload() != NULL)
ImGui::Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverActive);
To disable the logic, and then call if (ImGui::IsKeyPressed()) is you like.
You won't get a true return value from Shortcut() due to internal ActiveIdUsingAllKeyboardKeys logic, it's a little tricky, i'll investigate this if there is an oddly specific need.
I moved the code block before UpdateKeyboardInputs() so i am not sure you previous hack would still work, but using the dummy Shortcut() call should work.
Activity
ocornut commentedon Nov 15, 2025
The current way would be to do:
But mostly I believe this has been overlooked and we can consider introducing that by default in the codebase, with perhaps an opt-out flag. I dislike having to use
IsKeyPressed()in here. Due to a sum of internal factors the right way to use input-routing in this situation would be to do:Which might be very slightly more correct for some oddball case that is not going to happen, so I'd simply suggest using
IsKeyPressed()which is both simpler and more likely to just work everywhere.bryanedds commentedon Nov 15, 2025
ClearDragDrop is currently an internal function (which I'd need to import since my current .NET wrapper doesn't import internals), so maybe the semantics of Escape -> Cancel Drag Drop should just be built into Dear ImGui without the user needing to code anything?
ocornut commentedon Nov 15, 2025
Certainly, but the earlier requires you to add a few line to import, and the later requires me to spend some time designing the feature to ensure it is viable :) I will definitively do it eventually but in the meanwhile you may have a workaround available.
bryanedds commentedon Nov 15, 2025
Alright cool, already up and working on my side with just a few lines of import code! Should I close this issue or leave open until you do the built-in version?
ocornut commentedon Nov 15, 2025
Let’s leave this open as a reminder.
Drag and Drop: Pressing Escape while carrying a payload automatically…
ocornut commentedon Nov 18, 2025
I added this as a stock behavior with 23bd697.
Presently I couldn't see a reason to even add a flag for disabling this, so I'm making it always on.
achabense commentedon Nov 18, 2025
Hello @ocornut, can you add a way to opt-out this behavior? I'm already using right-click to cancel drag event in my project.
ocornut commentedon Nov 18, 2025
@achabense : well, how would it bother you if Escape also did this ?
achabense commentedon Nov 18, 2025
I'm concerned as that adds an additional undocumented operation (undocumented inside ui).
... But never mind, without a change in the library, there does seem a workaround to disable the behavior - to exchange away
ImGui::GetKeyData(ImGuiKey_Escape)->DownbeforeNewFrame(and restore after it). (This seems to affectNavUpdateCancelRequest, but since the project already disables keyboard navigation, this isn't a issue.)ocornut commentedon Nov 18, 2025
It seems reasonable that Escape cancels an operation just as it does cancel many others.
The problem here for me is that because there’s an active id, the input routing score is unusually high and less obvious to override (vs other use of Escape for eg Nav).
That seems hacky and not desirable.
achabense commentedon Nov 18, 2025
ShowUserGuideshould record the new behavior.imgui/imgui_demo.cpp
Line 8639 in 23bd697
I think ideally the library could centralize keyboard shortcuts the same way as styles and colors (in the current defaults), so users can selectively turn off them (
ImGuiKey_None) or use different ones. (How to document may become a headache, may simply explain they are the default settings.)ocornut commentedon Nov 19, 2025
I don't think the guide can possibly reflect all configuration options nor the possibility that many things may be overridden or disabled and be completely dynamic or local to a given window/applet/tool. It's merely a starting guide for "default settings" instead.
It doesn't seem too crazy or surprising that Escape would cancel an action, it already does it for 6 other things at least.
You can use
Shortcut()and input routing to disable most shortcuts locally or globally already.Allowing to change shortcuts would be too difficult and open more problem IMHO. Many aspects of the user interface inputs are very precisely tweaked. It's better to favor cross-app consistency and not facilitate code from different sources to operate neatly.
I honesty don't know what may be done better here.
The only question for me are:
Docs: update ShowUserGuide() + PR guidelines. (#9071)
Drag and Drop: handling of Escape after UpdateKeyboardInputs(). (#9071)
Drag and Drop: rework cancel drag and drop logic to be overridable. (#…
ocornut commentedon Nov 19, 2025
I have reworked the drag and drop logic to make it more logical to override.
You can do:
To disable the logic, and then call
if (ImGui::IsKeyPressed())is you like.You won't get a
truereturn value fromShortcut()due to internal ActiveIdUsingAllKeyboardKeys logic, it's a little tricky, i'll investigate this if there is an oddly specific need.achabense commentedon Nov 19, 2025
Thanks for the effort!
I was wrong. As tested that will just eat up the escape key (and cannot be restored).
ocornut commentedon Nov 19, 2025
I moved the code block before UpdateKeyboardInputs() so i am not sure you previous hack would still work, but using the dummy
Shortcut()call should work.Drag and Drop: Pressing Escape while carrying a payload automatically…
Docs: update ShowUserGuide() + PR guidelines. (ocornut#9071)
Drag and Drop: handling of Escape after UpdateKeyboardInputs(). (ocor…
Drag and Drop: rework cancel drag and drop logic to be overridable. (o…