javascript USB 条码扫描器打开浏览器的下载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27960841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:12:58  来源:igfitidea点击:

USB Barcode scanner opens browser's downloads page

javascriptbrowserbarcodebarcode-scanner

提问by yanivsh

I'm trying to scan some barcode to textfield contained in my browser but it opens the downloads page in any browser (chrome, firefox, ie). I'm guessing that there is some input equivalent to CTRL + J that triggers the browser to open the download page.

我正在尝试将一些条形码扫描到我的浏览器中包含的文本字段,但它会在任何浏览器(chrome、firefox,即)中打开下载页面。我猜有一些相当于 CTRL + J 的输入会触发浏览器打开下载页面。

Is anyone ran into this problem? Is there a way to pass it (assuming that I my clients can't change their scanner configuration neither the browser configuration)?

有没有人遇到过这个问题?有没有办法通过它(假设我的客户不能更改他们的扫描仪配置,也不能更改浏览器配置)?

Thanks.

谢谢。

回答by Mustafa sabir

Although it is late to post an answer, I hope this helps someone in future.

尽管发布答案为时已晚,但我希望这对将来的人有所帮助。

The problem is due to the end character sent from the barcode reader. The default setting of my barcode reader is to send CR+LFafter input. This unfortunately opens downloads page in chrome. The fix for this is very simple, instead of configuring the scanner itself (which can be tricky), you can add the following script to your page to ignore the end character sent from barcode scanner:

问题是由条形码阅读器发送的结束字符引起的。我的条码阅读器的默认设置是CR+LF输入后发送。不幸的是,这会在 chrome 中打开下载页面。对此的修复非常简单,而不是配置扫描仪本身(这可能很棘手),您可以将以下脚本添加到您的页面以忽略从条形码扫描仪发送的结束字符:

<script>
  document.addEventListener('keydown', function(event) {
    if( event.keyCode == 13 || event.keyCode == 17 || event.keyCode == 74 )
      event.preventDefault();
  });
</script>

There was also an old bugopened for this with chrome, but this was closed/unresolved since this is not a bug but more of an input configuration issue.

chrome 还为此打开了一个旧错误,但已关闭/未解决,因为这不是错误而是更多的输入配置问题。

回答by A. Blub

You have to setup your scanners.

您必须设置扫描仪。

Ctrl-J = 0x0A = Linefeed for Unix-like Systems

Check the manuels of your scanner-model for that. Normally you can find there some Barcodes which change your settings, otherway is to change your driver or you have settings in your driver for that.

检查您的扫描仪型号的手册。通常你可以找到一些改变你设置的条形码,否则就是改变你的驱动程序或者你的驱动程序中有设置。

回答by Manoj Singh Negi

The problem can be solved by checking if e.ctrlKeytrue then we can ignore the keypress. If the Ctrlkey is pressed the e.ctrlKeyis true

问题可以通过检查是否为e.ctrlKey真来解决,然后我们可以忽略按键。如果Ctrl键被按下了e.ctrlKeyIStrue

<script>
let data = ''

window.onload = function () {
     window.document.body.addEventListener('keydown', function(event){
        if( event.keyCode == 13 || event.keyCode == 16 ||  event.keyCode == 17 ) {
                event.preventDefault();
                    return;
                }

                if(event.ctrlKey) {
                    event.preventDefault();
                    return;
                }

        data += event.key
        console.log(data)
    });
}
</script>

By using this code we can prevent chrome from navigating to Downloads Page.

通过使用此代码,我们可以防止 chrome 导航到下载页面。

回答by George Helmke

//I managed to focus on exactly what was being sent by Chrome, and ignore just that:
var lastKeyCode = "";
document.addEventListener('keydown', function (event) {
    if (lastKeyCode == "ControlLeft" && event.code == "KeyJ")
        event.preventDefault();
    lastKeyCode = event.code;
});

回答by Francesco Meloni

I have this issue with chrome only. Firefox focus goes to google search bar after the first successful shot. I have a Manhattan barcode reader. For now i managed using Web ( also known as epiphany) it is the only one it works but it is not a "common customer suggest"

我只有 chrome 有这个问题。第一次成功拍摄后,Firefox 焦点会转到谷歌搜索栏。我有一个曼哈顿条码阅读器。现在我使用 Web(也称为顿悟)进行管理,它是唯一有效的方法,但它不是“常见的客户建议”

回答by MentallyYours

Putting control like (event.keyCode == 13 || event.keyCode == 17 || event.keyCode == 74)won't fix problem. Because if barcode value has 'J' character in it, you wont be able to get barcoderesult truely. We try to add e.ctrlKeycontrol extra. But this time we retrieve only one character from barcode. It seems that solving problem via jsis hard. Maybe the best option is changing scanner settings.

像控制一样 (event.keyCode == 13 || event.keyCode == 17 || event.keyCode == 74)不会解决问题。因为如果条形码值中有'J'字符,你将无法barcode真正得到结果。我们尝试添加e.ctrlKey额外的控制。但是这次我们只从条形码中检索一个字符。通过似乎js很难解决问题。也许最好的选择是更改扫描仪设置。