ASCII control character html input text

前端 未结 5 1233
盖世英雄少女心
盖世英雄少女心 2021-02-08 10:00

I\'m trying to process input from a barcode scanner in a javascript browser app. The scanner output is a string of characters, which also contains at least one Group Separator c

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 10:46

    Inspired by Sebastian G, this option allows to catch all Alt + number codes. This doesn't work on IE however.

        var altvalue = '';
        document.getElementById('scanfield').addEventListener('keydown', function (e) {
            if (e.altKey) {
                if (e.keyCode !== 18 /* ALT key */) { altvalue += e.key; }
            }
        });
    
        document.getElementById('scanfield').addEventListener('keyup', function (e) {
            if (e.altKey === false && altvalue != '') {
                this.value += String.fromCharCode(parseInt(altvalue));
                altvalue = '';
            }
        });
    

提交回复
热议问题