AFrame - el.addeventlistener - PullRequest
       5

AFrame - el.addeventlistener

0 голосов
/ 02 февраля 2019

Я хочу перехватить событие ctrl-click внутри события click.

el.addEventListener('click', function (e) {
  console.log("event="+e.detail.name); 
  console.log("eventobject="+e); 
  ..blah blah 
}

Я получаю эти результаты "event = undefined" и "eventobject = [object CustomEvent]" соответственно.Не могу понять, что я делаю не так.

1 Ответ

0 голосов
/ 02 февраля 2019
Событие

aframe click отличается от события, которое вы получаете , как правило, из браузера.

Поскольку в щелчках по рамке нет ctrlKey (в конце концов, это среда VR), вы можете вспомнить, нажата ли клавиша Ctrl, и использовать эту информацию при щелчке мышью:

AFRAME.registerComponent('foo', {
  init: function() {
    // variable keeping the ctrl button state
    this.ctrlIsDown = false 

    // bind the function recognizing whether ctrl is pressed
    this.ctrlHandler = AFRAME.utils.bind(this.ctrlHandler, this)
    document.body.addEventListener('keydown', this.ctrlHandler)
    document.body.addEventListener('keyup', this.ctrlHandler)

    // If ctrl is down when a click occured - log it
    this.el.addEventListener('click', (e) => {
      if (this.ctrlIsDown) {
        console.log("ctr key was pressed during the click");
      }
    })
  },
  // save the state of the ctrl button
  ctrlHandler: function(e) {
    // change the variable only if the state differs
    if (!this.ctrlIsDown && e.ctrlKey) {
      this.ctrlIsDown = true
    } else if (this.ctrlIsDown && !e.ctrlKey) {
      this.ctrlIsDown = false
    }
  }
})

Проверьте здесь .

...