Input Event System

    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.


    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

    Use to register global input event listeners. Event types included:

    Pointer events include mouse and touch events.

    • Event listener types

        • Input.EventType.MOUSE_DOWN

        • 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:

    • Event listener types

      • 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:

    • 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:

    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:

    • 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: