uniapp+native.js monitors android usb code scanner

       Recently, the project encountered such a demand. The Android system cash register needs to monitor the scanning data of the USB scanner when there is no input box focus . The applied technology is uniapp integrated into the Android native. At first, I kept using Android to write monitoring methods, and then threw the scanned code data back to uniapp. However, when debugging, I found that it was not so ideal. One reason is that the page is written by Vue, and when the applet page is opened natively, it cannot be started. Monitoring USB scan code events, maybe I didn't use the right method. Another problem is the communication between uniapp and the Android host. Finally, uniapp is used to monitor the keyboard keypress event, but uniapp does not monitor the keypress event for android. If it is a window, you can write it like this:

window.addEventListener('keypress',function(e){

})

      On the Android platform, you can only use H5 native.js to monitor the keyup event. Without the keypress event, the effect is actually the same. You can monitor the USB scanner data without an input box (it is actually an external keyboard). KeyCode also needs to be converted. The key-value correspondence table here needs to be Android.

<template>
	<view >	
     <text>输入内容:{
   
   {inputString}}</text>
	</view>
</template>


data() {
    	  return {
				 
				inputString:'',
				inputCache:'',
				 
			}
},

onLoad() {
	plus.key.addEventListener("keyup",this.keypress);
},
methods: {
	keypress(e){
				// console.log('focusElement',this.focusElement)
				//if(this.focusElement) return;
				if(e.keyCode===66){
								  this.inputString = this.inputCache;
								  this.inputCache = '';
				}else{
								  this.inputCache += this.keyValue(e.keyCode)
				}
				console.log('keypress argument:',e)
			},

	keyValue(keyCode){
				switch(keyCode){
					case 7:
					  return 0;
					case 8:
					  return 1;
					case 9:
					  return 2;
					case 10:
					  return 3;
				    case 11:
				      return 4;
				    case 12:
				      return 5;
				    case 13:
					  return 6;
				    case 14:
					  return 7;
				    case 15:
					  return 8;
				    case 16:
					  return 9;
				    case 156:
					   return '-';
					case 70:
					    return '=';
					case 157:
						return '+';
					default :
					    return '' ;
					
				}
			},
}

Guess you like

Origin blog.csdn.net/qq_33278354/article/details/126409071