js 獲取掃碼槍輸入數(shù)據(jù)的方法
1、掃碼槍相當(dāng)于鍵盤輸入設(shè)備,輸入一連串?dāng)?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) { // 當(dāng)掃碼前有keypress事件時,防止首字缺失 code = e.key; } else if (lastCode && lastTime) { code += e.key; } lastCode = nextCode; lastTime = nextTime; }
PS:下面看下js獲取USB掃碼槍數(shù)據(jù)的代碼
前言
找了很多相關(guān)的教程不太好用,汲取各家之長總結(jié)精簡了一下
原理
- 掃碼槍掃描到的條形碼每一位會觸發(fā)一次onkeydown事件
- 比如掃描條碼位‘1234567890'的條形碼,會連續(xù)執(zhí)行10次onkeydown事件
- 條碼掃描到最后一位,會直接觸發(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; } } }
總結(jié)
到此這篇關(guān)于js 獲取掃碼槍輸入數(shù)據(jù)的文章就介紹到這了,更多相關(guān)js 獲取掃碼槍輸入數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
當(dāng)自定義數(shù)據(jù)屬性為json格式字符串時jQuery的data api問題探討
當(dāng)自定義數(shù)據(jù)屬性是一個 json 格式字符串時,緩存的數(shù)據(jù)如果被修改, 則修改后的數(shù)據(jù)繼續(xù)存在于緩存系統(tǒng)中, 如果不留意,這可能導(dǎo)致一些BUG,接下來將對此問題詳細(xì)概述下2013-02-02Bootstrap導(dǎo)航欄各元素操作方法(表單、按鈕、文本)
這篇文章主要介紹了Bootstrap導(dǎo)航欄各元素操作方法,針對表單、按鈕、文本進(jìn)行操作,感興趣的小伙伴們可以參考一下2015-12-12一種Javascript解釋ajax返回的json的好方法(推薦)
下面小編就為大家?guī)硪黄环NJavascript解釋ajax返回的json的好方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06JavaScript自定義等待wait函數(shù)實例分析
這篇文章主要介紹了JavaScript自定義等待wait函數(shù),實例分析了自定義等待函數(shù)的實現(xiàn)與使用技巧,需要的朋友可以參考下2015-03-03js鎖屏解屏通過對$.ajax進(jìn)行封裝實現(xiàn)
js鎖屏解屏是通過對$.ajax進(jìn)行封裝實現(xiàn)的,需要的朋友可以參考下2014-07-07原生JS+HTML5實現(xiàn)的可調(diào)節(jié)寫字板功能示例
這篇文章主要介紹了原生JS+HTML5實現(xiàn)的可調(diào)節(jié)寫字板功能,涉及javascript結(jié)合HTML5屬性動態(tài)操作頁面元素實現(xiàn)繪圖功能相關(guān)技巧,需要的朋友可以參考下2018-08-08