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

泄露秘密 提交于 2019-12-08 01:36:42

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/10005451/scanning-a-barcode-with-an-ascii-control-character-into-an-input-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!