状况1:This simple jsfiddle可以正常处理键盘事件(在用户点击jsfiddle的显示面板后)。
情况2: - 当我尝试对此larger jsfiddle using THREE.js with OrbitControls and shaders应用类似的方法时,按键没有被捕获,并且可能导致文本字符出现在jsfiddle的代码区域中。
以下是我在后者使用的相关密钥处理代码: -
function F_handleKeyDown(event)
{
if (event.keyCode === 66)
{
window.isBDown = true; //... 66 is "b"
alert("Pressed b down");
}
}
function F_handleKeyUp(event)
{
if (event.keyCode === 66)
{
window.isBDown = false;
alert("Released b up");
}
}
window.addEventListener('keydown', F_handleKeyDown, false);
window.addEventListener('keyup' , F_handleKeyUp , false);
//... Tried, but didnt solve the problem.
//document.body.addEventListener('keydown', F_handleKeyDown, false);
//document.body.addEventListener('keyup' , F_handleKeyUp , false);
//... Tried, but didnt solve the problem.
//myContainer = document.getElementById('container');
//myContainer.addEventListener('keydown', F_handleKeyDown, false);
//myContainer.addEventListener('keyup' , F_handleKeyUp , false);
情况3: - 当我在localserver .html文件中应用相同的代码时,键盘事件被捕获正常。
问题 因此,在SITUATION 2中 - 复杂的jsfiddle(带有Orbit Controls) - 如何让javascript捕获键盘事件?