- Posted On 20 November 2014
- By
- In Programming
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; clearTimeout(timer); 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 $(".nextcontrol").focus(); return false; event.stopPropagation(); } }); $(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'); } else { $("#msg").html("<span style='color:red'>Its Manually Typed!</span>"); //alert('Its Manual Entry'); } keyupFiredCount = 0; }); }; try { $(".barcodeinput").ConvertToBarcodeTextbox(); if ($(".barcodeinput").val() == '') $(".barcodeinput").focus(); else $(".nextcontrol").focus(); } 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.
- Tags :
- ASP.NET
- MVC
- jQueryJavascript
Top 10 Visual Studio things which can boost developers coding speed
Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.
Visual Studio 2008 Shell and TFS integration
Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
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.
Best CSS Gradient background generator tools online
Here are some best CSS gradient background code generator online tools using which you can create a cross browser css code for gradient backgrounds.