js如何獲取掃碼槍輸入數(shù)據(jù)

這篇文章主要講解了js如何獲取掃碼槍輸入數(shù)據(jù),內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比城固網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式城固網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋城固地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

1、掃碼槍相當于鍵盤輸入設(shè)備,輸入一連串數(shù)字后加一個enter鍵。但在實際開發(fā)中需要區(qū)分是掃描槍輸入還是鍵盤用戶輸入,區(qū)別在于掃碼槍輸入很快。

 let code = '';
   let lastTime, nextTime;
   let lastCode, nextCode;
   window.document.onkeypress = (e) => {
    if (window.event) { // IE
     nextCode = e.keyCode;
    } else if (e.which) { // Netscape/Firefox/Opera
     nextCode = e.which;
    }
    if (nextCode === 13) {
     if (code.length < 3) return; // 手動輸入的時間不會讓code的長度大于2,所以這里只會對掃碼槍有

     console.log(code); // 獲取到掃碼槍輸入的內(nèi)容,做別的操作

     code = '';
     lastCode = '';
     lastTime = '';
     return;
    }
    nextTime = new Date().getTime();
    if (!lastTime && !lastCode) {
     code += e.key;
    }

    if (lastCode && lastTime && nextTime - lastTime > 30) { // 當掃碼前有keypress事件時,防止首字缺失
     code = e.key;
    } else if (lastCode && lastTime) {
     code += e.key;
    }
    lastCode = nextCode;
    lastTime = nextTime;
   }

PS:下面看下js獲取USB掃碼槍數(shù)據(jù)的代碼

前言

找了很多相關(guān)的教程不太好用,汲取各家之長總結(jié)精簡了一下

原理

  1. 掃碼槍掃描到的條形碼每一位會觸發(fā)一次onkeydown事件
  2. 比如掃描條碼位‘1234567890'的條形碼,會連續(xù)執(zhí)行10次onkeydown事件
  3. 條碼掃描到最后一位,會直接觸發(fā)Enter

需要引入jQuery,我這里用的是vue

window.onload = (e)=> {
  document.onkeydown = (e)=> {
  	let nextCode,nextTime = '';
  	let lastTime = this.lastTime;
  	let code = this.code;
    if (window.event) {// IE
      nextCode = e.keyCode
    } else if (e.which) {// Netscape/Firefox/Opera
      nextCode = e.which
    }
    nextTime = new Date().getTime();
    //字母上方 數(shù)字鍵0-9 對應(yīng)鍵碼值 48-57; 數(shù)字鍵盤 數(shù)字鍵0-9 對應(yīng)鍵碼值 96-105
    if((nextCode>=48&&nextCode<=57) || (nextCode>=96&&nextCode<=105)){
    	let 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();
    }
    // 第二次輸入延遲兩秒,刪除之前的數(shù)據(jù)重新計算
    if(nextTime && lastTime && nextTime-lastTime>2000){
			code = String.fromCharCode(nextCode);
    }else{
    	code += String.fromCharCode(nextCode)
    }
    // 保存數(shù)據(jù)
    this.nextCode = nextCode;
    this.lastTime = nextTime;
    this.code = code;
  	// 鍵入Enter
    if(e.which == 13) {
      // 判斷 code 長度(這里就獲取到條碼值了,以下業(yè)務(wù)自由發(fā)揮)
      	code = $.trim(code)
      if (code.length == 13) {
        this.$message('A類條碼:' + code);
      } else if (code.length == 23) {
				this.$message('B類條碼:' + code);
      } else if (code.length == 0) {
				this.$message('請輸入條碼');
      } else{
      	this.$message('條碼不合法:' + code);
      }
      //鍵入回車務(wù)必清空code值
    	this.code = ''
    	return false;
    }
  }
}

看完上述內(nèi)容,是不是對js如何獲取掃碼槍輸入數(shù)據(jù)有進一步的了解,如果還想學習更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:js如何獲取掃碼槍輸入數(shù)據(jù)
分享URL:http://muchs.cn/article34/ipicpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)定制網(wǎng)站、靜態(tài)網(wǎng)站動態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計