Keyboard event Reference


The following events are triggered by the keyboard, and these events apply to all HTML5 elements:

keydown Triggered when a key is pressed

keypress Triggered when a key is pressed and released

keyup Triggered when a key is released

The following is an example of subscribing to a keyboard event. Notice that the keyboard event object has the which property that contains the char code of the key that was pressed, and you can convert that to a string as needed:

lastName.addEventListener('keypress', keyGotPressed, false);

function keyGotPressed (e){

var charCode = e.which;

var charStr = String.fromCharCode(charCode);

alert(charStr);

}
Advertisements

One thought on “Keyboard event Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s