Keydown event (JavaScript) doesn't work on loading screen

Hello. I made a very simple loading screen with music, but the “keydown” event doesn’t works until I do click on the screen.

I think it is an issue with the focus, but I can’t find solution. I tried everything, I tried with other JS events, set focus, lua with NUI messages. Nothing seems to works

index.html

<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body style="background:#000">
		<audio id='muzica' style="display:none" volume='0.4' autoplay>
			<source src="audio.mp3" type="audio/mpeg">
			Browser-ul tau nu suporta acest tip de fisier.
		</audio>
		<script>
			var play = true;
			var audio = document.getElementById("muzica");
			audio.volume = 0.4;
			window.addEventListener("keydown", function() {
				console.log(play);
				switch (event.keyCode) {
				  case 32:
					if (play) {
						setTimeout(()=> {
							audio.pause();
							play = false;
						}, 100)
					} else {
						setTimeout(()=> {
							audio.play();
							play = true;
						}, 100)
					}
					break;
				}
				return false;
			}, false);
		</script>
	</body>
</html>

__resource.lua

files {
    'index.html',
    'audio.mp3'
}

loadscreen 'index.html'

Can you help me, please? Thanks!

loadingscreen.zip (4.6 MB)

Help please?

1 Like