John Smith
John Smith

Reputation: 55

Scanning a barcode with an ASCII control character into an input field

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);

Upvotes: 3

Views: 5247

Answers (1)

infinitenothing
infinitenothing

Reputation: 531

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

Upvotes: 1

Related Questions