Skip to content

Instantly share code, notes, and snippets.

@QuvonchbekBobojonov
Created January 9, 2024 06:29
Show Gist options
  • Save QuvonchbekBobojonov/322f144928cda2deeb143e6ef85af9b9 to your computer and use it in GitHub Desktop.
Save QuvonchbekBobojonov/322f144928cda2deeb143e6ef85af9b9 to your computer and use it in GitHub Desktop.
USB barcode scanning code in javascript
let scan = ''
const lang = window.localStorage.getItem('lang', 'ru')
window.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
if (document.querySelector('#id_barcode')) {
document.querySelector('#id_barcode').value = scan
document.querySelector('#id_barcode').dispatchEvent(new Event('input'))
scan = ''
} else if (window.location.pathname === window.localStorage.getItem('url_main')) {
addCard(`/${lang}/pos/add-to-card/barcode/${scan}/`)
scan = ''
}
} else if (e.target.tagName === 'INPUT') {
scan = ''
} else {
scan += e.key
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment