Bar Code Scanner and Keyboard Issue

余生颓废 提交于 2020-01-15 06:34:08

问题


Bar Code Scanner and Keyboard. NOTE: My Bar Code Scanner is USB Type.

Then...

What function must be use to trigger the keyboard if i'm at auto.aspx page? I tried this code but no success:

var barcode = document.getElementById('barcodenum');
barcode.addEventListener("keypress", function() { alert("Please use Barcode Scanner!"); document.getElementById('barcodenum').value = "";}, true);  

回答1:


As pointed out by @Robert Skarzycki above, i doubt you can integrate with the scanner using a web page.

On the keypress intercept issue.

Add this to the head section of you page.

  <script type="text/javascript">

        window.onload = function() {              
            var barcode = document.getElementById('barcodenum');
            barcode.addEventListener("keyup", function() {
                alert("Please use Barcode Scanner!");
                document.getElementById('barcodenum').value = "";
            }, true);
        };

    </script>



回答2:


I'm afraid you can't do that through JavaScript. If you were developing desktop app, it could be done.

EDIT: The only one solution in JavaScript is measure the time between the keypress events. Barcode scanner is faster then human, so if you set the - experimentally invented - time limet for gaps between two keypresses, you may cope with this problem. (Source of this idea.)




回答3:


Try this code. I assum that you know about the Jquery. Run this code and type anything from the keyboard while focusing the web page and hit enter key. If this works barcode reader do the same. Configure your barcode reader to pass enter key at the end of code reading. Jquery library

<script type='text/javascript' src='http://code.jquery.com/jquery-1.6.4.js'></script>

Jquery

$(document).ready(function() {
    var barcode="";
$(document).keydown(function(e) {

    var code = (e.keyCode ? e.keyCode : e.which);
    if(code==13)// Enter key hit
    {
        alert(barcode);
    }
    else if(code==9)// Tab key hit
    {
        alert(barcode);
    }
    else
    {
        barcode=barcode+String.fromCharCode(code);
    }
});

});


来源:https://stackoverflow.com/questions/14606005/bar-code-scanner-and-keyboard-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!