Metal Mathematician
Metal Mathematician

Reputation: 416

Count time between characters on input

I need to determine if the input has been made by a barcode scanner and not allow any more characters to be input in the field.

Since my scanner generates a keypress, I had the idea of getting the time between the first x input characters- getting the average and if the average is a humanly impossible, it would lock the field after the last character.

I am very new to JS, so help with any step of this is appreciated.

Upvotes: 1

Views: 189

Answers (2)

JMichelB
JMichelB

Reputation: 475

Barcode scanners can be configured to have additionnal characters added before and after the scan. For example, if the code reads "5858445", the peripheral might return "@5858445/". Some configuration might also add the white space characters CR, LF or both.

Check the configuration of the hardware you are using, and, if you can, look at the manual.

Upvotes: 1

Jonas0000
Jonas0000

Reputation: 1113

Try using this solution:

document.body.onkeydown = function () {
 var time = this._time;
 var timestamp = new Date().getTime();
 if (time) console.log('time: ' + (timestamp - time));  
 this._time = timestamp;
}

Upvotes: 1

Related Questions