可以在aframe实体上捕获键盘事件

时间:2017-09-05 10:15:18

标签: three.js aframe

是否可以捕获AFRAME实体上的特定键盘键事件?

我在实体上尝试通过addEventListener,但似乎事件不会传播到实体(有和没有wasd-controls)。

AFRAME.registerComponent('listenonclick', { ... init: function () { ... this.el.addEventListener('keydown', function(event) { console.log("onkeydown Button" + event.code); }); ... <a-box listenonclick id='box1' ..."></a-box>

但是,当将事件添加到窗口而不是实体时,它将被触发。

1 个答案:

答案 0 :(得分:4)

这是一种方法:在<场景

之前添加此代码
document.addEventListener('keydown', function(event) {
  document.querySelectorAll('.listenonkey').forEach(function(obj){
    obj.setAttribute('position', '0 0 0');
  });
});

然后

  <a-entity class='listenonkey' .... other stuff... </a-entity>

实际上,此解决方案将为HTML文档添加一个额外的键甚至处理程序,类似于aframe的处理方式, onkeydown 会移动它们。

我还没有找到如何在aframe文档中更优雅地做到这一点。