Skip to content

Instantly share code, notes, and snippets.

@sgreenfield
Created April 24, 2013 21:25
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 sgreenfield/5455709 to your computer and use it in GitHub Desktop.
Save sgreenfield/5455709 to your computer and use it in GitHub Desktop.
monitors keypress events and fires different event depending on whether or not the keypress actually produces a character with a value
;(function($){
//monitors keypress events and fires different event depending on whether or not the keypress actually produces a character with a value
$.fn.keylogger = function() {
return this.each(function() {
$(this).keypress(function(e) {
if ( hasValue(e) ) $(this).trigger( 'keylogger:withvalue', [String.fromCharCode(e.charCode), e] );
else $(this).trigger( 'keylogger:novalue', e );
});
});
function hasValue(e){
var code = (e.keyCode) ? e.keyCode : e.which;
return (e.charCode && code !== 32 && code !== 13 && !e.altKey && !e.ctrlKey && !e.metaKey);
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment