I have bar code scanner.
Sometimes user wants to search items by bar code.
But he is lazy enough and doens't want to use mouse at all to click on input.
Scanner usually inputs from 8 to 13 symbols very fast, no human types so fast.
It would be perfect solution to (1)detect extremely input and if no input element on the page has focus (2) then to select the input with certain class , fill it with content from scanner and start searching.
Second stage is non-issue. I 've no idea how to start with first stage.
Are there any libraries?
Not perfect solution would be to program bar code reader to begin every entering with some key bination and listen for this key and set focus on necessary input. But this would require extra work of other guys.
Is perfect solution reachable?
I have bar code scanner.
Sometimes user wants to search items by bar code.
But he is lazy enough and doens't want to use mouse at all to click on input.
Scanner usually inputs from 8 to 13 symbols very fast, no human types so fast.
It would be perfect solution to (1)detect extremely input and if no input element on the page has focus (2) then to select the input with certain class , fill it with content from scanner and start searching.
Second stage is non-issue. I 've no idea how to start with first stage.
Are there any libraries?
Not perfect solution would be to program bar code reader to begin every entering with some key bination and listen for this key and set focus on necessary input. But this would require extra work of other guys.
Is perfect solution reachable?
Share Improve this question asked May 15, 2017 at 13:58 TebeTebe 3,2149 gold badges43 silver badges63 bronze badges 3- So what event does the scanner send? – epascarello Commented May 15, 2017 at 14:04
- 1 @epascarello: I think these days scanners just tend to emulate keyboard button presses so you wouldn't know any different from scanner and key press – musefan Commented May 15, 2017 at 14:07
- @musefan I was trying to figure out the event so I could tell them to listen for input/keypress/etc on the document.... – epascarello Commented May 15, 2017 at 14:08
2 Answers
Reset to default 14You could respond to keypress
on document
by filling in that key in the input
and giving it focus, if the target of the keypress isn't an input
:
document.addEventListener("keypress", function(e) {
if (e.target.tagName !== "INPUT") {
var input = document.querySelector(".my-input");
input.focus();
input.value = e.key;
e.preventDefault();
}
});
<input type="text" class="my-input">
I was intrigued to find that if I didn't prevent the default, setting the focus made Chrome type the character in the input
(but not Firefox). Hence the preventDefault
to keep Chrome from doing that.
If you need to support obsolete browsers, you may need to do some playing around with keyCode
and/or which
rather than using key
. MDN claims key
support in any modern Chrome or Firefox and IE9+ (doesn't say for Edge).
The best way to capture an IR Scan without focusing a form input is to listen for the "keypress" and "textInput" events.
document.addEventListener('textInput', function (e){
console.log('textInput', e.data)
});
It is also easy to prevent the IR scan data from going into a form input. Focus can even be on an input, say a quantity input, and I can still capture the IR UPC code without affecting the quantity.
document.addEventListener('textInput', function (e){
if(e.data.length >= 6){
console.log('IR scan textInput', e.data);
e.preventDefault();
}
});
I have tested this on Android 4.4 and 7.0 with a CipherLab IR scanner.
Not all IR scanners will trigger the 'textInput' event. If your device does not, then you can check to see if it is emitting something similar with:
monitorEvents(document.body);
With that in, trigger an IR scan. You should see console logs for all the events you have to work with. Found this monitoring trick here: How do you log all events fired by an element in jQuery?
In order to support additional types of IR scanners I also include a listener for the 'keypress" event:
let UPC = '';
document.addEventListener("keypress", function(e) {
const textInput = event.key;
const targetName = event.target.localName;
let newUPC = '';
if (textInput && textInput.length === 1 && targetName !== 'input'){
newUPC = UPC+textInput;
/**
* Checks if at least 6 characters are entered then submit the UPC
*/
if (newUPC.length >= 6) {
this.submit();
}
}
});