Hi.
When I hold down the event.gamepad.dpad.left button, the event is sent only once.
Is this expected behavior or should it be sent constantly?
How to catch the event of a pressed button?
-I don’t have a real gamepad, I’m testing from the TV remote control.
if(event.gamepad.dpad.left){left();}
zzy
March 24, 2024, 11:16am
#2
Perhaps you should read the source code to get more info. I can’t find dounment or api of gamepad.
/*
Copyright (c) 2022 Chukong Technologies Inc.
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
This file has been truncated. show original
declare module 'pal/input' {
type InputSourceButton = import('pal/input/input-source').InputSourceButton;
type InputSourceStick = import('pal/input/input-source').InputSourceStick;
type InputSourceDpad = import('pal/input/input-source').InputSourceDpad;
type InputSourcePosition = import('pal/input/input-source').InputSourcePosition;
type InputSourceOrientation = import('pal/input/input-source').InputSourceOrientation;
type TouchCallback = (res: import('cocos/input/types').EventTouch) => void;
/**
* Class designed for touch input.
*/
export class TouchInputSource {
/**
* Register the touch event callback.
*/
public on (eventType: import('cocos/input/types/event-enum').InputEventType, callback: TouchCallback, target?: any);
}
type MouseCallback = (res: import('cocos/input/types').EventMouse) => void;
/**
This file has been truncated. show original
And I think this is the right way to use gamepad event.
import { __private, _decorator, Component, EventGamepad, Input, input, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('GamePadTest')
export class GamePadTest extends Component {
start() {
input.on(Input.EventType.GAMEPAD_INPUT, this.onGamePadInput, this)
input.on(Input.EventType.GAMEPAD_CHANGE, this.onGamePadChange, this)
}
private device0: __private._pal_input__GamepadInputDevice;
update(deltaTime: number) {
if(this.device0 && this.device0.connected){
if(this.device0.dpad.left.getValue()){
this.left();
}
}
}
public onGamePadInput(e: EventGamepad){
console.log("input");
}
public onGamePadChange(e: EventGamepad){
console.log("gamepad change id:" + e.gamepad.deviceId);
this.device0 = e.gamepad;
}
public left(){
console.log("pressing left");
}
}
I was just able to return to the project. I’ll test this method now. Thank you. As I understand it, you suggest using GAMEPAD_CHANGE
and saving it to a non-local variable.
I used GAMEPAD_INPUT
in my project.
I’ll be back later with the test result.