I need to scan a barcode with ASCII 29 (group separator) into an HTML input field, and replace all the group separators with |. The following JavaScript function works when I first scan the barcode into Notepad++ and then copy it to the input field, but not when the barcode is scanned directly into the input field. What's the problem?
var barcode = document.getElementById('barcode').value;
barcode = barcode.replace(new RegExp(String.fromCharCode(29), 'g'), '|');
alert(barcode);
On my Symbol Tech barcode scanner, characters are sent as key strokes. For example, the group separator will emulate holding down the left_control key and then sending a right bracket. Your browser will handle the simulated key strokes as if you were trying to use CTRL+] as a shortcut.
You can capture this event with onkeydown and event.which as described here: Firefox onkeydown detect pressed key