polyscope icon indicating copy to clipboard operation
polyscope copied to clipboard

Keyboard monitoring and interaction

Open otmanon opened this issue 4 years ago • 4 comments

polyscope.imgui.IsKeyPressed() exists but doesn't return as expected. It would be really super useful to figure out the bindings to make this work in order have a polyscope application be able to monitor keyboard input.

otmanon avatar Dec 17 '21 14:12 otmanon

Yes!

To support his, I think we need to bind the GLFW keycodes inside of our bindings. However, I think that we should not expose this directly to the users. For instance IsKeyPressed() could take a character as input, then (inside the bindings) translate to the GLFW keycode and call the corresponding imgui function.

@weshoke let me know if you have any thoughts on this!

nmwsharp avatar Dec 17 '21 14:12 nmwsharp

@otmanon It's not the clearest API, but the way ImGui is designed you use the following pattern:

LEFT_ARROW = imgui.GetKeyIndex(imgui.ImGuiKey_LeftArrow)


def cb():
    if imgui.IsKeyPressed(LEFT_ARROW):
        print("LEFT")


pscope.set_user_callback(cb)

ImGui has enums for a set of keys deemed important for UI events. Not all keys are available, just the ones in :

enum ImGuiKey_
{
    ImGuiKey_Tab,
    ImGuiKey_LeftArrow,
    ImGuiKey_RightArrow,
    ImGuiKey_UpArrow,
    ImGuiKey_DownArrow,
    ImGuiKey_PageUp,
    ImGuiKey_PageDown,
    ImGuiKey_Home,
    ImGuiKey_End,
    ImGuiKey_Insert,
    ImGuiKey_Delete,
    ImGuiKey_Backspace,
    ImGuiKey_Space,
    ImGuiKey_Enter,
    ImGuiKey_Escape,
    ImGuiKey_KeyPadEnter,
    ImGuiKey_A,                 // for text edit CTRL+A: select all
    ImGuiKey_C,                 // for text edit CTRL+C: copy
    ImGuiKey_V,                 // for text edit CTRL+V: paste
    ImGuiKey_X,                 // for text edit CTRL+X: cut
    ImGuiKey_Y,                 // for text edit CTRL+Y: redo
    ImGuiKey_Z,                 // for text edit CTRL+Z: undo
    ImGuiKey_COUNT
};

The first chunk of code I pasted above should work. If it doesn't then there's a bug.

There is a function IsKeyPressedMap that would make the above simpler, but it was overlooked in the current bindings on master.

Does this resolve your question?

weshoke avatar Dec 17 '21 17:12 weshoke

You're right! It works! I didn't think to first pass it through imgui.GetKeyIndex()! Thank you :)

otmanon avatar Dec 17 '21 17:12 otmanon

Oh woah, awesome! I didn't realize ImGUI predefined keycode enums. Thanks for the response here.

I'll leave this one open because it would still be nice to set up bindings for other keys some day. Also I should probably add your answer to the docs so others can find it.

nmwsharp avatar Dec 18 '21 03:12 nmwsharp