K
K
Kizzeon2020-07-06 21:58:29
JavaScript
Kizzeon, 2020-07-06 21:58:29

How to execute a certain code inside a tab after a successful barcode scan?

I have a local site that is opened by a person with a connected scanner and I would like to execute a certain js (modal window for example show) after the scanner successfully processes the barcode.

Tell me how the scanner works on the web (what action it can be triggered for) and how it is better to do it.
Ideally, I would like without additional libraries.

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kocherman, 2020-07-07
@kocherman

In 2007, I automated a mini-shop with a similar scanner. I do not remember the model (in short, the cheapest, only a USB cable and a non-switchable tweeter). No separate drivers seemed to be required, the scanner was defined as a Numpad keyboard. Actually, the scanner stupidly dialed the scanned number as on a regular keyboard. Processing an event is not a problem even in the browser, even in the terminal. Moreover, the operator can scan the code, or he can type it manually on the keyboard - and there is no difference, that is, this is much more difficult to track than just the "scan" event.

A
Anatoly, 2020-07-16
@Reals

Greetings!
Just a couple of months ago, I solved such a problem, wrote a small web application for inventorying a store.
I made the following logic:
I added a global event to "keydown", and it focused on the hidden input.
on the input itself, an event is also set for pressing the "Enter" key. Since the barcode scanner, after sending the code, also emulates pressing the "Enter" key, that is, as it were, completes the input.
And already in the event at the input, on "Enter", a function is launched, in which various checks of the entered information take place.
Here is an example of a global event:

window.addEventListener("keydown", function() {
  try {
    if (window.editing) return;
    let obj = document.getElementById("barcode");
    if (window.searched) obj.value = "";
    window.searched = false;
    obj.focus();
  } catch (e) {
    console.log(e);
  }
});

here some auxiliary checks take place, for example:
if some modal window is open to edit something there, then it does not transfer focus when the keys are pressed (function execution stops);
then the focus is directly on the desired input:
then the searched variable is checked, this variable is set to true in the function that checks the entered data (during the onEnter event), then the field is cleared for a new scan. Since the fact is that when the scanner sends, for example, a 13-digit code, then after each digit a "keydown" event occurs.
Something like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question