Client keydown and Keyup doesn't triggered
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
Have you tried a clean restart of the game without restarting the PC?
And please test it with current dev version too
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
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.
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 ]
Could you please test it with the latest development version of module and client?
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);
}
}
Can you test on dev55, it should have been fixed
@FlorianV85 ^^^
Oops, sorry. I have the same issue with alt.off on 13.0-dev57
@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()
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.
maybe the issue is that you are passing invalid function reference to alt.off in your code
you can validate function reference with this code:
const isHandlerRegistered = (event, handler) => {
return new Set(alt.getEventListeners(event)).has(handler)
}
it might be related to this one https://github.com/altmp/altv-js-module/issues/229
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 can you try to reproduce it on latest release?
@FlorianV85 can you try to reproduce it on latest release?
Tested on the latest DEV branch and it works!
so i guess this one: https://github.com/altmp/altv-js-module/issues/229 was the reason