Help me how to block user keyboard input but allow barcode input

3,343 views
Skip to first unread message

Prakash Ranganthan

unread,
Jul 22, 2012, 11:43:23 PM7/22/12
to greasemon...@googlegroups.com
Hi friends i tried blocking keyboard input but it also blocks barcode input any idea how to proceed with blocking only keyboard input in textbox and allowing barcode input. 

var barcode = document.getElementById('userid');
barcode.addEventListener("keypress", function() { alert("Please use Barcode Scanner!"); document.getElementById('userid').value = "";}, true);   

Brian L. Matthews

unread,
Jul 23, 2012, 1:11:34 AM7/23/12
to greasemon...@googlegroups.com
The barcode readers I've used just generate keystrokes, so as far as the
browser's concerned, it's all keystrokes. However, the keystrokes from
the reader usually come quickly and consistently, so maybe you could
measure the time between keystrokes and differentiate that way.

Of course whatever reader you're using may work differently.

Brian

Prakash Ranganthan

unread,
Jul 23, 2012, 6:12:54 AM7/23/12
to greasemon...@googlegroups.com
Thanks for the suggestion I too thought that but I don't have an idea about how to proceed. can you help me 

Sam Larison

unread,
Jul 23, 2012, 7:16:41 AM7/23/12
to greasemon...@googlegroups.com
Not tested this at all, I bet there is a bug or two... not sure why you need GM though

var lastKeyTime=new Date();
var low_threshold = 10;    //assuming milliseconds, keys required to be input with this much or less time between them
var high_threshold = 250;  //assuming  milliseconds between barcode reads, to reset the system if they wait long enough

<input id="myfield" type="text" onkeyup="return barcodeonkeyup(event)" />
In your keyup listener (use document.getElementById('myfield').addEventListener('keyup',barcodeonkeyup,false);

function barcodeonkeyup(ev){
  var keytimediference=new Date() - lastKeyTime;
  if(keytimediference  >  low_threshold  && keytimediference  < high_threshold ){
       // must be a keyboard input, clear your text field
       // document.getElementById('myfield').value="";
       ev.preventDefault()
       return false;
  }
  lastKeyTime=new Date();
}


--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/greasemonkey-users/-/YMrxvLyu8qgJ.

To post to this group, send email to greasemon...@googlegroups.com.
To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.

Brian L. Matthews

unread,
Jul 23, 2012, 11:18:43 AM7/23/12
to greasemon...@googlegroups.com
That will probably always drop the first keystroke, even from the barcode reader. That's easily fixable though by initializing lastKeyTime to -1 and testing for that in badcodeonkeyup.

You could also check after the first keystroke by setting a timeout on the first keystroke, so if you don't get the second within the high threshold, it clears the field value and alerts the user (or however you want to handle it).

However, doing this does mean if the barcode reader is broken or not connected or the driver isn't working or whatever, no one will be able to log in. Personally I'd let the user type their userid as a fallback, but I don't know the use case at all, so maybe that doesn't make sense.

And I assume the original poster needs GM because he can't modify the page, although if that's not the case, it would be better to just modify the page to have this logic rather than using GM.

Brian

Klaus Johannes Rusch

unread,
Jul 23, 2012, 1:29:13 AM7/23/12
to greasemon...@googlegroups.com
On 23.07.2012 07:11, Brian L. Matthews wrote:
> On 7/22/12 8:43 PM, Prakash Ranganthan wrote:
>> Hi friends i tried blocking keyboard input but it also blocks barcode
>> input any idea how to proceed with blocking only keyboard input in
>> textbox and allowing barcode input.
> The barcode readers I've used just generate keystrokes, so as far as
> the browser's concerned, it's all keystrokes. However, the keystrokes
> from the reader usually come quickly and consistently, so maybe you
> could measure the time between keystrokes and differentiate that way.
Why would you want to block the keyboard though? Not only is the
mechanism of sending the digits the same (emulating keyboard), they
keyboard also serves as an alternative when a barcode cannot be read by
the scanner.

--
Klaus Johannes Rusch
klaus...@atmedia.net
http://klausrusch.atmedia.net/

Sam Larison

unread,
Jul 24, 2012, 6:29:38 PM7/24/12
to greasemon...@googlegroups.com
It shouldn't drop the first keystroke because keytimediference will be larger than high_threshold for the first keystroke.

I would set the timeout in the else condition for the if statement, if the document.getElementById('myfield').value.length is less than the expected barcode length, clear the field.

I was also thinking that perhaps it's suppose to be keydown instead of keyup.

However at the store they sometimes have to key in the barcode don't they?

This feature seems a lot like a duck!  Hopefully there isn't any financial profit in this feature because if there is I want a slice!  

If you don't clear the field then they can type really slowly and enter a number still.

Something to keep in mind might be to call document.getElementById('myfield').select() any time the field is to be cleared by the next keystroke or barcode.

rhlounsbury

unread,
Jul 26, 2012, 11:05:38 PM7/26/12
to greasemon...@googlegroups.com
Most barcode scanners these days have a USB interface.  By default, they operate as keyboard wedge scanners... that is, scanner data appears to have been typed at the keyboard.  You can't really tell the two sources apart.  But your scanner manual probably includes a configuration section where you can scan special programming barcodes to change settings.  Here are two possible solutions:

1.  Configure the scanner to appear on a serial port.  You will need to handle input from this serial port separately from the keyboard (more coding) but you can keep the two streams absolutely separate.  This is the method I use in my industrial systems.

2.  Configure the scanner to automatically append (or prepend) a particular character to the barcode data.  For example, always insert a tilde ~ right before the enter code at the end of the barcode.  Any time you get data, check the last character.  If it's a tilde (or whatever you select), remove it and treat the data as though it came from the scanner.  If there is no tilde and you don't want the operator to be typing right now, just ignore the input.

Bob
Reply all
Reply to author
Forward
0 new messages