Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


Detect input is from barcode scanner or manual typing using jQuery JavaScript

Share with WhatsApp

In this post I am sharing how you can detect whether input is scanned or manually typed using jQuery and JavaScript.

In one of the project I had worked there was a requirement where I need to detect whether input is coming from scanner or user manually typing through keyboard. I had searched on Google to know whether there is some direct way by which we can identify the scanner but at the end came to the conclusion that you can only achieve it by using timer or something which will observe at what speed input is coming and accordingly you can decide whether it is a scanner or user is manually typing.

While searching I had found one link where that blogger has shared a code by which you can hold a function to be executed which I had used to achieve this functionality. (Not having the link to share, but thanks to that blogger).

If you are working on such functionality then definitely you will able to understand the code as it’s pretty simple.

The core logic is we are identifying the time between two characters entered. When input is from scanner, it’s very fast but when user is typing he spends a little time between two characters while typing and we are observing that time and incrementing our count on every such delay and at the end if this count greater than 1 then I have concluded that its manual entry. Delay is in milliseconds which I have kept 300 for now but you can adjust it as per your requirement.

This code will require some manipulation to handle copy paste and reedit operation which I am keeping for users to achieve. :-)

Here is the JavaScript jQuery code.

$(document).ready(function () {
    var keyupFiredCount = 0; 

    function DelayExecution(f, delay) {
        var timer = null; 
        return function () {
            var context = this, args = arguments;
            timer = window.setTimeout(function () {
                f.apply(context, args);
            delay || 300);
    $.fn.ConvertToBarcodeTextbox = function () {

        $(this).focus(function () { $(this).select();$("#msg").html(""); });

         $(this).keydown(function (event) {
            var keycode = (event.keyCode ? event.keyCode : event.which); 
            if ($(this).val() == '') {
                keyupFiredCount = 0; 
            if (keycode == 13) {//enter key 
                    return false;

        $(this).keyup(DelayExecution(function (event) {
            var keycode = (event.keyCode ? event.keyCode : event.which);  
                keyupFiredCount = keyupFiredCount + 1;  

         $(this).blur(function (event) { 
             if ($(this).val() == '')
                 return false;

             if(keyupFiredCount <= 1)
                 $("#msg").html("<span style='color:green'>Its Scanner!</span>");
                 //alert('Its Scanner'); 
                 $("#msg").html("<span style='color:red'>Its Manually Typed!</span>");
                 //alert('Its Manual Entry');

             keyupFiredCount = 0;
    try {
        if ($(".barcodeinput").val() == '')
    } catch (e) { }


Below is the HTML

Enter/Scan Barcode : <input type="text" id="txtCode" class="barcodeinput"/>
<input type="button" value="OK" class="nextcontrol"/>
<h3 id="msg"/>

You can test the behavior in live fiddle below.

Let me know your thoughts on it and do share it with your friends if you find it useful. Thanks.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!