Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

How to solve KeyUp and KeyDown mismatch in WebGL?

Discussion in 'Scripting' started by jkh-sumdog, Jul 13, 2017.

  1. jkh-sumdog


    Jul 4, 2017

    I've got a problem with Input.GetKeyDown and Input.GetKeyUp in WebGL

    My game has two phases, a question phase and play phase. A game goes Question Phase -> Play Phase -> Question Phase -> Play Phase and so on.

    In the question phase the user answers question in an embedded html page. Because of this, in the question phase we set - WebGLInput.captureAllKeyboardInput to false. And when the question phase enters we enable it so that the user can move around in their play phase.

    The problem we have is when a user is holding down a key when their play phase ends and they go into question phase. They then release the input. Then they go back into the player, and press the same key down again. We don't receive the key down event

    Here is a simplified time line:

    Code (CSharp):
    1. // User presses 'W'
    2. // Input.GetKeyDown('W') received
    4. // Question Phase starts
    5. WebGLInput.captureAllKeyboardInput = false;
    7. // User releases 'W'
    8. // Input.GetKeyUp('W') not received
    10. // Question Phase ends
    11. WebGLInput.captureAllKeyboardInput = true;
    13. // User presses 'W'
    14. // Input.GetKeyDown('W') not received
    16. // User releases 'W'
    17. // Input.GetKeyUp('W')  received
    19. // User presses 'W'
    20. // Input.GetKeyDown('W')  received
    Looking at the documention for GetKeyDown it says:
    " It will not return true until the user has released the key and pressed it again."

    So our problem is that unity doesn't think the user has released the key. Because it happens while Unity is ignoring key events. So we don't get the new KeyDown event.

    Is there a way that I can trigger an Input.GetKeyUp event from the code?
  2. wdear


    Mar 28, 2017
    you can change the condition when question phase can start by add a input check:
    if( #your conditions when question phase can start# && !Input.anyKey)
    WebGLInput.captureAllKeyboardInput = false;