.keydown( handler )Возвращает: jQuery

Описание: Устанавливает обработчик перехода клавиши клавиатуры в нажатое состояние, либо, запускает это событие.

This method is a shortcut for .on( "keydown", handler ) in the first and second variations, and .trigger( "keydown" ) in the third.

The keydown event is sent to an element when the user presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

For example, consider the HTML:

<input id="target" type="text" value="Hello there">
<div id="other">
Trigger the handler

The event handler can be bound to the input field:

$( "#target" ).keydown(function() {
alert( "Handler for .keydown() called." );

Now when the insertion point is inside the field, pressing a key displays the alert:

Handler for .keydown() called.

To trigger the event manually, apply .keydown() without an argument:

$( "#other" ).click(function() {
$( "#target" ).keydown();

After this code executes, clicks on Trigger the handler will also alert the message.

If key presses anywhere need to be caught (for example, to implement global shortcut keys on a page), it is useful to attach this behavior to the document object. Because of event bubbling, all key presses will make their way up the DOM to the document object unless explicitly stopped.

To determine which key was pressed, examine the event object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the key code. This code corresponds to a key on the keyboard, including codes for special keys such as arrows. For catching actual text entry, .keypress() may be a better choice.

Дополнительные замечания:

  • As the .keydown() method is just a shorthand for .on( "keydown", handler ), detaching is possible using .off( "keydown" ).

Примеры использования