问题
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