欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js读取usb扫码枪数据功能代码实现

程序员文章站 2022-04-15 19:44:44
js读取usb扫码枪数据功能代码实现

js读取usb扫码枪数据功能代码实现

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body class="hold-transition login-page">
<script>
    window.onload = function(e){
        var code = "";
        var lasttime,nexttime;
        var lastcode,nextcode;
 
        document.onkeypress = function(e) {
            nextcode = e.which;
            nexttime = new date().gettime();
 
            if(lastcode != null && lasttime != null && nexttime - lasttime <= 30) {
                code += string.fromcharcode(lastcode); 
            } else if(lastcode != null && lasttime != null && nexttime - lasttime > 100){
                code = "";
            }
            lastcode = nextcode;
            lasttime = nexttime;
        }
        this.onkeypress = function(e){
            if(e.which == 13){
                console.log(code);
                code = "";
            }
        } 
    }
</script>
</body>
</html>