zoukankan      html  css  js  c++  java
  • js 扫码枪的输入

    关于js 获取扫码枪的输入获取方式,之前在网上搜了好多,都是大同小异的,都是说扫码枪输入的时间间隔不会超过30毫秒。但事实上我拿了几台电脑测试的结果是,有的时间间隔甚至超过了100毫秒,所以用时间间隔去判断是人工输入还是扫码枪输入,感觉还是不怎么理想,所以我换了一种逻辑去实现获取扫码枪的输入,代码如下:

        scanCode() {
          let code = ''
          let lastTime, nextTime
          let lastCode, nextCode
          let that = this
          window.document.onkeypress = function(e) {
            if (window.event) { // IE
              nextCode = e.keyCode
            } else if (e.which) { // Netscape/Firefox/Opera
              nextCode = e.which
            }
            if (e.which === 13) {
              if (code.length < 3) return // 手动输入的时间不会让code的长度大于2,所以这里只会对扫码枪有
              console.log(code)
              console.log('扫码结束')
              // that.distinguishCode(code) // 获取到扫码枪输入的内容,做别的操作
              code = ''
              lastCode = ''
              lastTime = ''
              return
            }
            nextTime = new Date().getTime()
            if (!lastTime && !lastCode) {
              console.log('扫码开始。。。')
              code += e.key
            }
    
            if (lastCode && lastTime && nextTime - lastTime > 500) { // 当扫码前有keypress事件时,防止首字缺失
              console.log('防止首字缺失。。。')
              code = e.key
            } else if (lastCode && lastTime) {
              console.log('扫码中。。。')
              code += e.key
            }
            lastCode = nextCode
            lastTime = nextTime
          }
        }
  • 相关阅读:
    nyoj 139 我排第几个--康拓展开
    树形dp--hdu 3534 Tree
    hdu 2196 Computer 树形dp模板题
    poj 2342 Anniversary party 简单树形dp
    hdu 4738 Caocao's Bridges 图--桥的判断模板
    poj 1144 Network 图的割顶判断模板
    poj 3159 Candies 差分约束
    poj 3169 Layout 差分约束模板题
    codeforces C. Triangle
    java中过滤器、监听器、拦截器的区别
  • 原文地址:https://www.cnblogs.com/yyh1/p/11206505.html
Copyright © 2011-2022 走看看