altmp / altv-issues

Issues and roadmap for alt:V project
93 stars 17 forks source link

Add event for controller keys #1127

Closed ppaglier closed 2 years ago

ppaglier commented 2 years ago

Suggestion

Add event like keydown or keyup for the controller keys or add in keydown & keyup event the controler keys

Use cases

When you need want to create a key bindings system that can use keyboard & controler

Describe the solution you'd like

Add an event called controlerkeydown & controlerkeyup or add this into keydown & keyup event

Describe alternatives you've considered

Actually, if you want to get an "keydown" or "keyup" for controllers, you need to call a native for checking if somes controls is pressed to get wich controller keys is down/up Like this shitty code :

const controllerKeys = [
    {
        id: Keys.padUp,
        pressed: false,
        controls: [
            Controls.sniperZoomInSecondary, // ]
            Controls.phoneUp, // Arrow up
            Controls.scriptPadUp, // Z
            Controls.replayScreenshot, // U
        ],
    },
];

alt.everyTick(ontick);

function onTick() {
    controllerKeys.forEach((key) => {
        const wasPressed = key.pressed;
        key.pressed = true;

        for (let i = 0; i < key.controls.length; i++) {
            const control = key.controls[i];

            if (!natives.isControlPressed(0, control) && !natives.isDisabledControlPressed(0, control)) {
                key.pressed = false;
                break;
            }
        }

        if (key.pressed !== wasPressed) {
            if (key.pressed) {
                onKeyDown(key.id);
            } else {
                onKeyUp(key.id);
            }
        }
    });
}

Additional context

For example, when you press the padUp of your controller, you can know with the event keydown/keyup (or controllerkeydown/controlerkeyup) wich key is pressed (x is a number or a string in this case)

const controlerKeys = {
    x: 'padUp',
};
alt.on('keydown', (key) => {
    console.log(controlerKeys[key]);
});
C0kkie commented 2 years ago

Its related to #353

ppaglier commented 2 years ago

Its related to #353

2019 -> 2021 still open and no news..