這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)javascript獲取掃碼槍輸入數(shù)據(jù)的方法,以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、武穴網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
1、掃碼槍相當(dāng)于鍵盤輸入設(shè)備,輸入一連串?dāng)?shù)字后加一個(gè)enter鍵。但在實(shí)際開發(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; // 手動(dòng)輸入的時(shí)間不會(huì)讓code的長(zhǎng)度大于2,所以這里只會(huì)對(duì)掃碼槍有 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事件時(shí),防止首字缺失 code = e.key; } else if (lastCode && lastTime) { code += e.key; } lastCode = nextCode; lastTime = nextTime; }
PS:下面看下js獲取USB掃碼槍數(shù)據(jù)的代碼
原理
需要引入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 對(duì)應(yīng)鍵碼值 48-57; 數(shù)字鍵盤 數(shù)字鍵0-9 對(duì)應(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ù)重新計(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 長(zhǎng)度(這里就獲取到條碼值了,以下業(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('請(qǐng)輸入條碼'); } else{ this.$message('條碼不合法:' + code); } //鍵入回車務(wù)必清空code值 this.code = '' return false; } } }
上述就是小編為大家分享的javascript獲取掃碼槍輸入數(shù)據(jù)的方法了,如果您也有類似的疑惑,不妨參照上述方法進(jìn)行嘗試。如果想了解更多相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊。
網(wǎng)站標(biāo)題:javascript獲取掃碼槍輸入數(shù)據(jù)的方法
新聞來源:http://m.2m8n56k.cn/article38/jdsssp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、電子商務(wù)、靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)