Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save neelbhanushali/8b77171ae7b775f2b25325760f2b5191 to your computer and use it in GitHub Desktop.
Save neelbhanushali/8b77171ae7b775f2b25325760f2b5191 to your computer and use it in GitHub Desktop.
Javascript for detecting usb connected - hand held - barcode scanner input
// Author: Neel Bhanushali <neal.bhanushali@gmail.com>
document.addEventListener('keydown', function(e) {
// add scan property to window if it does not exist
if(!window.hasOwnProperty('scan')) {
window.scan = []
}
// if key stroke appears after 10 ms, empty scan array
if(window.scan.length > 0 && (e.timeStamp - window.scan.slice(-1)[0].timeStamp) > 10) {
window.scan = []
}
// if key store is enter and scan array contains keystrokes
// dispatch `scanComplete` with keystrokes in detail property
// empty scan array after dispatching event
if(e.key === "Enter" && window.scan.length > 0) {
let scannedString = window.scan.reduce(function(scannedString, entry) {
return scannedString + entry.key
}, "")
window.scan = []
return document.dispatchEvent(new CustomEvent('scanComplete', {detail: scannedString}))
}
// do not listen to shift event, since key for next keystroke already contains a capital letter
// or to be specific the letter that appears when that key is pressed with shift key
if(e.key !== "Shift") {
// push `key`, `timeStamp` and calculated `timeStampDiff` to scan array
let data = JSON.parse(JSON.stringify(e, ['key', 'timeStamp']))
data.timeStampDiff = window.scan.length > 0 ? data.timeStamp - window.scan.slice(-1)[0].timeStamp : 0;
window.scan.push(data)
}
})
// listen to `scanComplete` event on document
document.addEventListener('scanComplete', function(e) { console.log(e.detail) })
@marcossaore
Copy link

it helps me a lot! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment