Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Keyboard input gets "stuck" when WebGL canvas loses focus

Discussion in 'Input System' started by uwdlg, Nov 21, 2021.

  1. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    154
    Hi,

    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".

    Is there another way to work around this issue?
     
    Last edited: Nov 29, 2021
  2. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    154
    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!
     
  3. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    812
  4. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    154
    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:
    1. 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.
    2. 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:
    Code (JavaScript):
    1. document.addEventListener('contextmenu', event => event.preventDefault());
    I'll have to do some more thorough testing when time permits it.
    That still isn't a real solution of course...
     
    MousePods likes this.
  5. dan_soqqle

    dan_soqqle

    Joined:
    Feb 2, 2022
    Posts:
    115
    hi was there a solution ?