using Input System version 1.0.2, Unity 2021.1.7f1 targeting WebGL, I’ve encountered the following problem:
when the WebGL canvas loses focus (e.g. by clicking outside) while keys are pressed, they get stuck in this pressed state. EDIT: it’s not just focus loss causing this and in some cases OnApplicationFocus doesn’t get called at all but keys stay stuck, see post #4.
From this thread , I think the underlying problem is already known:
I’m looking for a workaround, essentially resetting the keyboard state on focus loss. Two workarounds are proposed in the linked thread, but the first one involves changing settings that I don’t have (and would trigger upon regaining focus, not when losing it). The second one is unclear to me:
If I understand that correctly, I would be calling native code and thus no longer be platform-independent.
I also tried calling InputSystem.TryResetDevice(Keyboard.current) without success, it always returns false and the keys stay “stuck”.
I checked the workaround from the other thread I mentioned before (setting “Background Behaviour” to “Reset And Disable All Devices” in the Input System settings in 2021.2+). Same problem as before though, so:
Bump!
I don’t think so, but this “motivated” me to look at the problem again. I may have generalized too much in the opening post, I now have two more specific cases where the “keys get stuck” problem happens:
When holding a key (let’s say W for walking forward) and - without releasing it - left-clicking on something that also takes keyboard input, e.g. the URL bar of the browser or console input line of the developer tools. This triggers a call to OnApplicationFocus signaling focus loss and stops input, but when focus is regained, e.g. by clicking inside the WebGL canvas, the input resumes even if the keys are no longer pressed (meaning my player rig walks forward on its own). The “normal” state can only be recovered by pressing and releasing the “stuck” key again.
The more irritating case for me: when releasing the right mouse button with the mouse outside the WebGL canvas, pressed keys get stuck. Focus is not lost during this, so the input doesn’t stop temporarily like in the first case. This happens regularly, because holding the right mouse button and moving the mouse is how I implemented looking around. Combined with WASD for walking, I often let go of the right mouse buttons outside of the canvas and get stuck walking onwards.
Experimenting around specifically with the second case, I had a hunch that the right click popup menu is making trouble (or at least preventing the correct detection of focus loss). So I’m cautiously optimistic that suppressing the context menu could be the workaround I’m looking for. For now, I’m doing that with JavaScript like this: