I know that you can pass additional arguments when using trigger() , but I'm not sure if you can overwrite the properties of the event object. You can transfer such data, though:
$(document).on('keydown keyup keypress', function (event, characterCode) { if (typeof(characterCode) == 'undefined') { characterCode = -1; } console.log('type = ' + event.type); console.log('characterCode = ' + characterCode); });
And this will be the trigger() code:
$(document).trigger('keydown', [ "0".charCodeAt(0) ]);
Here is a demo: http://jsfiddle.net/A7cEE/ (see log console)
The code from the stackoverflow answer you provided works fine:
Capture:
$(document).on('keydown', function (event) { console.log('type = ' + event.type); console.log('keyCode = ' + event.keyCode); });
To Trigger:
var e = jQuery.Event('keydown'); e.keyCode = "0".charCodeAt(0); $(document).trigger(e);
Demo: http://jsfiddle.net/A7cEE/1/
Note that .on() is new in jQuery 1.7 and in this case is the same as .bind() .
source share