When I use the key-up event listener in JavaScript, the keys "wait" before starting again. I think it's because the key-up needs to listen to specific keys but for me that hasn't worked yet.
I have tried using different tutorials for different perspectives on this problem, but none of them have worked for me yet... I think I just need to not use the key-up event listener but I'm not sure how. I only have only known JavaScript for a few weeks so I don't know how to use advanced methods for things.
var key = "";
document.addEventListener('keyup', e => { key = ""; });
document.addEventListener('keydown', e => { key = e.code; });
setInterval(e => {
document.getElementById('test').innerText = key;
}, Infinity);
<p>Press and hold keys...</p>
<p id="test"></p>
Use a
Set(mdn reference) to track all keys currently pressed. Your current approach of using a single string,key, is impractical for storing multiple keys.