扫描枪事件监听代码(关键字:浏览器、二维码、条形码、H5、Javascript、Web、史上最好的浏览器扫描枪监听代码)

1、引入一下javascript代码

/**
 * 扫描枪工具类
 */
var ScanCodeUtil={ 	 	
 	keys:[], 	 	
 	code:null,
 	verify:function(key){
 		if(  key.keyCode == 13 ){//Enter
 			return false;
 		}
 		if(  key.keyCode == 16 ){//Shift
 			return false;
 		}
 		return true;
 	},
 	execute:function(key,callback){
 		this.keys.push(key);
 		var keys=this.keys;
 		var endTime=key.timeStamp;
 		var code="";
 		if( key.keyCode == 13 ){//扫描枪结束时的自动回车 			
 			for( var i=keys.length-1;i >= 0;i-- ){
 				if( endTime - keys[i].timeStamp < 50 ){ 	 					
 					if( this.verify(keys[i]) ){
 						code = keys[i].key+code;
 					} 	 					
 					endTime=keys[i].timeStamp;
 				}else{
 					break;
 				} 	
 						
 			} 	 		
 			this.code=code; 	 			
 			if( code ){
 				callback(code); 			
 			}		
 		}
 	},
 	listen:function(callback){
 		//1.模拟聚焦使得页面可以监听到扫描事件 		
 		setInterval(function(){
 			var $scanCodeInput= $("<input type='text'/>")
 	 		$("body").append($scanCodeInput);
 	 		$scanCodeInput.focus();
 	 		$scanCodeInput.remove();
 			
 		}, 1000);
 		//2.监听扫描事件
 		$("body").keydown(function(event){ 			
 	 		ScanCodeUtil.execute(event,callback);	 	 	
 	 	}); 	 		
 	}	 
};

2、使用实例

	$(function(){	
		
		ScanCodeUtil.listen(function(code){
			//code为扫描枪扫到的二维码/条形码内容
			//写你的业务代码
 	 		alert("code:"+code); 	 	
 	 	});		
 	 	
	});

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我了解您的要求。以下是一个简单的Java代码示例,用于监听USB扫描枪数据: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.TooManyListenersException; import javax.usb.UsbConfiguration; import javax.usb.UsbDevice; import javax.usb.UsbDeviceDescriptor; import javax.usb.UsbEndpoint; import javax.usb.UsbException; import javax.usb.UsbHostManager; import javax.usb.UsbHub; import javax.usb.UsbInterface; import javax.usb.UsbPipe; import jssc.SerialPort; import jssc.SerialPortEvent; import jssc.SerialPortEventListener; import jssc.SerialPortException; public class USBScannerListener implements SerialPortEventListener { private static final int VENDOR_ID = 0x067B; private static final int PRODUCT_ID = 0x2303; private static final int DATA_BITS = 8; private static final int STOP_BITS = 1; private static final int PARITY = 0; private static final int BAUD_RATE = 9600; private SerialPort serialPort; public USBScannerListener() { // 查找特定的 USB 设备 UsbDevice device = findDevice(VENDOR_ID, PRODUCT_ID); if (device == null) { System.err.println("USB Scanner not found."); return; } // 在USB设备上找到输入端点 UsbInterface iface = findInterface(device); UsbEndpoint endpoint = (UsbEndpoint) iface.getUsbEndpoints().get(0); try { // 打开 USB 传输管道 UsbPipe pipe = endpoint.getUsbPipe(); pipe.open(); // 设置串口参数 serialPort = new SerialPort("COM1"); serialPort.openPort(); serialPort.setParams(BAUD_RATE, DATA_BITS, STOP_BITS, PARITY); // 注册串口数据监听事件 serialPort.addEventListener(this); serialPort.setEventsMask(SerialPort.MASK_RXCHAR); } catch (UsbException | TooManyListenersException | SerialPortException ex) { System.err.println("Error initializing USB scanner listener: " + ex.getMessage()); } } private UsbDevice findDevice(int vendorId, int productId) { UsbDevice device = null; try { // 获取 USB 根集线器 UsbHub rootHub = UsbHostManager.getUsbServices().getRootUsbHub(); // 递归遍历 USB 树来查找设备 device = findDevice(rootHub, vendorId, productId); } catch (SecurityException | UsbException ex) { System.err.println("Error finding USB device: " + ex.getMessage()); } return device; } private UsbDevice findDevice(UsbHub hub, int vendorId, int productId) throws SecurityException, UsbException { UsbDevice device = null; // 遍历每个 USB 设备 for (UsbDevice d : (Iterable<UsbDevice>) hub.getAttachedUsbDevices()) { // 如果设备有子集线器,递归查找子集线器 if (d.isUsbHub()) { device = findDevice((UsbHub) d, vendorId, productId); if (device != null) { break; } } // 如果设备匹配给定的厂商ID和产品ID,则返回设备USB UsbDeviceDescriptor desc = d.getUsbDeviceDescriptor(); if (desc.idVendor() == vendorId && desc.idProduct() == productId) { device = d; break; } } return device; } private UsbInterface findInterface(UsbDevice device) throws UsbException { UsbInterface iface = null; // 遍历每个配置并找到第一个具有输入端点的接口 for (UsbConfiguration config : (Iterable<UsbConfiguration>) device.getUsbConfigurations()) { for (UsbInterface i : (Iterable<UsbInterface>) config.getUsbInterfaces()) { if (i.getUsbEndpoints().size() > 0) { iface = i; break; } } if (iface != null) { break; } } if (iface == null) { throw new UsbException("Device does not have any interfaces with input endpoints."); } return iface; } @Override public void serialEvent(SerialPortEvent event) { if (event.isRXCHAR()) { try { // 读取串口数据并输出到控制台 String data = serialPort.readString(); System.out.println(data); } catch (SerialPortException ex) { System.err.println("Error reading data from serial port: " + ex.getMessage()); } } } public static void main(String[] args) throws IOException { // 创建 USB 扫描仪侦听器 USBScannerListener listener = new USBScannerListener(); // 在按下回车键之前等待用户输入 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Press Enter to exit."); br.readLine(); // 关闭串口和 USB 管道 try { listener.serialPort.closePort(); listener.serialPort.removeEventListener(); listener.serialPort = null; } catch (SerialPortException ex) { System.err.println("Error closing serial port: " + ex.getMessage()); } } } ``` 这个代码在 jssc 库的帮助下工作,它是一组Java命令,可以访问串口和USB设备和数据的通信。 若要使用此示例,请确保已安装jssc库。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值