Input field in webpage not working with Unity WebGL in page

Hello, I have a page that has an html input field with type=text in the page, beside my Unity WebGL application.
The WebGL application somehow causes the input fields to not work. I’m guessing it eats input somehow. How can I disable this?

Is the WebGL app capturing the keyboard input you think?

Yes, that’s what I’m thinking. In the same page without the WebGL canvas the input field works just fine.
Also, I can place the marker in the input field (to make it have focus), and I can even paste into it with ctrl-v. But I can’t type and I can’t use backspace.

Check this forum thread for discussion about the same problem, and a possible solution:

1 Like

Thank you very much! I will try that!

The solutions you provided worked great for me. (I disabled keyboard input completly)

We have a similar issue with HTML sliders.

Slider works until the emscripten blob is loaded, at which point it becomes unresponsive.

Submitted as bug (case 667770) in February, but as yet unresolved.

I’m not 100% sure this is really a bug… it’s necessary for the game to capture keyboard and mouse input otherwise how would you play it? I could take a look at the javascript generated by Unity and see if I can come up with a workaround… I think the easiest would be that when the game loses focus it stops capturing input, and starts capturing when it gets focus again.

1 Like

Dustin: not every Unity app is a game. Our app includes heavy interaction between Unity and the web page around it. We can easily focus the Unity ‘pane’ by having the user click on it. However, the Unity WebGL build hijacks interaction outside of the Unity pane. The above solution solves the keyboard issue, but some strange focus behaviour remains with mouse interaction, in particular with sliders.

I did a bit of work yesterday migrating away from regular webkit sliders to jQuery sliders, and results are promising.

1 Like

I’m aware they are not all games.

1 Like

Hi,
I imagine this issue is the reason why my Unity WebGl games don’t work with the keyboard within Newgrounds and Kongregate pages.

Is there anything I can do on my side or to those portals need to do something to their own pages?

thanks.

I had the same issue as everybody, I see that this solution helped, but for me it isnt working. Nor the 2. option, to forbid webgl to listen to keayboard via doNotCaptureKeyboard: true, neither the 3. option, to add keyboardlisteningelement to Module. The 3rd option would have been the best for me. I set the focus properly but still WebGL eats all of my inputs …

Thx for any suggestion on how to make this work.

“doNotCaptureKeyboard: true” no longer works in Unity 5.1, as we rewrote input handling in WebGL. But, 5.1.2p1 (which was shipped yesterday) now has an API to switch this setting from Unity scripting code:

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

And what about that listening element ? It would be better solution for me because I only want to stop webGL from listening when input textfield is focused. But It doesnt seem to work properly either.

Input Field is broken in 5.1.2p1, demo: http://dragonfly.com.ua/webGlTesting/
I was using:

    void Awake()
    {
#if !UNITY_EDITOR && UNITY_WEBGL
    WebGLInput.captureAllKeyboardInput = true;
#endif
    }

and (on early versions it worked):

   void LateUpdate()
    {
        if(Input.GetMouseButtonDown(0))
            Application.ExternalEval("window.focus();");
    }

This is a different bug. it’a regression that we are fixing in both 5.2 and 5.1

I will get the fix back-ported to the next 5.1 patch, which should be out in the next few days.

Thanks for the heads up!

1 Like
WebGLInput.captureAllKeyboardInput=true;

Unity doesn’t know WebGLInput. How to include it?

It is a WebGL-only API, so you need to put it inside a conditional like this:

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

Yes, I use like this. I had this problem because I used 5.1.2f1. I updated Unity for 5.1.3f1

So, I added this code in Start() in my simple snake game. Can it work in iFrame? Snake_v1.0.1 (source)

I upload my application on social network vk.com. But keyboard input doesn’t work. Is it a problem with vk.com or with Unity?

You can run it.

Keyboard input will start to work if you will press W and will click on scene when the game are starting.

the new API mentioned here, will allow you to control whether all keyboard inputs will be captured by the webgl canvas, or they will also be sent to html input fields. This does not seem to be your problem, isn’t it?