gpt4 book ai didi

javascript - 为什么 CraftyJS/Chrome 会限制并发按键事件的数量?

转载 作者:行者123 更新时间:2023-11-30 16:05:07 25 4
gpt4 key购买 nike

我正在尝试使用 CraftyJS 开发游戏。我正在使用

  • 狡猾的 0.7.1
  • Chrome 50.0.2661.94
  • window 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] 无论我如何敲击 E 键,它仍然无法识别。但是,例如,如果我按住 ASDFJKL,那么我会看到:[65, 83, 68, 70, 74, 75, 76]

  • 一次只能识别两个箭头键,除非第三个是向下箭头。例如,按 LEFT、UP、RIGHT 会导致:[37, 38] 但 LEFT DOWN RIGHT 会导致:[37, 39, 40]

    <

这到底是怎么回事?我的猜测是我的手指和我的 JS 之间有人试图巧妙地纠正错误的按键(拼写错误),但我不知道是谁,我也不知道是什么规则来管理这个。

编辑:我怀疑是我的键盘本身(或操作系统)未能发送这些键盘事件。但我仍在寻找确认这一点的好方法。

最佳答案

正如@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...

由于并非每个消费者都拥有游戏键盘(尤其不会遇到此问题),我想您唯一能做的就是围绕不需要同时按下三个或更多键来设计您的游戏。

关于javascript - 为什么 CraftyJS/Chrome 会限制并发按键事件的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37154208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com