Global System Events

In this section, the global system events of Cocos Creator will be introduced.

Global system events are irrelevant with the node hierarchy, so they are dispatched globally by systemEvent, currently supported:

  • Mouse
  • Touch
  • Keyboard
  • DeviceMotion

The global mouse and touch events are very similar to the node events, except that the area of action is different. The following is a description of these events.

The difference between node events and global mouse and touch events

Note: before beginning this section, read up on Auto fit for multi-resolution and understand the screen area and UI display area.

When listening for global mouse/touch events, the acquired contacts are calculated based on the bottom left corner of the screen area (device display resolution). The contacts fetched by the UI node listener are not the same as the contacts fetched by the global event, which are converted to the points calculated in the lower left corner of the adapted UI viewable area. Global touch points are better suited for manipulating the behavior of 3D nodes by tapping directly on the screen, without having to add UI nodes to the scene to listen for mouse/touch events.

How to define the input events

Use systemEvent.on(type, callback, target) to register Keyboard and DeviceMotion event listeners.

Event types included:

  1. SystemEventType.KEY_DOWN
  2. SystemEventType.KEY_UP
  3. SystemEventType.DEVICEMOTION

Keyboard events

  • Type: SystemEventType.KEY_DOWN and SystemEventType.KEY_UP
  • Call Back:
    • Custom Event: callback(event);
  • Call Back Parameter:
  1. import { _decorator, Component, Node, systemEvent, SystemEventType, EventKeyboard, macro } from 'cc';
  2. const { ccclass } = _decorator;
  3. @ccclass("Example")
  4. export class Example extends Component {
  5. onLoad () {
  6. systemEvent.on(SystemEventType.KEY_DOWN, this.onKeyDown, this);
  7. systemEvent.on(SystemEventType.KEY_UP, this.onKeyUp, this);
  8. }
  9. onDestroy () {
  10. systemEvent.off(SystemEventType.KEY_DOWN, this.onKeyDown, this);
  11. systemEvent.off(SystemEventType.KEY_UP, this.onKeyUp, this);
  12. }
  13. onKeyDown (event: EventKeyboard) {
  14. switch(event.keyCode) {
  15. case macro.KEY.a:
  16. console.log('Press a key');
  17. break;
  18. }
  19. }
  20. onKeyUp (event: EventKeyboard) {
  21. switch(event.keyCode) {
  22. case macro.KEY.a:
  23. console.log('Release a key');
  24. break;
  25. }
  26. }
  27. }

Device motion

  • Type: SystemEventType.DEVICEMOTION
  • Call back:
    • Custom event: callback(event);;
  • Call back parameter:
  1. import { _decorator, Component, Node, systemEvent, SystemEventType, log } from 'cc';
  2. const { ccclass } = _decorator;
  3. @ccclass("Example")
  4. export class Example extends Component {
  5. onLoad () {
  6. systemEvent.setAccelerometerEnabled(true);
  7. systemEvent.on(SystemEventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
  8. }
  9. onDestroy () {
  10. systemEvent.off(SystemEventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
  11. }
  12. onDeviceMotionEvent (event: EventAcceleration) {
  13. log(event.acc.x + " " + event.acc.y);
  14. }
  15. }

Please review the test-cases-3d (This includes the keyboard, accelerometer, single point touch, multi-touch examples).