How to set up and catch keyboard events? How about controllers?
BasiliosZ

I noticed that keyboard events are mentioned in the What's New section for Wade 0.8. How can I set up event listeners for keyboard presses?

I think it should be the addEventListener() method of the wade object, but if it is that it takes an event name. What to use?

I also wonder whether there is a way to manage generic controller events (the user inputting right, left, a fire button, a custom command, and so on) that can abstract away keyboard interfaces on computers, controllers on consoles and keypad or touchscreen directional controls (similar to the ones provided in Quintus, the JS game development library). If not available, is there any plans about them for the future?

All 5 Comments
Gio
Keyboard event names are:
  • onKeyPress
  • onKeyDown
  • onKeyUp
To catch these, just add functions to your App object like this:
this.onKeyDown = function(eventData){    var key = eventData.keyCode;    var shift = eventData.shift;    var ctrl = eventData.ctrl;    var alt = eventData.alt;    var meta = eventData.meta;}


Unlike mouse (or touch) events, it doesn't make sense to listen for a keyboard event on a specific scene object, so these are just "global" app input events, and you don't need to call addEventListener()

Regarding controllers: if you open your app in a console browser (Xbox360 for example), the main controller button (green on Xbox360) is mapped to the onMouseDown event, while the left gamepad stick triggers onMouseMove events. This is true for Smart TV's too.

However, if you have a controller attached to your PC, on a PC browser it won't be recognized. Full controller support is planned for version 1.0 of WADE. Unless people need it sooner, in which case we can try to put it in for 0.9.
BasiliosZ

Ok, thanks - now I understand.

BasiliosZ

I realize only now that I don't really know the key codes. Are they the ASCII codes?

Gio

In most cases yes, they are. But be careful with some "special" keys, such as arrow keys, escape, basckspace, etc. - because for them some browsers will not fire an onKeyPress event, but only an onKeyDown event.

If you need arrow keys, listen for onKeyDown and onKeyUp events. Key codes are:

left: 37
up: 38
right: 39
down: 40

Here you can find a complete list (and a tool to try it on your own browser)

BasiliosZ

Extremely useful web page, thanks!

Post a reply
Add Attachment
Submit Reply
Login to Reply