扫码枪解析
扫码枪本质是键盘按键输入,扫码枪在扫码完成会有一个回车输入,我们可以将回车键视为扫码完成。所以监听扫码枪输入实际就是监听按键输入。
代码
barCode :储存扫码数据
//js
open(){
window.addEventListener("keydown", this.autoBarcodeEventFun);
}
// 监听扫码枪事件
autoBarcodeEventFun(e) {
//未输入回车,将数据收集
if (e.keyCode !== 13) {
if (e.keyCode !== 40) {
this.barCode += e.key;
}
} else {
//输入回车,扫码结束,进行自己的逻辑处理
this.func();//自己的逻辑处理
//扫码数据初始化,等待下次扫码
this.barCode = "";
}
},
// 移除扫码枪事件
removeAutoOutEventFun() {
window.removeEventListener("keydown", this.autoBarcodeEventFun);
console.log("扫码枪监听已关闭");
},
func(){
this.removeAutoOutEventFun()
}