Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Disable/Enable keyboard in runtime [WebGL]

Discussion in 'Unity 5 Pre-order Beta' started by d12frosted, Dec 19, 2014.

  1. d12frosted

    d12frosted

    Joined:
    Feb 3, 2014
    Posts:
    43
    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?
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    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.
     
  3. d12frosted

    d12frosted

    Joined:
    Feb 3, 2014
    Posts:
    43
    Great. Already sent report #658342.
     
  4. Jungnissen

    Jungnissen

    Joined:
    Sep 4, 2014
    Posts:
    5
    Hi,

    Any news on this matter?

    As I also experience this, with input fields in html and WebGL.
     
    Last edited: Jan 4, 2015
  5. d12frosted

    d12frosted

    Joined:
    Feb 3, 2014
    Posts:
    43
    Nope, nothing new about this.
     
  6. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    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:
    Code (csharp):
    1.  
    2. doNotCaptureKeyboard: true,
    3.  
    to the setup of "Module" in the generated html, for instance just after
    Code (csharp):
    1.  
    2.   var Module = {
    3.  
    3. 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"):

    Code (csharp):
    1.  
    2. <div d="myTextInputReceiver" contentEditable="true">
    3.  
    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):

    Code (csharp):
    1.  
    2. keyboardListeningElement: document.getElementById('myTextInputReceiver'),
    3.  
    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.
     
  7. d12frosted

    d12frosted

    Joined:
    Feb 3, 2014
    Posts:
    43
    Thank you very much for your help. The third option looks good enough :D
     
  8. Jungnissen

    Jungnissen

    Joined:
    Sep 4, 2014
    Posts:
    5
    Thank you so much for the help.
     
    atiqanazir15 likes this.