[英]Can i make a function that returns an integer based on the key pressed (nodejs)
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).我正在使用nodejs,我只需要一个function,当我调用它时返回按下的键的值(如果没有按下键,则为0)。
I have tried using ioHook but that just executes a function whenever a key is pressed.我曾尝试使用 ioHook,但只要按下某个键,它就会执行 function。
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这就是我在简单 js 上的做法 这不是在节点 js 上,但代码应该看起来相同,只是具有不同的事件侦听器名称,所以我决定发布这个,它可以帮助您在后端模拟它
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:它返回一个Promise所以举个简单的例子,它可以像下面这样使用:
async function test(){
const e = await getCurrentKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.