Skip to content
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

Mouse and keyboard events at the same time #5888

Closed
jzubizarreta opened this issue Nov 15, 2022 · 8 comments
Closed

Mouse and keyboard events at the same time #5888

jzubizarreta opened this issue Nov 15, 2022 · 8 comments

Comments

@jzubizarreta
Copy link

Version/Branch of Dear ImGui:

Version: 1.89
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_vulkan.cpp
Compiler: MSC_VER=1933
Operating System: Windows 10

My Issue/Question:

Hey, I'm trying to obtain mouse and keyboard events when the mouse is hovering a specific window. More specifically, I'm trying to check if both the left mouse button and the ctrl key are down/pressed. However, I'm only getting events from one of them at the same time, even when both are pressed. If I remember well, having both at the same time was possible before so I'm wondering if something changed with the recent version 1.89 that I'm missing. Here is some example code in case it helps:

ImGui::Begin("MyWindow");
if (ImGui::IsItemHovered())
{
  // record events
  const bool isCtrlPressed = ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl);
  const bool isLeftPressed = ImGui::IsMouseClicked(ImGuiMouseButton_Left) || ImGui::IsMouseDragging(ImGuiMouseButton_Left);

  // only one of them is true, even when both are pressed
  std::cerr << isCtrlPressed << ", " << isLeftPressed << std::endl;
}
ImGui::End();

Thanks in advance!

@ocornut
Copy link
Owner

ocornut commented Nov 16, 2022

Hello,

I can confirm there is "kind of" a bug related to the key ownership system, specifically when CLICKING on a window to move it, as the moving-window operation is preventing keys from having other actions (not specifically mods key but any keys).

Simpler repro:

const bool isCtrlDown = ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl);
const bool isLeftPressed = ImGui::IsMouseClicked(ImGuiMouseButton_Left) || ImGui::IsMouseDragging(ImGuiMouseButton_Left);
ImGui::Text("isCtrlDown = %s, isLeftPressed = %s", isCtrlDown ? "Ctrl" : "----", isLeftPressed ? "Left" : "----");

Also note that you are likely to want to use the Mod and not keys:

const bool isCtrlDown = ImGui::IsKeyDown(ImGuiMod_Ctrl);
const bool isLeftPressed = ImGui::IsMouseClicked(ImGuiMouseButton_Left) || ImGui::IsMouseDragging(ImGuiMouseButton_Left);
ImGui::Text("isCtrlDown = %s, isLeftPressed = %s", isCtrlDown ? "Ctrl" : "----", isLeftPressed ? "Left" : "----");

(same issue will appear here but this is simpler)

However note that it doesn't seem regular/wise to react while simultaneously moving a window which is what you are doing here.

Hey, I'm trying to obtain mouse and keyboard events when the mouse is hovering a specific window.

Why? Can you explain the context?
Generally if you'd want to catch a click or drag you'll want an item to drag it (an InvisibleButton() is enough most of the time), otherwise the click will also moving the window.

Right now if you use io.KeyCtrl you'll be able to get the unfiltered input for this, but:

  • It'd be more correct to submit an item to get those inputs.
  • I would still want to specifically get mods to not appear filtered while moving a window.

ocornut added a commit that referenced this issue Nov 16, 2022
…5888, #4921, #456)

Amend change of SetActiveIdUsingAllKeyboardKeys() in 4448d97 which seemingly accidentally reverted the change intended by fd408c9
@ocornut
Copy link
Owner

ocornut commented Nov 16, 2022

Pushed d60985d

Amend change of SetActiveIdUsingAllKeyboardKeys() in 4448d97 which seemingly accidentally reverted the change intended by fd408c9.

Note that your code polling ImGui::IsKeyDown(ImGuiKey_LeftCtrl) won't work while moving a window you should use ImGui::IsKeyDown(ImGuiMod_Ctrl).

I'll keep this open because I would like to investigate SetActiveIdUsingAllKeyboardKeys() which I earlier flagged as rather undesirable. It is not a new feature but since 4448d97 its effect increased from "bit array polled by system code" to "filtering IsKeyXXX functions" which is perhaps not good, it would be more sensible if it at least only affected input-owner aware code.
Sorry this is a lots of jargon, also writing this down for myself..

@ocornut
Copy link
Owner

ocornut commented Nov 16, 2022

I've pushed another fix 7bee9a8 to make the behavior more consistent with pre-4448d97 logic.

ocornut added a commit that referenced this issue Nov 16, 2022
…put-owner-unaware code from accessing keys. (#5888, #4921, #456)

Amend 4448d97. This is more consistent with input owner design.
@jzubizarreta
Copy link
Author

Wow that was fast! I tried with your fixes and it's working :)

My intention is to use the mouse and keyboard to provide navigation in a 3D viewport. Like, for example, rotation with mouse left click and translation with left + Ctrl. For that, I also made it so each window can only be moved using the tittle bar.

Thanks again @ocornut !

@ocornut
Copy link
Owner

ocornut commented Nov 16, 2022

Then you should indeed use InvisibleButton() to grab inputs at that location.

@jzubizarreta
Copy link
Author

jzubizarreta commented Nov 16, 2022

What is the difference of using an InvisibleButton? Not sure how to use it, do you have any link I could look at?
I already have an ImGui::Image rendering a texture in the entire canvas before calling the code from the first post.

Thanks.

@ocornut
Copy link
Owner

ocornut commented Nov 24, 2022

Then call ImageButton().

@jzubizarreta
Copy link
Author

It worked! Thanks @ocornut :)

@ocornut ocornut closed this as completed Mar 23, 2023
kjblanchard pushed a commit to kjblanchard/imgui that referenced this issue May 5, 2023
…cornut#5888, ocornut#4921, ocornut#456)

Amend change of SetActiveIdUsingAllKeyboardKeys() in 4448d97 which seemingly accidentally reverted the change intended by fd408c9
kjblanchard pushed a commit to kjblanchard/imgui that referenced this issue May 5, 2023
…put-owner-unaware code from accessing keys. (ocornut#5888, ocornut#4921, ocornut#456)

Amend 4448d97. This is more consistent with input owner design.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants