场景:uniapp打包的app在手持设备上使用,手持设备外接扫描枪,快速扫描
关键:扫描枪一般是触发 键盘事件keydown或keyup
无输入框式
import keymap from './keymap'
export default {data() {return {inputString: '',inputCache: ''}},mounted() {// #ifdef APP-PLUSplus.key.addEventListener("keyup", this.keypress);// #endif },beforeDestroy() {// #ifdef APP-PLUSplus.key.removeEventListener("keyup", this.keypress);// #endif },deactivated() {// #ifdef APP-PLUSplus.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": "+"
}