C
C
cicatrix2017-11-09 13:39:12
JavaScript
cicatrix, 2017-11-09 13:39:12

How to get non-printable character via web form?

The bottom line is this: there is a web form on ASP.Net WebForms, where there is a TextBox into which the client needs to scan a barcode (GS1 Databar).
According to the specification, the barcode contains the FNC1 (0x1D) ASCII Group Separator character (non-printable).
The problem is that the scanner imitates keystrokes when scanning. That is, this character is encoded as clamped CTRL + 0xDD. Accordingly, this character does not fall into the text box and is not displayed in any way. Well, as a result, only printed characters are transmitted to the server.
The problem is on the client side, with JS I am very mediocre, I made such a script:

<script type="text/javascript">
    var ctrlReceived = false;
    function handleStroke(event) {
        var ctl = document.getElementById('hfTargetControl').value;
        if (ctrlReceived && event.keyCode == 221)
            document.getElementById(ctl).value += '|';
        ctrlReceived = (event.keyCode == 17);
        return null;
    };
</script>

hfTargetControl - hidden input, into which the id of the required textbox is written from the server.
Accordingly, I replace the GS combination with |, and on the server I replace it back.
The textbox itself is formed like this:
<input name="ctl00$ContentPlaceHolder1$ctl02$txtScanBoxBarcode"
    type="text" 
    id="txtScanBoxBarcode"
    class="TextField"
    onkeydown="handleStroke(event);"
    style="width:60%;" />

Problem: tested on all browsers, code works everywhere except Safari. Why is a mystery to me, and it's difficult to test, because I work under Windows. Can anyone guess what the problem is?
And the second question: in principle, is it possible to transfer FNC1 to a textbox somehow easier?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2017-11-09
@cicatrix

Yes, you can sniff codes on the js side and transfer them to the server as is, for example, separated by commas, parse them on the server. Or convert to another encoding, as an option:

<script>
var inputKeys = [];
function handleStroke(event) {
    inputKeys.push(event.keyCode);
  onSubmitForm();
};
function onSubmitForm(){
  console.log('Через , =' + inputKeys.join(','));
  
  // если надо пееркодировать, закодировать https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
    var utf8decoder = new TextDecoder('utf8');
    console.log('Пример, в utf8 =' + utf8decoder.decode(new Uint8Array(inputKeys)));
}
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question