Skip to content

Commit

Permalink
Fix "Scan barcode" modal input (#7610)
Browse files Browse the repository at this point in the history
* Prevent browser shortcuts in barcode input field

* Add Alt & Control key input
  • Loading branch information
tomvaneyck authored Jul 10, 2024
1 parent 7655113 commit 8059fb1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/backend/InvenTree/templates/js/translated/barcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,44 @@ function barcodeDialog(title, options={}) {

var barcode = $(modal + ' #barcode');

// The control characters that are used in barcodes.
const controlKeys = {
"BracketRight": String.fromCharCode(29), // Group separator
"Digit6": String.fromCharCode(30), // Record separator
}
let altValue = 0;

barcode.keydown(function(event) {
if (event.ctrlKey) {
// Prevent most of the keyboard shortcuts.
// Not all of them will be blocked, browser don't allow this:
// https://stackoverflow.com/questions/59952382/using-preventdefault-to-override-opening-new-tab
event.preventDefault();
if (event.originalEvent.code in controlKeys) {
event.target.value += controlKeys[event.originalEvent.code];
}
}
else if (event.altKey && event.key != "Alt") {
let val = parseInt(event.key, 10);
if (Number.isNaN(val) || val < 0 || val > 9) {
altValue = 0;
return;
}
altValue *= 10;
altValue += val;
}
});

// Handle 'enter' key on barcode
barcode.keyup(function(event) {
event.preventDefault();

if (event.which == 10 || event.which == 13) {
clearTimeout(barcodeInputTimer);
sendBarcode();
} else if (event.key == "Alt" && altValue > 0) {
event.target.value += String.fromCharCode(altValue);
altValue = 0;
} else {
// Start a timer to automatically send barcode after input is complete
clearTimeout(barcodeInputTimer);
Expand Down

0 comments on commit 8059fb1

Please sign in to comment.