Skip to content

Instantly share code, notes, and snippets.

@erobit
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erobit/57bb3c2a617f26cd5e6e to your computer and use it in GitHub Desktop.
Save erobit/57bb3c2a617f26cd5e6e to your computer and use it in GitHub Desktop.
Input scanner
$(document).ready(function(){
function Scanner(el) {
this.robot = false;
this.count = 0;
this.tick = 0;
this.total = 0;
this.avg = 0;
this.scanThreshold = 10;
this.minChars = 25;
this.callback = null;
this.el = $('#'+el);
this.init();
}
Scanner.prototype.init = function() {
var self = this;
this.el.on('keyup', function(e){ self.scan(); });
this.el.on('focus', function(e){ self.reset(); });
this.el.on('blur', function(e){ self.reset(); });
}
Scanner.prototype.scan = function() {
var d = new Date();
var newTick = d.getTime();
var diff = newTick - this.tick;
this.count++;
if(this.el.val() == '') {
this.reset();
}
if(this.tick != 0) {
this.total += diff;
this.avg = this.total / this.count;
}
this.tick = newTick;
if(this.avg < this.scanThreshold && this.count >= this.minChars) {
this.robot = true;
this.complete();
}
//console.log('avg keystroke duration=' + this.avg);
//console.log('robot=' + this.robot);
};
Scanner.prototype.on = function(action, fn) {
// only one action currently
this.callback = fn;
}
Scanner.prototype.complete = function() {
if(this.callback != null)
this.callback();
};
Scanner.prototype.reset = function() {
this.count=this.tick=this.total=this.avg=0;
};
var scanner = new Scanner('box');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment