vue中如何监听手机键盘

如题所述

vue项目监听安卓手机物理返回键
1、挂载完成后,判断浏览器是否支持popstate
mounted(){
_f(window.historywindow.history.pushState){
_history.pushState(null,null,document.URL);
_window.addEventListener('popstate',this.fun,false);//false阻止默认事件
_
},
2、页面销毁时,取消监听。否则其他vue路由页面也会被监听(destroyed钩子在使用keep-alive就不会执行)在开发过程中遇到一个问题,每次返回只在第一次有效,后面就没有效果了,于是在每次打开弹框的时候动态绑定事件,在返回时解除绑定,问题就解决了。
destroyed(){
_indow.removeEventListener('popstate',this.fun,false);//false阻止默认事件
},
3、将监听操作写在methods里面,removeEventListener取消监听内容必须跟开启监听保持一致,所以函数拿到methods里面写
methods:{
fun(){
console.log("监听到了");
}
}vue中监听手机键盘的【完成】或【搜索】按钮触发事件,需要两个条件
1.form表单,输入框
[email protected]事件或者直接@keyup然后事件的处理函数里判断event.keyCode===13
Vue中监听键盘事件
keyCode实际键值
48到570到9
65到90a到z(A到Z)
112到135F1到F24
8BackSpace(退格)
9Tab
13Enter(回车)
20Caps_Lock(大写锁定)
32Space(空格键)
37Left(左箭头)
38Up(上箭头)
39Right(右箭头)
40Down(下箭头)
Vue中:
别名实际键值
.deletedelete(删除)/BackSpace(退格)
.tabTab
.enterEnter(回车)
.escEsc(退出)
.spaceSpace(空格键)
.leftLeft(左箭头)
.upUp(上箭头)
.rightRight(右箭头)
.downDown(下箭头)
.ctrlCtrl
.altAlt
.shiftShift
.meta(window系统下是window键,mac下是command键)
另外,Vue中还支持组合写法:
组合写法按键组合:
@keyup.alt.67=”function”Alt+C
@click.ctrl=”function”Ctrl+Click
参考:
vue组件中怎么监听键盘事件
body
divid="counter-event-example"
p{{total}}/p
button-counterv-on:ee="incrementTotal"/button-counter
button-counterv-on:ee="incrementTotal"/button-counter
/div
script
Vue.component('button-counter',{
template:'buttonv-on:click="increment"{{counter}}/button',
data:function(){
return{
counter:0
}
},
methods:{
increment:function(){
this.counter+=1
this.$emit('ee','cc')
}
},
})
newVue({
el:'#counter-event-example',
data:{
total:'arg'
},
methods:{
incrementTotal:function(b){
this.total=b+'1';
}
}
})
/script
/body
子组件通过$emit触发父组件的事件,$emit后面的参数是向父组件传参,注意,父组件的事件处理函数直接写函数名即可,不要加(),参数直接传递到了父组件的methods的事件处理函数了。
温馨提示:答案为网友推荐,仅供参考
相似回答