场景
大家跟我想象这样一个场景:
在你的系统里,某个页面,你想实现一个快捷键,你“X”+“#”,神乎奇迹的调用了一个接口,是不是很炫酷。
代码如下:
我这里是M+U键盘 ,然后执行了某个方法,当然可以用switch,多种组合键,执行不同的方法或传不同的参数等等
mounted () {
$('body').keydown((e) => {
console.log(e.keyCode)
if (window.event) {
// IE
if(this.keyCode1==77){
this.keyCode2 = e.keyCode
}else{
this.keyCode1 = e.keyCode
}
} else if (e.which) {
// Netscape/Firefox/Opera
if(this.keyCode1==77){
this.keyCode2 = e.which
}else{
this.keyCode1 = e.which
}
}
var ctrlKey = e.ctrlKey || e.metaKey;
if (ctrlKey) return
if (e.target.tagName === 'INPUT') {
return
}
if(this.keyCode1==77&&this.keyCode2==85&&this.selectedRow.output.value){
....调用方法 this.func()
}
})
},
//销毁的时候 解绑一下
destroyed() {
$('body').unbind('keydown')
}