Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    6
    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;
    }