Skip to content

Best way to cancel current drag-drop operation with Escape key in Dear ImGui? #9071

Closed

Description

@bryanedds

Version/Branch of Dear ImGui:

imgui docking 1.91.5

Back-ends:

https://github.com/bryanedds/Nu/blob/master/Nu/Nu/Render/RendererImGui.fs

Compiler, OS:

Win 10, VS2022

Details:

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.

Activity

  1. ocornut commented on Nov 15, 2025

    @ocornut
    Owner

    The current way would be to do:

    #include "imgui_internal.h"
    
    [...]
    if (ImGui::GetDragDropPayload() != NULL && ImGui::IsKeyPressed(ImGuiKey_Escape))
    {
        ImGui::ClearActiveID();
        ImGui::ClearDragDrop();
    }

    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:

    if (ImGui::GetDragDropPayload() != NULL && ImGui::Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverActive, ImGui::GetActiveID()))
    {
        ImGui::ClearActiveID();
        ImGui::ClearDragDrop();
    }

    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.

  2. bryanedds commented on Nov 15, 2025

    @bryanedds
    Author

    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?

  3. ocornut commented on Nov 15, 2025

    @ocornut
    Owner

    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.

  4. bryanedds commented on Nov 15, 2025

    @bryanedds
    Author

    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?

  5. ocornut commented on Nov 15, 2025

    @ocornut
    Owner

    Let’s leave this open as a reminder.

  6. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    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.

  7. achabense commented on Nov 18, 2025

    @achabense
    Contributor

    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.

  8. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    @achabense : well, how would it bother you if Escape also did this ?

  9. achabense commented on Nov 18, 2025

    @achabense
    Contributor

    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.)

  10. ocornut commented on Nov 18, 2025

    @ocornut
    Owner

    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

    That seems hacky and not desirable.

  11. achabense commented on Nov 18, 2025

    @achabense
    Contributor

    ShowUserGuide should record the new behavior.

    void ImGui::ShowUserGuide()


    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.)

  12. ocornut commented on Nov 19, 2025

    @ocornut
    Owner

    ShowUserGuide should record the new behavior.

    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.

    The only question for me are:

    • To find if someone truly need to disable this.
    • To make make it easier to disable in code.
  13. ocornut commented on Nov 19, 2025

    @ocornut
    Owner

    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.

  14. achabense commented on Nov 19, 2025

    @achabense
    Contributor

    Thanks for the effort!


    But never mind, without a change in the library, there does seem a workaround to disable the behavior ...

    I was wrong. As tested that will just eat up the escape key (and cannot be restored).

  15. ocornut commented on Nov 19, 2025

    @ocornut
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址