Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WebGL Input.GetKey() problem: keyboard keys can get stuck

Discussion in 'Web' started by bererton, Nov 13, 2018.

  1. bererton

    bererton

    Joined:
    Jun 20, 2014
    Posts:
    34
    Hello WebGL experts,

    We've got a Unity game with a WebGL export. Currently running Unity 2018.2.12f1 with the WebGL WebAssembly export. Unfortunately we've run into a semi-rare issue in that when we call Input.GetKey() sometimes it looks like the key is held down, when in fact it is up on the physical keyboard.

    Looking at the javascript for this, it seems that because the raw javascript only gives you keydown and keyup events that various javascript libraries have attempted to work around this by detecting/overriding keys that have been down for too long etc. However, it seems to us that Unity's webGL export doesn't do some of the fanciness that these libraries do.

    In particular, you'll be playing the game and using WASD or arrow keys and running around. Without changing focus sometimes a key you were pressing gets stuck in the down state.

    Has anyone else run into this? Do you have a workaround? I've seen other Unity WebGL exports (zombsroyale.io) also have this problem with the keyboard getting stuck and it can be frustrating in a competitive game. At this point I'm looking at calling out to a jslib in our WebGL build that listens to the document level keydown and keyup events, but also catches a few other events to ensure that they javascript keyboard state stays as good as possible. Is there a better solution?

    Any ideas/help would be greatly appreciated.

    Thanks!
     
    Harpaceas and decapator like this.
  2. decapator

    decapator

    Joined:
    Sep 2, 2013
    Posts:
    10
    Wow im having the same problem.

    Did you ever end up finding a solution?

    Each browser ive tested in, has different behavior.
    And the non-buffered input is only present in ARM-64bit build of chromium on the Jetson Nano.

    How odd.
     
  3. cbert

    cbert

    Joined:
    Jan 24, 2014
    Posts:
    1
    ultimately I never did find a great workaround. Seems like the actual javascript was missing the events as well. So I did end up using ram javascript... but I ended up making it so that on the keydown of the right arrow, I manually did a keyup on the left arrow.... that worked a little bit better. Still not great to be honest.
     
  4. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    589