Skip to content Skip to sidebar Skip to footer

In Javascript, How Can I Create My Own Keyboard Event?

In JavaScript, how can I create my own event that I can dispatch to another function. For example, I want to create Enter Key press eventso I may dispatch to another function that

Solution 1:

From Is it possible to simulate key press events programmatically?

functionsimulateKeyPress(character) {
  jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}

$(function() {
  $('body').keypress(function(e) {
    alert(e.which);
  });

  simulateKeyPress("e");
});

DEMO

Post a Comment for "In Javascript, How Can I Create My Own Keyboard Event?"