Skip to content

Instantly share code, notes, and snippets.

@benjaminrau
Created November 14, 2024 16:18
Show Gist options
  • Save benjaminrau/9331bb562c2745aeafcf954a6e2ccfdd to your computer and use it in GitHub Desktop.
Save benjaminrau/9331bb562c2745aeafcf954a6e2ccfdd to your computer and use it in GitHub Desktop.
Simulate USB Scanner barcode scan
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