栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

你有做过扫码枪的开发吗?知道它的原理吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

你有做过扫码枪的开发吗?知道它的原理吗?

扫码枪是一种类似键盘的输入设备,扫码完成后会以极快的速度输入扫描到的字符,一般在末尾会带一个换行符。原理是检测 keydown 事件两次输入的间隔如果小于一个值的话就当成扫码枪输入。

barpreScanner.js

function BarpreScannerDetect(callback) {  this.lastTime = null  this.nextTime = null  this.pre = ''  this.detect = (e) => {    const keypre = e.keyCode || e.which || e.charCode    this.nextTime = new Date()    // 回车键13    if (keypre === 13) {      if (this.lastTime && this.nextTime - this.lastTime < 30) {        // 扫码枪        // console.log(this.pre)        callback(this.pre)      } else {        // 键盘      }      this.pre = ''      this.lastTime = null      e.preventDefault()    } else {      // 忽略一些没用的字符以及中文输入法bug字符      if (keypre !== 16 && keypre !== 229) {        if (!this.lastTime) {          this.pre = String.fromCharCode(keypre)        } else {          if (this.nextTime - this.lastTime < 30) { // console.log(keypre, String.fromCharCode(keypre)) this.pre += String.fromCharCode(keypre)          } else { this.pre = ''          }        }      }      this.lastTime = this.nextTime    }  }}BarpreScannerDetect.prototype.startDetect = function() {  // console.log('startDetect')  document.addEventListener('keydown', this.detect)}BarpreScannerDetect.prototype.stopDetect = function() {  // console.log('stopDetect')  document.removeEventListener('keydown', this.detect)}export default BarpreScannerDetect
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号