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

How to solve KeyUp and KeyDown mismatch in WebGL?

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

  1. jkh-sumdog

    jkh-sumdog

    Joined:
    Jul 4, 2017
    Posts:
    7
    Hi,

    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
    3.  
    4. // Question Phase starts
    5. WebGLInput.captureAllKeyboardInput = false;
    6.  
    7. // User releases 'W'
    8. // Input.GetKeyUp('W') not received
    9.  
    10. // Question Phase ends
    11. WebGLInput.captureAllKeyboardInput = true;
    12.  
    13. // User presses 'W'
    14. // Input.GetKeyDown('W') not received
    15.  
    16. // User releases 'W'
    17. // Input.GetKeyUp('W')  received
    18.  
    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

    wdear

    Joined:
    Mar 28, 2017
    Posts:
    1
    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;
    }