Module gamejs/event

Deal with mouse and keyboard events.

You can either handle all events in one callback with gamejs.event.onEvent():

gamejs.onEvent(function(event) {
   if (event.type === gamejs.event.MOUSE_UP) {
     gamejs.logging.info(event.pos, event.button);
   } else if (event.type === gamejs.event.KEY_UP) {
     gamejs.logging.info(event.key);
   }
});

Or recieve more specific callbacks, e.g. only for KEY\_UP with gamejs.event.onKeyUp():

gamejs.onKeyUp(function(event) {
     gamejs.logging.info(event.key);
});

All events passed to your callback are instances of gamejs.event.Event and have a type property to help you distinguish between the different events. This type property is set to one of those constants:

  • gamejs.event.MOUSE_UP
  • gamejs.event.MOUSE_MOTION
  • gamejs.event.MOUSE_DOWN
  • gamejs.event.KEY_UP
  • gamejs.event.KEY_DOWN
  • gamejs.event.DISPLAY_FULLSCREEN_ENABLED
  • gamejs.event.DISPLAY_FULLSCREEN_DISABLED
  • gamejs.event.QUIT
  • gamejs.event.MOUSE_WHEEL
  • gamejs.event.TOUCH_DOWN
  • gamejs.event.TOUCH_UP
  • gamejs.event.TOUCH_MOTION

Keyboard constants

There are also a lot of keyboard constants for ASCII. Those are all prefixed with K\_, e.g. gamejs.event.K\_a would be the "a" key and gamejs.event.K_SPACE is the spacebar.

Touch events

Touch events do not have a single position but for all TOUCH\_* events you get an array of touches, which each have their own pos attribute and a unique identifier for tracking this touch across multiple TOUCH\_MOTION events.

User defined events

All user defined events can have the value of gamejs.event.USEREVENT or higher. Make sure your custom event ids follow this system.

Example

gamejs.onEvent(function(event) {
       if (event.type === gamejs.event.MOUSE_UP) {
         gamejs.logging.log(event.pos, event.button);
       } else if (event.type === gamejs.event.KEY_UP) {
         gamejs.logging.log(event.key);
       }
    });




Event ()

Properties of the event object argument passed to the callbacks.


Event.prototype.button

the number of the mousebutton pressed


Event.prototype.key

key the keyCode of the key. compare with gamejs.event.Ka, gamejs.event.Kb,...


Event.prototype.pos

pos the position of the event for mouse events


Event.prototype.rel

relative movement for a mousemove event


Event.prototype.type

The type of the event. e.g., gamejs.event.QUIT, KEYDOWN, MOUSEUP.




K_0


K_1


K_2


K_3


K_4


K_5


K_6


K_7


K_8


K_9























K_a


K_b


K_c


K_d


K_e


K_f


K_g


K_h


K_i


K_j


K_k


K_l


K_m


K_n


K_o


K_p


K_q


K_r


K_s


K_t


K_u


K_v


K_w


K_x


K_y


K_z













onDisplayResize (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onEvent (callback, scope)

The function passsed to onEvent will be called whenever any event (mouse, keyboard, etc) was triggered.

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onFullscreen (callback, scope)

Pass a callback function to be called when Fullscreen is enabled or disabled. Inspect event.type to distinguis between entering and exiting fullscreen.

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onKeyDown (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onKeyUp (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onMouseDown (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onMouseMotion (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onMouseUp (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onTouchDown (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onTouchMotion (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)

onTouchUp (callback, scope)

Parameters

Function callback to be called
Object scope within which the callback should be called. It's `this` during invocation. (optional)