-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Fixed modifier key state setting in GLFW callbacks. #183
Conversation
Hmm, this is odd. Perhaps it should be raised to GLFW first ? |
I'll try to see if this is a GLFW issue. Most of the time I don't use the sample program myself, since I usually just rebuild our game to check out new features and we have our own handlers for all of these. |
I talked with one of the glfw maintainers and they pointed out a bug in my original code where hitting the two of the same modifiers (left + right alt, for example) would not be detected correctly. Furthermore, I discovered that the reason why Mac and Linux behave differently is that they see the modifier differently in the callback function. Using the events sample program from glfw on Mac yields the following sequence:
Meanwhile, my Linux system does:
As you can see, the modifier is detected only when the key is released on Linux but Mac sees it when pressed. They told me that the modifiers are only valid for the key event and this particular behavior is actually expected. The suggestion to work around this was to grab the last reported key state by calling glfwGetKey(), which reports whether the key was previously pressed or released, which is what my new commit uses. |
I'm merging but I still don't understand this. What does "for the key event" means? Is the discussion on their github? (couldn't find it) |
i am not convinced blindly merging this was a good idea ,) |
Well, it is a workaround for how GLFW functions. I don't understand why GLFW would provide mods this way. We have just changed this:
To that:
I must prefer the early obviously, but it's not a big deal. Still, I would like to undersstand the behavior. |
I spoke with dreda directly on their IRC channel yesterday, so you won't find anything on their GitHub. I still suspect something is amiss in glfw because my understanding of the mods parameter is that it's only provided as extra information to go along with a particular key event, and should not be used to directly set any key state, but the mods parameter behaves in a way that's not consistent and in some cases, not intuitive. For example, I can use it to know if a mod key was pressed/released when a non-modifier key is pressed/released (such as "A"), but I should not use it to try to detect if a mod key was pressed or released with this mechanism since the actual press/release of the modifier key generates its own event. If you want to know whether a key was pressed or released, you should use its key callback directly (GLFW_KEY_*) instead. But looking at the events output from my Mac and Linux machines, you can see how the reversed reporting of the mods parameter makes this really inconsistent and propagates bugs into user applications. |
Sorry, that last paragraph was really bad. The distinction I was trying to make is that you should not try to detect state changes of a mod key through the mods parameter, which is basically what we were trying to do. We're interested in knowing when exactly the modifier keys are released or pressed which, apparently, can only be known by handling the specific GLFW_KEY_* event and checking for its pressed/released. But if mods were simply reported like the Mac does across all systems, then I think we could use the old callback code and everything would be fine. Since Linux does not report it in this way and tells us a mod key was pressed at the same time it was released, our code breaks. An alternative might be:
|
You mean inside ImGui_ImplGlfw_NewFrame() ? Yeah maybe that's shorter and perhaps match closer than the end-user would do with their own engine. If you don't mind double checking under Mac/Linux (no hurry) to put that under the " // Setup inputs " block I'll push that change. Thanks! |
I was thinking just inside the key callback, right now I have:
Which is fine on Linux, about to check Mac... |
Yes that should be fine, you are right. |
Examples: GLFW Set modifier key state by inspecting imgui's io.KeysDown array (see #183)
Thanks! |
On my Linux system, GLFW 3.1.1, the key callback handles modifier keys incorrectly. Pressing the modifier will set the flag true, but releasing it does not. The only way to unset the modifiers is to hit a non-modifier key.
This issue does not seem to be present on my MacBook Pro.