Created
November 14, 2024 16:18
-
-
Save benjaminrau/9331bb562c2745aeafcf954a6e2ccfdd to your computer and use it in GitHub Desktop.
Simulate USB Scanner barcode scan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function simulateScannerInput(barcode) { | |
const activeElement = document.activeElement; | |
if (!activeElement) { | |
console.error('No active element found!'); | |
return; | |
} | |
// Simulating keydown events for each character in the barcode | |
barcode.split('').forEach(char => { | |
const event = new KeyboardEvent('keydown', { | |
key: char, | |
bubbles: true, | |
cancelable: true | |
}); | |
activeElement.dispatchEvent(event); | |
}); | |
// Simulating Enter key event | |
const enterEvent = new KeyboardEvent('keydown', { | |
key: 'Enter', | |
bubbles: true, | |
cancelable: true | |
}); | |
activeElement.dispatchEvent(enterEvent); | |
} | |
simulateScannerInput('somebarcode35252'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment