Disable/Enable keyboard in runtime

I’m running into this issue here: Disable/Enable keyboard in runtime [WebGL] - Unity Engine - Unity Discussions

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 (allow customizing the DOM element we listen for key events on using M… · emscripten-core/emscripten@0a4b5c0 · GitHub) but it is not in the files exported by unity.

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.

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.

+1

This is extremely important for us also. Sadly 5.1 solved other critical bugs so we can’t go back.

This solution worked for me, in the meantime:

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

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.

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.

Are you using the proper #define’s ? For example:

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

I was using #if UNITY_WEBGL without !UNITY_EDITOR. Using both of them works.

Thanks, for that.

1 Like

@ MacroTrivellato there is no keyword called “WebGLInput” in unity 5.1 , should i need to use any namespace to get this…

No, just UnityEngine.

It’s in 5.1.2 Patch 1. Which version of Unity are you using ?

@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 …

Thanks! This worked for us.

Here is what we did,

Add this to Unity to stop taking all keyboard entries:

  #if !UNITY_EDITOR && UNITY_WEBGL
  WebGLInput.captureAllKeyboardInput = false;
  #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:

tabindex="1"

Where 1 could be whatever number you want to use for the tabindex.

1 Like

Setting
WebGLInput.captureAllKeyboardInput = false;
seem to have helped a lot of developers. It didn’t help us. We have a

dynamically filled by Javascript placed on top of the Unity #canvas and Unity still gobbles up all keyboard events.

@wurzel-digital : 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 How to get rid of Unity canvas collecting all keyboard input , 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.

1 Like