|
@@ -15,20 +15,46 @@
|
|
|
<view class="">
|
|
<view class="">
|
|
|
{{aaa}}
|
|
{{aaa}}
|
|
|
</view>
|
|
</view>
|
|
|
|
|
+
|
|
|
|
|
+ <keyboard-listener v-if="outscan" @keyup="monitorKeyboard"></keyboard-listener>
|
|
|
</view>
|
|
</view>
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
<script>
|
|
|
|
|
+import keyboardListener from 'uniapp-keyboard-listener';
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
export default {
|
|
export default {
|
|
|
|
|
+ components: {keyboardListener},
|
|
|
data() {
|
|
data() {
|
|
|
return {
|
|
return {
|
|
|
inputString: '',
|
|
inputString: '',
|
|
|
inputCache: '',
|
|
inputCache: '',
|
|
|
text:'',
|
|
text:'',
|
|
|
aaa:'',
|
|
aaa:'',
|
|
|
|
|
+ outscan:false,
|
|
|
|
|
+ keyArr:[],
|
|
|
|
|
+ nvalue:''
|
|
|
};
|
|
};
|
|
|
},
|
|
},
|
|
|
methods: {
|
|
methods: {
|
|
|
|
|
+ monitorKeyboard(e) {
|
|
|
|
|
+ console.log(e)
|
|
|
|
|
+ if (e.key.length == 1) { // 字母和数字的长度都是1,所以只要字母和数字的按键,适配不同的设备
|
|
|
|
|
+ this.keyArr.push(e.key)
|
|
|
|
|
+ } else if (e.key == 'Enter') { // 监听到回车
|
|
|
|
|
+ let newArr = [];
|
|
|
|
|
+ this.nvalue = '';
|
|
|
|
|
+ // 条码中可能会有大写字母和未定义的按键,所以要过滤出来
|
|
|
|
|
+ newArr = this.keyArr.filter(item => item != 'Shift' && item != 'Enter' && item != 'Unidentified')
|
|
|
|
|
+ this.nvalue = newArr.join("")
|
|
|
|
|
+ console.log("回车", newArr, this.nvalue);
|
|
|
|
|
+ // if (this.nvalue) {
|
|
|
|
|
+ // this.search(this.nvalue)
|
|
|
|
|
+ // }
|
|
|
|
|
+ this.keyArr = [];
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
onConfirm(text) {
|
|
onConfirm(text) {
|
|
|
// alert(JSON.stringify(text))
|
|
// alert(JSON.stringify(text))
|
|
|
console.log('拿到的text', text);
|
|
console.log('拿到的text', text);
|
|
@@ -36,20 +62,26 @@ export default {
|
|
|
this.text = text;
|
|
this.text = text;
|
|
|
},
|
|
},
|
|
|
keypress(e) {
|
|
keypress(e) {
|
|
|
- this.aaa = JSON.stringify(e);
|
|
|
|
|
- // alert(JSON.stringify(e))
|
|
|
|
|
|
|
+ console.log(e)
|
|
|
|
|
+ this.aaa = JSON.stringify(e);
|
|
|
if (e.keyCode === 66 || e.key === 'Enter') {
|
|
if (e.keyCode === 66 || e.key === 'Enter') {
|
|
|
this.inputString = this.inputCache;
|
|
this.inputString = this.inputCache;
|
|
|
this.onConfirm(this.inputString);
|
|
this.onConfirm(this.inputString);
|
|
|
this.inputCache = '';
|
|
this.inputCache = '';
|
|
|
} else {
|
|
} else {
|
|
|
- this.inputCache += e.key;
|
|
|
|
|
|
|
+ if (e.key !== 'Shift') {
|
|
|
|
|
+ this.inputCache += e.key;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
- mounted() {
|
|
|
|
|
|
|
+ onLoad() {
|
|
|
|
|
+ this.outscan=true
|
|
|
|
|
+ // uni.onkeyup(res=>{
|
|
|
|
|
+ // console.log(res)
|
|
|
|
|
+ // })
|
|
|
// #ifdef APP-PLUS
|
|
// #ifdef APP-PLUS
|
|
|
- plus.key.addEventListener("keyup", this.keypress);
|
|
|
|
|
|
|
+ // plus.key.addEventListener("keyup", this.keypress);
|
|
|
// #endif
|
|
// #endif
|
|
|
// #ifdef H5
|
|
// #ifdef H5
|
|
|
document.addEventListener("keyup", this.keypress);
|
|
document.addEventListener("keyup", this.keypress);
|