简体   繁体   English

如何在Javascript中区分扫描仪输入和键盘输入?

[英]How do I distinguish between a scanner input and keyboard input in Javascript?

I have gone through answers and came across two ways which can help in distinguishing between scanner and keyboard inputs. 我已经通过了答案,并找到了两种方法,可以帮助区分扫描仪和键盘输入。 It can be done through: 它可以通过以下方式完成:

  • Time Based: Scanner inputs are faster than manual keyboard inputs. 基于时间:扫描仪输入比手动键盘输入快。

  • Prefix Based: Append a prefix to barcodes or scanners (inbuilt in scanner devices) and use it to identify the scanner inputs. 基于前缀:为条形码或扫描仪(内置于扫描仪设备中)附加前缀,并使用它来识别扫描仪输入。

Here are the links: link 1 , link 2 which I have used for the references. 以下是链接: 链接1链接2 ,我用于参考。

The problem which I have run into is that whenever the user manually types some keyboard keys while the scanning event is being fired it gets added to scanner input and leads to inconsistent results. 我遇到的问题是,每当用户在扫描事件被触发时手动键入某些键盘键时,它就会被添加到扫描仪输入并导致不一致的结果。

Here is the code which I am using: 这是我正在使用的代码:

var BarcodeScannerEvents = function(){
  this.initialize.apply(this, arguments);
};

BarcodeScannerEvents.prototype = {
  initialize: function() {
    $(document).on({
      keypress: $.proxy(this._keypress, this)
    });
  },
  _timeoutHandler: 0,
  _inputString: '',
  _keypress: function (e){
    if(this._timeoutHandler){
      clearTimeout(this._timeoutHandler);
    }
    this._inputString += String.fromCharCode(e.which);
    //CHECKS FOR VALID CHARACTERS WHILE SCANNING 
    this._timeoutHandler = setTimeout($.proxy(function(){
      if(this._inputString.length <= 10){
        this._inputString = '';
        return;
      }
      $(document).trigger('barcodescanned', this._inputString);
      this._inputString = '';
    }, this), 20);
  }
};

new BarcodeScannerEvents();

The format for my barcode is: ~xxx-xxx-xxxxxx where x can be any number between 0-9. 条形码的格式为:~xxx-xxx-xxxxxx,其中x可以是0-9之间的任意数字。 If a character which is a number is appended to the barcode it leads to wrong inserts in the database. 如果将数字字符附加到条形码,则会导致数据库中的插入错误。

I have tried comparing the events from keyboard inputs and scanner inputs but to no avail. 我试过比较键盘输入和扫描仪输入的事件但无济于事。 I have given a thought of appending extra characters before each digit and then invalidate the scanned barcode if consecutive numbers appear. 我想过在每个数字前添加额外的字符,然后如果连续的数字出现则使扫描的条形码无效。 But I don't feel this is best way to approach this problem. 但我不认为这是解决这个问题的最佳方法。 Can someone help me out here? 有人可以帮帮我吗?

It is not necessary to judge from keyboard/barcode scanner. 没有必要从键盘/条形码扫描仪判断。
If you decide the Enter(Carriage Return) key notification as input completion on any device, you can use it as simplest trigger to execute Price Look Up/input value verification. 如果您在任何设备上决定输入(回车)键通知作为输入完成,则可以将其用作最简单的触发器来执行价格查询/输入值验证。

Most scanners can add suffix code to the scanned barcode data for notification. 大多数扫描仪可以将后缀代码添加到扫描的条形码数据以进行通知。
The most commonly used is the Enter key, but the Tab key may also be used. 最常用的是Enter键,但也可以使用Tab键。
By sending the suffix code by the barcode scanner, the possibility that the scanner notification and the key input are mixed is much lower than the timeout detection. 通过条形码扫描器发送后缀代码,扫描仪通知和键输入混合的可能性远低于超时检测。

