为什么 CraftyJS/Chrome 限制并发按键事件的数量?
Why does CraftyJS/Chrome limit the number of concurrent key press events?
我正在尝试使用 CraftyJS 开发游戏。我正在使用
- 狡猾 0.7.1
- Chrome 50.0.2661.94
- Windows 10
- 2016 戴尔 XPS15。
我注意到键盘事件的处理方式有些奇怪。
我猜这在很大程度上与 Chrome 甚至可能有关
我的物理键盘,而 Crafty 只与我相关
使用它 API.
首先,这是我的 SSCCE。此代码在按下时向 keys 数组添加一个键,在释放时将其删除,并每秒注销该数组。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>keyTest</title>
<script src="../crafty.js"></script>
<script>
window.onload = function(){
Crafty.init(window.innerWidth, window.innerHeight, document.getElementById('game'));
var keys = [];
var keyDown = function(e){
console.log("KeyDown " + e.key);
keys.push(e.key);
};
var keyUp = function(e){
console.log("KeyUp " + e.key);
keys.splice(keys.indexOf(e.key), 1);
};
Crafty.e("Keyboard").bind("KeyUp", keyUp).bind("KeyDown", keyDown);
Crafty.e("Delay").delay(function(){console.log(keys)}, 1000, -1);
};
</script>
</head>
<body>
<div id="game"></div>
</body>
</html>
我注意到一些奇怪的行为:
首先,看起来 Crafty 一次只能识别四个字母键,除非它们可以同时用右手和左手输入。例如,立即按住 ASDFE 会导致: [65, 83, 68, 70]
无论我按多少 bash,E 键都无法识别。但是,如果我按住 ASDFJKL,那么我会看到:[65, 83, 68, 70, 74, 75, 76]
.
一次只能识别两个箭头键,除非第三个是向下箭头。例如,按 LEFT、UP、RIGHT 会导致:[37, 38]
但 LEFT DOWN RIGHT 会导致:[37, 39, 40]
这到底是怎么回事?我的猜测是我的手指和我的 JS 之间有人试图巧妙地纠正错误的按键(拼写错误),但我不知道是谁,我也不知道是什么规则来管理这个。
编辑:我怀疑是我的键盘本身(或 OS)未能发送这些键盘事件。但我仍在寻找确认这一点的好方法。
正如@David 发现的那样,这个问题被称为 keyboard ghosting:
"Ghosting" is the problem that some keyboard keys don't work when multiple keys are pressed simultaneously. The key presses that don't show up on the computer or seem to have disappeared are said to have been "ghosted". On most keyboards, even some that are explicitly marketed as "Anti-Ghosting," this happens with many three key combinations. Imagine playing your favorite video game and not being able to, say, run diagonally and fire your weapon at the same time (say pressing a, w, and g simultaneously). This is a result of the internal design of most existing keyboards...
由于并非每个消费者都有游戏键盘(尤其不会遇到此问题),我想您唯一能做的就是围绕不需要同时按下三个或更多键来设计您的游戏.
我正在尝试使用 CraftyJS 开发游戏。我正在使用
- 狡猾 0.7.1
- Chrome 50.0.2661.94
- Windows 10
- 2016 戴尔 XPS15。
我注意到键盘事件的处理方式有些奇怪。 我猜这在很大程度上与 Chrome 甚至可能有关 我的物理键盘,而 Crafty 只与我相关 使用它 API.
首先,这是我的 SSCCE。此代码在按下时向 keys 数组添加一个键,在释放时将其删除,并每秒注销该数组。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>keyTest</title>
<script src="../crafty.js"></script>
<script>
window.onload = function(){
Crafty.init(window.innerWidth, window.innerHeight, document.getElementById('game'));
var keys = [];
var keyDown = function(e){
console.log("KeyDown " + e.key);
keys.push(e.key);
};
var keyUp = function(e){
console.log("KeyUp " + e.key);
keys.splice(keys.indexOf(e.key), 1);
};
Crafty.e("Keyboard").bind("KeyUp", keyUp).bind("KeyDown", keyDown);
Crafty.e("Delay").delay(function(){console.log(keys)}, 1000, -1);
};
</script>
</head>
<body>
<div id="game"></div>
</body>
</html>
我注意到一些奇怪的行为:
首先,看起来 Crafty 一次只能识别四个字母键,除非它们可以同时用右手和左手输入。例如,立即按住 ASDFE 会导致:
[65, 83, 68, 70]
无论我按多少 bash,E 键都无法识别。但是,如果我按住 ASDFJKL,那么我会看到:[65, 83, 68, 70, 74, 75, 76]
.一次只能识别两个箭头键,除非第三个是向下箭头。例如,按 LEFT、UP、RIGHT 会导致:
[37, 38]
但 LEFT DOWN RIGHT 会导致:[37, 39, 40]
这到底是怎么回事?我的猜测是我的手指和我的 JS 之间有人试图巧妙地纠正错误的按键(拼写错误),但我不知道是谁,我也不知道是什么规则来管理这个。
编辑:我怀疑是我的键盘本身(或 OS)未能发送这些键盘事件。但我仍在寻找确认这一点的好方法。
正如@David 发现的那样,这个问题被称为 keyboard ghosting:
"Ghosting" is the problem that some keyboard keys don't work when multiple keys are pressed simultaneously. The key presses that don't show up on the computer or seem to have disappeared are said to have been "ghosted". On most keyboards, even some that are explicitly marketed as "Anti-Ghosting," this happens with many three key combinations. Imagine playing your favorite video game and not being able to, say, run diagonally and fire your weapon at the same time (say pressing a, w, and g simultaneously). This is a result of the internal design of most existing keyboards...
由于并非每个消费者都有游戏键盘(尤其不会遇到此问题),我想您唯一能做的就是围绕不需要同时按下三个或更多键来设计您的游戏.