Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Disable/Enable keyboard in runtime

Discussion in 'WebGL' started by alechewitt, Jun 26, 2015.

  1. alechewitt

    alechewitt

    Joined:
    Jun 26, 2015
    Posts:
    1
    I'm running into this issue here: http://forum.unity3d.com/threads/disable-enable-keyboard-in-runtime-webgl.286557/

    The Unity WebGL build is swallowing all the input of the keyboard. The solution in the above thread now appears not to work.

    I have grepped through all the files outputted by the unity build and there is no "keyboardListeningElement" or "doNotCaptureKeyboard" in there.

    I can see this was an option added to Emscrippten some time ago (https://github.com/kripken/emscripten/commit/0a4b5c011e19b13aa4193b7c86164219b9593e74) but it is not in the files exported by unity.
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    This is due to a change we made in 5.1 regarding input handling. To fix it, there will be a new API that will allow you to change this behavior, so that Unity WebGL does not capture all keyboard inputs. This will be available in 5.2, however, since this is an issue, we will look at making it available in 5.1 as well.
     
  3. aiab_animech

    aiab_animech

    Joined:
    Jul 4, 2012
    Posts:
    177
    doNotCaptureKeyboard is critical to all our WebGL projects. We can't update to 5.1 until this is resolved, which is unfortunate. Please include something equivalent in a patch release.
     
  4. Murrrly

    Murrrly

    Joined:
    Apr 25, 2013
    Posts:
    16
  5. abryden

    abryden

    Joined:
    Sep 29, 2014
    Posts:
    24
    This is extremely important for us also. Sadly 5.1 solved other critical bugs so we can't go back.
     
  6. wastvedt

    wastvedt

    Joined:
    Oct 6, 2012
    Posts:
    25
  7. dark_end

    dark_end

    Joined:
    Jun 24, 2015
    Posts:
    5
    So, are you telling us you dropped the input handlers completely and without notice WITHOUT providing an alternative?

    The workaround works, but its still a mess to have to do this. Please notify us promptly when the new API is available
     
  8. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    The new API is available in the latest patch. You need to look for WebGLInput.captureAllKeyboardInput. It's a property that you can change at runtime (via script) to control whether all keyboard inputs should only be consumed by the WebGL canvas or not.
     
  9. codingChris

    codingChris

    Joined:
    Apr 8, 2013
    Posts:
    7
    Just downloaded the patch, but it is not there. All I get is The name `WebGLInput' does not exist in the current context
    The docs say, that it should be right inside the UnityEngine namespace, but when I inspect the dll with monos Assembly-Browser it can not be found.

    PS: I'm running the editor on a mac.
     
    Last edited: Jul 22, 2015
  10. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Are you using the proper #define's ? For example:

    #if !UNITY_EDITOR && UNITY_WEBGL
    WebGLInput.captureAllKeyboardInput = false;
    #endif
     
  11. codingChris

    codingChris

    Joined:
    Apr 8, 2013
    Posts:
    7
    I was using #if UNITY_WEBGL without !UNITY_EDITOR. Using both of them works.

    Thanks, for that.
     
    Marco-Trivellato likes this.
  12. rameshkumar

    rameshkumar

    Joined:
    Aug 5, 2013
    Posts:
    50
    @ MacroTrivellato there is no keyword called "WebGLInput" in unity 5.1 , should i need to use any namespace to get this..
     
  13. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    No, just UnityEngine.

    It's in 5.1.2 Patch 1. Which version of Unity are you using ?
     
  14. rameshkumar

    rameshkumar

    Joined:
    Aug 5, 2013
    Posts:
    50
    @Macro Trivellato , thank u dude and i have one major issue i am working on Web GL game and thats is taking some time to load so what is requirement to run the WebGL game , any graphics card or ram size should be upgarded.please help me ..
     
  15. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    344
    Thanks! This worked for us.

    Here is what we did,

    Add this to Unity to stop taking all keyboard entries:

    Code (CSharp):
    1.   #if !UNITY_EDITOR && UNITY_WEBGL
    2.   WebGLInput.captureAllKeyboardInput = false;
    3.   #endif
    Script page.

    Only now, you will find that Unity receives no keyboard input at all. So, add this to your canvas in HTML for the Unity object:

    Code (CSharp):
    1. tabindex="1"
    Where 1 could be whatever number you want to use for the tabindex.
     
    modelme3d likes this.
  16. wurzel-digital

    wurzel-digital

    Joined:
    Jun 21, 2013
    Posts:
    9
    Setting
    WebGLInput.captureAllKeyboardInput = false;
    seem to have helped a lot of developers. It didn't help us. We have a <div> dynamically filled by Javascript placed on top of the Unity #canvas and Unity still gobbles up all keyboard events.
     
  17. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
    @wurzelmedien: I appreciate you posting in the forums about this, but raising the issue on top of a 5 year old forum post is quite counterproductive, especially since you already posted about this issue in another forum thread https://forum.unity.com/threads/how-to-get-rid-of-unity-canvas-collecting-all-keyboard-input.431060/ , which also was several years old.

    Simply stating that "it does not work for me" in several threads will not get much help, since there are no leads to go on. Can you instead open a new forum thread, with a zip of a small test project of your WebGL template where you are seeing the issue, along with the relevant details like the Unity version you are using, and browser and OS.
     
    shacharoz likes this.