SoFunction
Updated on 2025-02-28

Detailed explanation of the idea code for obtaining the barcode scanned by scanning the code gun using JavaScript.

The following is an example code to introduce the implementation method of scanning barcodes by scanning js code guns. The specific code is as follows:

 var keycode = "";
  var lastTime=null,nextTime;
  var lastCode=null,nextCode;
	document.οnkeydοwn=function(e){
		if(){
			// IE
			nextCode = 
		} else if(){
			// Netscape/Firefox/Opera
			nextCode = 
		}
		
		//+ key, add new data row		if(nextCode==107 || nextCode==187){
			addNewGoodLine();
		} 
		//-key, delete the last line of data		else if(nextCode==109 || nextCode==189){
			$(".new_products:last").remove();
		}
		//The number keys above the letters 0-9 correspond to the key code value 48-57		//Number keypad Number key 0-9 corresponding key code value 96-105		else if((nextCode>=48&&nextCode<=57) || (nextCode>=96&&nextCode<=105)){
			//There is a problem with the corresponding characters of the keycode value of the numeric keyboard, so manually adjust the keycode value.			var codes = {'48':48,'49':49,'50':50,'51':51,'52':52,'53':53,'54':54,'55':55,'56':56,'57':57,
						 '96':48,'97':49,'98':50,'99':51,'100':52,'101':53,'102':54,'103':55,'104':56,'105':57
			};
			nextCode = codes[nextCode];
			nextTime = new Date().getTime();
		  if(lastCode == null && lastTime == null) {
		  	keycode = (nextCode);
		  } else if(lastCode != null && lastTime != null && nextTime - lastTime <= 30) {
		  	keycode += (nextCode);
		  } else{
				keycode = "";
			  lastCode = null;
			  lastTime = null;
			}
		  lastCode = nextCode;
		  lastTime = nextTime;
		}
		//13 is the key Enter		else if(nextCode==13 && keycode!= ""){
			var code = $(".new_products:last .code").val();
			if(code != ""){
				//The last line has entered data, and a new line is regenerated				addNewGoodLine();
			}
			$(".new_products:last .code").val(keycode).blur();
			keycode = "";
		  lastCode = null;
		  lastTime = null;
		}
	}
	
	function addNewGoodLine(){
		//Generate new data row		var html = '<tr class="new_products">';
			html += '	<td></td>';
			html += '	<td>';
			html += '		<input type="text" class="code" οnblur="getProductDetail()" />';
			html += '	</td>';
			html += '</tr>';
	}
	
	function getProductDetail(){
		//Get the product details and assign the value		
	}

Ideas:

1. Register the onkeydown event and capture the press event of the numeric keys

2. Calculate the time interval when the number key is pressed. If the interval is less than 30 milliseconds, it is the code scanner input.

3. Capture the press event of the Enter case, determine whether the captured code scan gun input value is empty or not, assign values ​​to the corresponding text box, and trigger the method of finding barcodes to find products.

Summarize

This is the article about using JavaScript to obtain the barcode obtained by scanning the code of the QR code gun. This is the end. For more related contents of the QR code scanner, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!