ASCII control character html input text

前端 未结 5 1228
盖世英雄少女心
盖世英雄少女心 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:33

    I had the same problem and based on the idea with the event listener I build custom solutions for IE/Chrome and Firefox:

    IE + Chrome (Raidox solution):

    document.getElementById('scanfield').addEventListener('keypress', function(e) {
        if (e.which === 29) {
            this.value += String.fromCharCode(e.which);
        }
    });
    

    Firefox:

    var altValue = '';
    document.getElementById('scanfield').addEventListener('keypress', function(e) {
        if(e.altKey){
            altValue += String.fromCharCode(e.which)
            if(altValue == '0029'){
                this.value += String.fromCharCode(29);
                altValue='';
            }
        }
    });
    

    Edge works out of the box.

提交回复
热议问题