试图制作一个html页面
event.keyCode
写了这个脚本:
document.addEventListener('keydown', eventT, false);
function eventT(event) {
switch (event.keyCode) {
case 38:
console.log('вперед');
break;
case 40:
console.log('назад');
break;
case 37:
console.log('лево');
break;
case 39:
console.log('право');
break;
}
}
一切正常,但我怎样才能让运动下降添加了这样一个脚本,但它没有用
if (event.keyCode == 37 && event.keyCode == 38) {
console.log('найскосок NUM7')
} else
if (event.keyCode == 38 && event.keyCode == 39) {
console.log('найскосок NUM9')
}
又遇到一个问题,当你按向上箭头和向左箭头时,向上箭头被打断,这可以避免吗?
<HTML>
<HEAD>
<TITLE>Control Panel</TITLE>
</HEAD>
<BODY>
<H1>Control Panel</H1>
<br/>
<br/>
<script>
document.addEventListener('keydown', eventT, false);
function eventT(event) {
if (event.keyCode == 37 && event.keyCode == 38) {
console.log('найскосок NUM7')
document.addEventListener('keydown', eventT, false);
} else
if (event.keyCode == 38 && event.keyCode == 39) {
console.log('найскосок NUM9')
}
console.log(event.keyCode)
switch (event.keyCode) {
case 38:
console.log('вперед');
document.addEventListener('keydown', eventT, false);
break;
case 40:
console.log('назад');
document.addEventListener('keydown', eventT, false);
break;
case 37:
console.log('лево');
document.addEventListener('keydown', eventT, false);
break;
case 39:
console.log('право');
break;
}
}
</script>
</BODY>
</HTML>
要处理同时击键,您需要分别记录击键,并分别处理它们。尝试:
您感到困惑...这是类似于更正确实现的内容: