自学内容网 自学内容网

uniapp小程序监听外接扫描枪

场景:uniapp打包的app在手持设备上使用,手持设备外接扫描枪,快速扫描

关键:扫描枪一般是触发 键盘事件keydown或keyup

无输入框式

import keymap from './keymap'
export default {
data() {
return {
inputString: '',
inputCache: ''
}
},
mounted() {
// #ifdef APP-PLUS
plus.key.addEventListener("keyup", this.keypress);
// #endif 
},
beforeDestroy() {
// #ifdef APP-PLUS
plus.key.removeEventListener("keyup", this.keypress);
// #endif 
},
deactivated() {
// #ifdef APP-PLUS
plus.key.removeEventListener("keyup", this.keypress);
// #endif 
},
methods: {
keypress(e) {
if (e.keyCode === 66 || e.key === 'Enter') { 
this.inputString = this.inputCache;
this.onConfirm(this.inputString);
this.inputCache = '';
} else {
this.inputCache +=keymap[e.keyCode] || '';
}
}
onConfirm(code) {
console.log('拿到的code', code);
//写你的逻辑
},
}
}

keymap.js

export default {
"7": "0",
"8": "1",
"9": "2",
"10": "3",
"11": "4",
"12": "5",
"13": "6",
"14": "7",
"15": "8",
"16": "9",
"29": "A",
"30": "B",
"31": "C",
"32": "D",
"33": "E",
"34": "F",
"35": "G",
"36": "H",
"37": "I",
"38": "J",
"39": "K",
"40": "L",
"41": "M",
"42": "N",
"43": "O",
"44": "P",
"45": "Q",
"46": "R",
"47": "S",
"48": "T",
"49": "U",
"50": "V",
"51": "W",
"52": "X",
"53": "Y",
"54": "Z",
"55": ",",
"56": ".",
"59": "",
"69": "-",
"70": "=",
"81": "+"
}

原文地址:https://blog.csdn.net/hu104160112/article/details/142910451

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!