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. SystemEvent.EventType.KEY_DOWN
  2. SystemEvent.EventType.KEY_UP
  3. SystemEvent.EventType.DEVICEMOTION

Keyboard events

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

Device motion

  • Type: SystemEvent.EventType.DEVICEMOTION
  • Call back:
    • Custom event: callback(event);;
  • Call back parameter:
  1. import { _decorator, Component, Node, systemEvent, SystemEvent, 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(SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
  8. }
  9. onDestroy () {
  10. systemEvent.off(SystemEvent.EventType.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).