Input Event System

As mentioned in the previous document, EventTarget supports a complete set of event listening and emitting mechanisms. In Cocos Creator v3.4.0, input object is supported, which implements the event registering interface of EventTarget, and can register global system input events through this object. The original systemEvent object has been deprecated since v3.4.0 and will be gradually removed in the future, we recommend using the input object as a replacement.

The differences between systemEvent and input are as follows:

  • Differences in type definitions

    • The declaration of touch event callback in systemEvent is (touch: Touch, event: EventTouch) => void

    • The declaration of touch event callback in input is (event: EventTouch) => void

  • Differences in priority

    • The event listener of systemEvent will be intercepted by the event listener of the node.

    • input objects have higher priority than nodes and will not be intercepted.

      Note: we lowered the priority of input in v3.4.1, so there is no difference in priority between the two objects since v3.4.1.


In this section, the handling of global input events in Cocos Creator will be introduced.

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

  • Mouse
  • Touch
  • Keyboard
  • DeviceMotion

How to define the input events

Use input.on(type, callback, target) to register global input event listeners. Event types included:

Input Eventtype
Mouse EventInput.EventType.MOUSE_DOWN
Input.EventType.MOUSE_MOVE
Input.EventType.MOUSE_UP
Input.EventType.MOUSE_WHEEL
Touch EventInput.EventType.TOUCH_START
Input.EventType.TOUCH_MOVE
Input.EventType.TOUCH_END
Input.EventType.TOUCH_CANCEL
Keyboard EventInput.EventType.KEY_DOWN (keyboard pressed)
Input.EventType.KEY_PRESSING (keyboard continuously pressed)
Input.EventType.KEY_UP (keyboard release)
DeviceMotion EventInput.EventType.DEVICEMOTION

Pointer Events

Pointer events include mouse and touch events.

  • Event listener types

    • Mouse event listening

      • Input.EventType.MOUSE_DOWN

      • Input.EventType.MOUSE_MOVE

      • Input.EventType.MOUSE_UP

      • Input.EventType.MOUSE_WHEEL

    • Touch event listening

      • Input.EventType.TOUCH_START

      • Input.EventType.TOUCH_MOVE

      • Input.EventType.TOUCH_CANCEL

      • Input.EventType.TOUCH_END

  • Callback function after event triggering

    • Custom callback function: callback(event);
  • Callback parameter

Examples of the use of pointer events are as follows:

  1. import { _decorator, Component, input, Input, EventTouch } from 'cc';
  2. const { ccclass } = _decorator;
  3. @ccclass("Example")
  4. export class Example extends Component {
  5. onLoad () {
  6. input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
  7. }
  8. onDestroy () {
  9. input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
  10. }
  11. onTouchStart(event: EventTouch) {
  12. console.log(event.getLocation()); // location on screen space
  13. console.log(event.getUILocation()); // location on UI space
  14. }
  15. }

Keyboard events

  • Event listener types

    • Input.EventType.KEY_DOWN

    • Input.EventType.KEY_PRESSING

    • Input.EventType.KEY_UP

  • Callback function after event triggering

    • Custom Function: callback(event);
  • Callback parameter

Examples of the use of keyboard events are as follows:

  1. import { _decorator, Component, input, Input, EventKeyboard, KeyCode } from 'cc';
  2. const { ccclass } = _decorator;
  3. @ccclass("Example")
  4. export class Example extends Component {
  5. onLoad () {
  6. input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  7. input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
  8. }
  9. onDestroy () {
  10. input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  11. input.off(Input.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

  • Event listener type

    • Input.EventType.DEVICEMOTION
  • Callback function after event triggering

    • Custom callback function: callback(event);
  • Callback parameter

Examples of the use of DeviceMotion events are as follows:

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

The specific usage can be found in the example event, which contains the implementation of keyboard, accelerometer, single-touch, multi-touch and other functions.

Touch detection for 3D objects

The touch detection for 3D objects and 2D UI nodes is different:

  • 2D UI nodes only need the size information provided by the UITransform component and the position information of the node to do the touch detection. For details, please refer to Node Event System.

  • The touch detection for 3D objects needs to be implemented by ray cast. The specific method is to generate a ray from the rendering camera of the 3D object to the screen coordinates of the touch point to determine whether the ray hits the object that was detected. The specific code implementation is as follows:

    1. import { _decorator, Component, Node, Camera, geometry, input, Input, EventTouch, PhysicsSystem } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass("Example")
    4. export class Example extends Component {
    5. // Specify the camera rendering the target node.
    6. @property(Camera)
    7. readonly cameraCom!: Camera;
    8. @property(Node)
    9. public targetNode!: Node
    10. private _ray: geometry.Ray = new geometry.Ray();
    11. onEnable () {
    12. input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
    13. }
    14. onDisable () {
    15. input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
    16. }
    17. onTouchStart(event: EventTouch) {
    18. const touch = event.touch!;
    19. this.cameraCom.screenPointToRay(touch.getLocationX(), touch.getLocationY(), this._ray);
    20. if (PhysicsSystem.instance.raycast(this._ray)) {
    21. const raycastResults = PhysicsSystem.instance.raycastResults;
    22. for (let i = 0; i < raycastResults.length; i++) {
    23. const item = raycastResults[i];
    24. if (item.collider.node == this.targetNode) {
    25. console.log('raycast hit the target node !');
    26. break;
    27. }
    28. }
    29. } else {
    30. console.log('raycast does not hit the target node !');
    31. }
    32. }
    33. }