Catching Input Events in WebGL build

1.8k views Asked by At

When running a Unity project as a WebGL app is there way to catch the site's input events (e.g. keypress) once the application is running?

Currently once the Unity app launches it eats keyboard events so that I can't type in to my text boxes on the page. The best workaround I could come up with is to use jQuery to directly read the keypress event:

 $('#input_message').keypress(function(evt){
   event.preventDefault();

   var code = evt.keyCode || evt.which;
   var key=String.fromCharCode(code);
   $('#input_message').val($('#input_message').val() + key);
 });

};

This works for my immediate needs but I'm hoping there is a cleaner way through the Unity API.

Versions

  • Chrome v43
  • Unity 5.1.1f1
2

There are 2 answers

0
Mateus Pires On

This question has no solution, so if someone comes up here, there is a short path:

By default, Unity WebGL will process all keyboard input send to the page, regardless of whether the WebGL canvas has focus or not. This is done so that a user can start playing a keyboard-based game right away without the need to click on the canvas to focus it first. However, this can cause problems when there are other HTML elements on the page which should receive keyboard input, such as text fields - as Unity will consume the input events before the rest of the page can get them. If you need to have other HTML elements receive keyboard input, you can change this behavior using the WebGLInput.captureAllKeyboardInput property.

This was taken from here.

The property you are looking for is WebGLInput.captureAllKeyboardInput.

0
Purrx On

I would make this a comment if I had enough points but:

You could have your game send the inputs to your site via websockets. I've done similar things with Azure + SignalR