Disable/Enable keyboard in runtime [WebGL]

Sometimes on webgl build I show text field to get some input (using html form). But when I try to input some text, it looks like Unity swallows all input. Is it possible to disable/enable it in runtime?

Probably. Could you send a little project demonstrating this in a bug report? Then i will look at it, and see what is the best way to set this up.

Great. Already sent report #658342.

Hi,

Any news on this matter?

As I also experience this, with input fields in html and WebGL.

Nope, nothing new about this.

So, I just looked into the example provided in #658342.

  1. By default, emscripten will capture all keyboard input on the page. This makes sense, as it means that the common simple case of having a game on a page will just work without having to deal with keyboard focus and the like. However, once you want other html elements on the page to deal with input, you need to figure out some better solution. Luckily emscripten provides some options:

  2. In the simple case that you don’t actually need keyboard input for your webgl content, you can disable it, by adding the line:

doNotCaptureKeyboard: true,

to the setup of “Module” in the generated html, for instance just after

  var Module = {
  1. If you need input in both other html and the webgl content, you have to do some more work. Unfortunately, most browsers will not allow you to have a canvas itself receive text input. So, you need to have a html element on the page which will deal with text input for you (like a div element with contentEditable=“true”):
<div d="myTextInputReceiver" contentEditable="true">

Then you tell emscripten to use that for text input (instead of the whole page), by adding a line like this to the Module setup (same place as above):

keyboardListeningElement: document.getElementById('myTextInputReceiver'),

Now, this element will be used for text input. You need to handle focus yourself, by calling document.getElementById(‘myTextInputReceiver’).focus() when your content should receive text focus.

4 Likes

Thank you very much for your help. The third option looks good enough :smile:

Thank you so much for the help.

1 Like