SiteMap  | About    

Keyboard Events

KeyboardEvent objects describe a user interaction with the keyboard ; each event describes a single interaction between the user and a key on the keyboard. The event type (keydown, keypress, or keyup) identifies what kind of keyboard activity occurred. When a user presses a key, jQuery determines which event to raise based on whether the keyboard message specifies a character key or a physical key . It is important to note that, keyboard events may not be fired if the user is using an alternate means of entering text, such as a handwriting system on a tablet or graphics tablet.

The main keyboard events you need to understand are:

  1. keydown
  2. keypress
  3. keyup

keydown event

The keydown event is triggered as soon as a user presses a key down. This event doesn't require the user to finish the keypress in order to be triggered. In fact, in many cases, if the user holds down the key, the keydown event will be triggered continuously.

run this source code Try it Yourself

Full Source

keypress event

The keypress event is normally triggered when a key on the keyboard is being pressed down. The method keypress() attaches an event handler which is triggered when the key is being pressed down, or triggers the keypress event. When modifier keys are pressed (shift, ctrl, alt, etc.), the event will not fire. The modifier keys cause a keydown event , but not a keypress event.

run this source code Try it Yourself

Full Source

keyup event

The jQuery method of keyup() corresponds the keyup event, it is triggered when the user releases an key. It will not fire until the key is released. The difference between this one and the others (keypress and keydown) is that when a key is held down, this event will not fire until the key is released. The other keyboard events (keydown and keypress ) continue to fire as long as the key is held down.

run this source code Try it Yourself

Full Source

Determine which key was pressed

The event.which property normalizes event.keyCode and event.charCode . It is recommended to watch event.which for keyboard key input. So, below is simple jQuery code to determine or find out which key was pressed. We are using keypress() event of jQuery to detect which key was pressed.

Listen for the enter key

The enter key is represent by code "13" in ASCII. To check if an "enter" key is pressed inside a textbox, just bind the keypress() to the textbox. (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.