You can do as follows. 你可以这样做。

  • Using the setting barcode, it is set to inform that keys such as Enter, Tab etc. which are not normally included in the barcode as a suffix. 使用设置条形码,设置为通知条形码中通常不包括的Enter,Tab等键作为后缀。
  • Bind an event listener for the corresponding suffix key to the text input field. 将相应后缀键的事件侦听器绑定到文本输入字段。
  • The key code is judged in the event listener, and if it is the suffix key, it assumes that the input of the barcode data is complete, carries out processing such as Price Look Up/input value verification, and moves the input focus to the next field. 在事件监听器中判断密钥代码,如果是后缀密钥,则假定条形码数据的输入完成,执行价格查询/输入值验证等处理,并将输入焦点移动到下一场。

For example see this article. 例如,请参阅此文章。
execute function on enter key 在输入键上执行函数


In Addition: 此外:
Your worries seem to be overwhelmed by situations that do not occur often. 您不必经常出现的情况使您的担忧不堪重负。

If it really happens to be a problem, you should give up dealing with JavaScript. 如果它确实是一个问题,你应该放弃处理JavaScript。
Please acquire scanner data with another program by the following method. 请通过以下方法使用其他程序获取扫描仪数据。 Please notify it to the application in some way. 请以某种方式通知应用程序。

If you want to continue keyboard input emulation, it is better to capture data before the browser or application is notified. 如果要继续键盘输入仿真,最好在通知浏览器或应用程序之前捕获数据。

SetWindowsHookExW function / LowLevelKeyboardProc callback function SetWindowsHookExW函数 / LowLevelKeyboardProc回调函数
EasyHook / Indieteur/GlobalHooks EasyHook / Indieteur / GlobalHooks

hook into linux key event handling / uinput-mapper 挂钩到linux关键事件处理 / uinput-mapper
The Linux keyboard driver / LKL Linux KeyLogger / kristian/system-hook Linux键盘驱动程序 / LKL Linux KeyLogger / kristian / system-hook
system wide keyboard hook on X under linux / Error when trying to build a Global Keyboard Hook in Ubuntu Linux / 10.5.2 Keyboard and Pointer Events Linux下X上的系统范围键盘钩子 / 尝试在Ubuntu Linux / 10.5.2键盘和指针事件中 构建全局键盘钩子时出错


Alternatively, set the scanner to serial port mode and have a dedicated program to receive it. 或者,将扫描仪设置为串行端口模式,并有一个专用程序来接收它。

Serial API 串行API
JavaScript/JQuery communicate with SerialPort/COM1 JavaScript / JQuery与SerialPort / COM1通信

Questions tagged opos / Questions tagged pos-for-.net / Questions tagged javapos 标记为opos / Questions的问题标记为pos-for-.net /问题标记为javapos

My first answer would be to train the users not to touch the keyboard while scanning. 我的第一个答案是训练用户在扫描时不要触摸键盘。 However, the tone of your responses to answers and comments makes it sound like you're thinking more of malicious, intentional attempts to corrupt the data. 但是,您对答案和评论的回复语调使得您更多地考虑恶意,故意破坏数据的尝试。

Beyond kunif's very thorough answer, you're not going to find a solution to the problem you're envisioning or running into. 除了kunif非常彻底的答案,你不会找到解决你想象或遇到的问题的方法。 The reason is that JavaScript is only going to receive from the operating system's input buffer; 原因是JavaScript只会从操作系统的输入缓冲区接收; JS will not (cannot! for OS security reasons) distinguish how the input buffer is filled. JS不会(出于操作系统安全性原因)不能区分输入缓冲区的填充方式。 If keystrokes and scan data are simultaneously being put into the buffer, that is an issue to try to address at the OS or hardware level. 如果同时将击键和扫描数据放入缓冲区,则尝试在OS或硬件级别进行寻址是一个问题。 JavaScript is just not equipped to deal with it. JavaScript没有能力处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM