I'm using nodejs and i just need a function that when i call returns the value of the key pressed (0 if no key pressed).
I have tried using ioHook but that just executes a function whenever a key is pressed.
This is how I would do it on simple js This is not on node js but the code should look like the same only with a different event listener name so I decided to post this and it may help you simulate it on backend
const getCurrentKey = () => new Promise((resolve , reject) =>{
function handleKeyDown(event){
resolve(event);
document.removeEventListener('keydown' , handleKeyDown) // removing the event listener so it'll not be triggered along with others next time
}
document.addEventListener('keydown' , handleKeyDown)
})
and it returns a Promise so for a simple example it can be used like below:
async function test(){
const e = await getCurrentKey();
}