altv-issues icon indicating copy to clipboard operation
altv-issues copied to clipboard

Client keydown and Keyup doesn't triggered

Open FlorianV85 opened this issue 3 years ago • 5 comments

Description of the problem

Keyup and Keydown client events sometimes fail to register for unknown reasons.

https://discord.com/channels/371265202378899476/1032607409304244284/1032607409304244284

Reproduction steps

alt.on('keyup', (key) => { alt.log(key); });

Expected behaviour

The event is supposed to register correctly and trigger after pressing the key, but for unknown reasons, sometimes the event just doesn't register and its content doesn't trigger. As if it were empty.

Additional context

It seems that Stuyk also had the problem without finding the cause.

I tried to test a simple event in a blank resource, and it had the same behavior.

Finally, I simply restarted my computer and the events worked again. However, they stopped working again when the server was restarted.

I also tried to test with alt.isKeyDown() inside an everyTick, and the result is that the key was indeed detected, but it is not detected in the Key events

Operating system

Debian 11 / Windows 10

Version

release 12.11

Reproduction tested

  • [X] I confirm that I have made sure that this issue is also present on the newest dev version

FlorianV85 avatar Oct 20 '22 15:10 FlorianV85

Have you tried a clean restart of the game without restarting the PC?

Doxoh avatar Oct 21 '22 18:10 Doxoh

And please test it with current dev version too

Doxoh avatar Oct 21 '22 18:10 Doxoh

Have you tried a clean restart of the game without restarting the PC?

Yes, same result

And please test it with current dev version too

I already tested with the dev branch and it gives the same result

FlorianV85 avatar Oct 21 '22 18:10 FlorianV85

Can you upload your resource and the server CFG to test it?

I cannot reproduce the problem. I have added these events and reconnect several times, but the key is logged each time.

Doxoh avatar Oct 21 '22 18:10 Doxoh

Can you upload your resource and the server CFG to test it?

I cannot reproduce the problem. I have added these events and reconnect several times, but the key is logged each time.

I also tested on a blank resource with just alt.log() in those events, so it's not really the resources.

alt.on('keyup', (key) => {
    alt.log(key);
});

Apparently, it's happening randomly. For some it's constant (that's my case), for others after a few reconnects. It's really strange.

We had a discussion about this in #scripting-questions if it can provide any information. https://discord.com/channels/371265202378899476/1032607409304244284/1032607409304244284

My server.cfg :

name: 'My Server'
debug : true
language : fr
gamemode : roleplay
modules : [ js-module ]
resources : [ main ]

FlorianV85 avatar Oct 21 '22 19:10 FlorianV85

Could you please test it with the latest development version of module and client?

Doxoh avatar Oct 29 '22 13:10 Doxoh

Could you please test it with the latest development version of module and client?

Tested, but same issue, but works on a blank ressource. When I add a resource with only a keyup as a dependency of the main resource, all events are working again.

I have commented the first alt.off('keyup', ....) executed in my code, and the key events are working again. It's like it was the first alt.off() that turned off all the key events without the function being equal.


namespace SignIn {
    const keyup = (key: number) => {
        // Enter
        if (key == 13) _webview.emit('validate');
    };

(...)

    export function done() {
        (...)

        alt.off('keyup', keyup); // <==== Him
        alt.toggleGameControls(true);
        alt.showCursor(false);
    }

}

FlorianV85 avatar Oct 29 '22 19:10 FlorianV85

Can you test on dev55, it should have been fixed

yannbcf avatar Nov 18 '22 10:11 yannbcf

@FlorianV85 ^^^

xxshady avatar Nov 22 '22 20:11 xxshady

Oops, sorry. I have the same issue with alt.off on 13.0-dev57

FlorianV85 avatar Nov 22 '22 21:11 FlorianV85

@FlorianV85 can you try to test with this code?

import alt from "alt-client"

const handler = (key) => {
  alt.log("keydown", key)
  listenKeydown()
}

const listenKeydown = () => {
  const [listener] = alt.getEventListeners("keydown")
  alt.log("keydown listener:", listener)
  if (listener) {
    alt.log("alt.off keydown")
    alt.off("keydown", listener)
  }

  alt.log("alt.on keydown")
  alt.on("keydown", handler)
}

listenKeydown()

xxshady avatar Nov 23 '22 12:11 xxshady

Tested on a blank resource :

I also tested adding the code to a file in my resources and importing it into the main client file. When this code is present, all other events work. When it is not imported, the issue is back.

FlorianV85 avatar Nov 24 '22 07:11 FlorianV85

maybe the issue is that you are passing invalid function reference to alt.off in your code

xxshady avatar Nov 24 '22 10:11 xxshady

you can validate function reference with this code:

const isHandlerRegistered = (event, handler) => {
  return new Set(alt.getEventListeners(event)).has(handler)
}

xxshady avatar Nov 24 '22 10:11 xxshady

it might be related to this one https://github.com/altmp/altv-js-module/issues/229

xxshady avatar Nov 25 '22 10:11 xxshady

you can validate function reference with this code:

const isHandlerRegistered = (event, handler) => {
  return new Set(alt.getEventListeners(event)).has(handler)
}

I have tested my different handlers with this and they all return "true" for me :s It is always the same function stored in a const (example code earlier in the issue).

FlorianV85 avatar Nov 25 '22 10:11 FlorianV85

@FlorianV85 can you try to reproduce it on latest release?

xxshady avatar Feb 02 '23 23:02 xxshady

@FlorianV85 can you try to reproduce it on latest release?

Tested on the latest DEV branch and it works!

FlorianV85 avatar Feb 15 '23 03:02 FlorianV85

so i guess this one: https://github.com/altmp/altv-js-module/issues/229 was the reason

xxshady avatar Feb 15 '23 04:02 xxshady