[英]Javascript replace event “mousedown” with “spacebar”
I have this little game in HTML with javascript and to play the game I need to click on mouse but I want to replace with spacebar. 我用HTML的HTML游戏和玩游戏我需要点击鼠标,但我想用空格键替换。
var evt = "touchstart";
if (width >= 500) {
width = 320;
height = 480;
canvas.style.border = "1px solid #000";
evt = "mousedown";
}
How can I use spacebar as "mousedown"? 如何将空格键用作“mousedown”?
Thanks! 谢谢!
You have to change the event type to keydown
, and check for the spacebars keycode
which is 32. 您必须将事件类型更改为keydown
,并检查空格keycode
是否为32。
Here is an example of what that would look like: 以下是一个示例:
window.addEventListener('keydown', function(e) {
if (e.keyCode == 32) {
console.log('Spacebar pressed');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.