aframe - AFrame - el.addeventlistener
问题描述
I want to trap a ctrl-click event within a click event.
el.addEventListener('click', function (e) {
console.log("event="+e.detail.name);
console.log("eventobject="+e);
..blah blah
}
I get these results "event=undefined" and "eventobject=[object CustomEvent]", respectively. Can't figure out what i I'm doing wrong.
解决方案
aframeclick
事件与您通常从浏览器获得的事件不同。
由于没有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
}
}
})
在这里查看。
推荐阅读
- sql - 如何推断从一种状态转换到另一种状态的指标?
- python-3.x - 如何检查 MongoDB 集合中的字典中是否存在键、值对
- python - Python 图形
- c# - AsyncFunction().GetAwaiter().GetResult() 和 Task.Run(() => AsyncFunction).GetAwaiter().GetResult() 有什么区别?
- mariadb - MariaDB CONCAT / GROUP_CONCAT
- python - gzip API 响应问题(python)
- python - 使用字典理解过滤 django 模型
- python - 将 2d Numy 数组添加到 1d Numpy 数组
- microsoft-graph-api - 特定日历的奇怪确定性“服务不可用”错误
- java - 将日期发送到 postgresql