Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

I want to scan barcodes with an external barcode scanner

I want to scan barcodes with an external barcode scanner. I already installed https://github.com/milon/barcode. What steps can i take next? NOTE: I am not trying to generate barcodes

0 likes
5 replies
Tray2's avatar

The barcode scanner acts like a keyboard in most cases so most likely all you need to do is set focus on the input field and then scan the value. Then you have something called post amble and that is most likely an enter key. so the form need to have submit on enter. Then you can handle the submit with javascript. So it depends a bit on what kind of barcode scanner you are using.

tomasosho's avatar

let use this example

<form action="/submitbarcode" method="post">
    <label for="Barcode">barcode</label>
    <input type="text" name="barcode" autofocus>


</form>

<script>
var BarcodeScanerEvents = function() {
     this.initialize.apply(this, arguments);
};

BarcodeScanerEvents.prototype = {
    initialize: function() {
       $(document).on({
          keyup: $.proxy(this._keyup, this)
       });
    },
    _timeoutHandler: 0,
    _inputString: '',
    _keyup: function (e) {
        if (this._timeoutHandler) {
            clearTimeout(this._timeoutHandler);
            this._inputString += String.fromCharCode(e.which);
        } 

        this._timeoutHandler = setTimeout($.proxy(function () {
            if (this._inputString.length <= 3) {
                this._inputString = '';
                return;
            }

            $(document).trigger('onbarcodescaned', this._inputString);

            this._inputString = '';

        }, this), 20);
    }
};
</script>

How do i implement the JS in the form input method?

Please or to participate in this conversation.