keypress
Событие keypress происходит когда нажимается символьная клавиша, то есть клавиша которая создаёт символ. Пример таких клавиш это буквы, цифры, знаки пунктуации и т.д. Примеры клавиш которые не создают символы, это клавиши модификаторы, такие как: Alt, Shift, Ctrl, or Meta.
Общая информация
- Спецификация
-
DOM L3 Устарело
- Интерфейс
- Bubbles
-
Yes
- Cancelable
-
Yes
- Цель
-
Document, Element
- Действие по умолчанию
-
Varies:
keypressevent; launch text composition system;blurandfocusevents;DOMActivateevent; other event
Свойства
| Свойства | Тип | Описание |
|---|---|---|
target Только для чтения | EventTarget | Цель события (самая верхняя цель в дереве DOM). |
type Только для чтения | DOMString | Тип события. |
bubbles Только для чтения | Boolean | Whether the event normally bubbles or not |
cancelable Только для чтения | Boolean | Отменяется ли событие или нет |
view Только для чтения | WindowProxy | document.defaultView (window of the document) |
detail Только для чтения | long (float) | 0. |
target Только для чтения | EventTarget (DOM element) | Сосредоточенный элемент, обрабатывающий ключевое событие, корневой элемент, если не выделен подходящий элемент ввода. |
char Только для чтения | DOMString (string) | The character value of the key. If the key corresponds to a printable character, this value is a non-empty Unicode string containing that character. If the key doesn't have a printable representation, this is an empty string. See key names and char values for the detail. Примечание: Если ключ используется в качестве макроса, который вставляет несколько символов, значением этого атрибута является вся строка, а не только первый символ. |
key Только для чтения | DOMString (string) | The key value of the key represented by the event. If the value has a printed representation, this attribute's value is the same as the |
code Только для чтения | DOMString (string) | Holds a string that identifies the physical key being pressed. The value is not affected by the current keyboard layout or modifier state, so a particular key will always return the same value. |
charCode Только для чтения | Unsigned long (int) | The Unicode reference number of the key; this attribute is used only by the keypress event. For keys whose char attribute contains multiple characters, this is the Unicode value of the first character in that attribute. Warning: This attribute is deprecated; you should use char instead, if available. |
keyCode Только для чтения | Unsigned long (int) | A system and implementation dependent numerical code identifying the unmodified value of the pressed key. This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key; see Virtual key codes for a list of common values. If the key can't be identified, this value is 0. Warning: This attribute is deprecated; you should use key instead, if available. |
which Только для чтения | Unsigned long (int) | A system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as keyCode. Warning: This attribute is deprecated; you should use key instead, if available. |
location Только для чтения | long (float) | The location of the key on the device. |
repeat Только для чтения | boolean | true if a key has been depressed long enough to trigger key repetition, otherwise false. |
locale Только для чтения | string | The language code for the key event, if available; otherwise, the empty string. |
ctrlKey Только для чтения | boolean | true if the control key was down when the event was fired. false otherwise. |
shiftKey Только для чтения | boolean | true if the shift key was down when the event was fired. false otherwise. |
altKey Только для чтения | boolean | true if the alt key was down when the event was fired. false otherwise. |
metaKey Только для чтения | boolean | true if the meta key was down when the event was fired. false otherwise. |
Примечания
Chrome не запускает событие keypress для известных сочетаний клавиш (reference). Какие сочетания клавиш известны, зависит от системы пользователя. Используйте событие keydown для реализации сочетаний клавиш.
Связанные события
Пример
<!doctype html> <html> <head> <script> "use strict"; document.addEventListener("keypress", (event) => { const keyName = event.key; alert("keypress event\n\n" + "key: " + keyName); }); </script> </head> <body></body> </html>