Recording exact keys pressed in WebGL

I’m making a typing speed test game in WebGL for a research project that can:

  • Record the exact keys pressed and when (e.g. Shift + d instead of just D). This means Input.inputString is unhelpful.
  • Record when the keys are released.

I have read here that Event.Current in OnGUI() has poor performance, so I have implemented their suggestion of Input.GetKeyDown and a Foreach loop in Update(), following all of their steps.

This is working great in the Editor (both 1 and 2 work), but when I do a development / normal build as WebGL, nothing seems to happen when I press the keys. When I Debug.Log in OnKeyDown(), I can’t find the log in the javascript console.

Could anyone comment on whether the linked answer above should work in WebGL? I haven’t used the interface stuff before.

It seems the original answerer has been inactive since 2018, so I would appreciate any suggestions on how to fulfil my 2 goals in WebGL. Especially ones that are relatively quick to implement as I’m on a time crunch.

WebGL is kind of a special beast subject to all the security concerns around a browser, such as keeping random Javascript on websites from reading your keyboard and stealing your passwords. One or more of these things might be in play.

Either way, first thing to test is a simple raw app that displays every key down/up and run that on WebGL to make sure it works, then reason about if your actual app has a problem or not.

Hi Kurt, you make a good point, thanks.

I have a working app (www.turbo-typing.com) that used the if(Input.anyKeyDown) and keysPressed = Input.inputString method, but perhaps Input.GetKeyDown and the associated commands are blocked for security reasons. I’ll try the stripped down version you suggest.