Skip to content

Detecting escape key in InputText #2620

@notnullnotvoid

Description

@notnullnotvoid

Version: 1.70 release
Operating System: macOS 10.14.5
Back-ends: SDL + opengl3

My Issue/Question:

My specific situation is this: I have a few windows which I show when performing some action, like loading or saving a level. The window has a text input, and a "Confirm" and "Cancel" button, standard dialog box stuff. I have already made it so that hitting ENTER in the text input confirms the action and closes the window, and would like to make hitting ESC cancel the action and close the window.

Is there a way to be notified (bool return, callback, etc.) when the ESC key is pressed in an InputText field? Or, is there some way to achieve similar results by different means?

Standalone, minimal, complete and verifiable example:

static bool saveAs = true;
if (saveAs) {
    ImGui::Begin("Save level as...", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
    static char buffer[1024] = {};
    bool enter = ImGui::InputText("Level name", buffer, sizeof(buffer), ImGuiInputTextFlags_EnterReturnsTrue);
    bool click = ImGui::Button("Save");
    ImGui::SameLine();
    if (ImGui::Button("Cancel")) { saveAs = false; buffer[0] = '\0'; }
    if (enter || click) {
        // ... [code that saves the level] ...
        saveAs = false;
        buffer[0] = '\0';
    }
    ImGui::End();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions