game-developmentcocoscreator

How to get mouse button with node.on(Node.EventType.TOUCH_START) in CC 3.8?


I am developing a game in Cocos Creator 3.8.2 in which the user clicks their mouse and a different command is run for clicking different mouse buttons. I've searched far and wide across the Internet and while I have found how to get which mouse button is pressed by using AddEventListener(), I cannot figure out how to get which mouse button is pressed with node.on(Node.EventType.TOUCH_START, (event) =>{}).

I tried running console.log(event.getButton) and console.log(event.button), but all they return is undefined. At this point I am considering just using AddEventListener(). Does anyone know how to get which mouse button is pressed when a TOUCH_START event happens in Cocos Creator 3.8?


Solution

  • From the documentation, There's MOUSE_DOWN for the mouse click.

    And the code would be something like this.

    node.on(Node.EventType.MOUSE_DOWN, (event) => {
        if (event.getButton() === EventMouse.BUTTON_LEFT) {
            // Left mouse button clicked
            console.log("Left mouse button clicked");
        } else if (event.getButton() === EventMouse.BUTTON_RIGHT) {
            // Right mouse button clicked
            console.log("Right mouse button clicked");
        } else if (event.getButton() === EventMouse.BUTTON_MIDDLE) {
            // Middle mouse button clicked
            console.log("Middle mouse button clicked");
        }
    });