Salman Virk
Salman Virk

Reputation: 12317

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 accepts Keyboard events.

Upvotes: 0

Views: 2900

Answers (1)

Moshe K.
Moshe K.

Reputation: 710

From Is it possible to simulate key press events programmatically?

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

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

  simulateKeyPress("e");
});

DEMO

Upvotes: 2

Related Questions