Home >Web Front-end >JS Tutorial >Keyboard events of jQuery events (ctrl Enter key to submit the form, etc.)_jquery
Keyboard events handle all user keystrokes, whether inside or outside the text input area. The scope of keyboard events is different in different browsers. Generally, this kind of keyboard events can act on elements such as Form elements, a tag elements, window, and document. Keyboard events can be triggered on all elements that can obtain intersection points. Elements that can obtain focus can be understood in this way. Elements that can be jumped to when using the Tab key are elements that can use keyboard events (if tabindex is not set for these elements) In the case of attribute values, when tabindex is set to a negative number, focus will not be obtained when using the Tab key).
Keyboard events can pass a parameter event. In fact, all jQuery events can pass such a parameter. This event is an object, which includes some properties. When the event is triggered, you can use the event to get some information about the event. For example, when using the keyboard, you can use event.keyCode to obtain the ASCII code value of the pressed key. See below
1: The keydown() event is the first keyboard event triggered when the keyboard is clicked. If the user continues to hold down the key, the keydown event will continue.
3: The keyup() event is the last event to occur (after the keydown event). Unlike the keydown event, this event is only triggered once when the keyboard is released (because releasing the keyboard is not a continuous state).
The code is as follows:
keydown();
keyup();keypress();
keydown()
The keydown event will be triggered when the keyboard is pressed. You can return false in the bound function to prevent the browser's default event from being triggered.
keyup()
The keyup event will be triggered when the key is released, that is, the event after you press the keyboard and get up
keypress()
The keypress event is triggered when a key is tapped. We can understand it as pressing and lifting the same key
How can we get whether I pressed the A, Z or Enter button?
Keyboard events can pass a parameter event. In fact, some jQuery event functions can pass such a parameter
In the above code, event.keyCode can help us get what key we pressed. It returns the ascII code, such as the up, down, left and right keys, which are 38, 40, 37, 39 respectively
If we want to implement ctrl Enter, it is ctrl Enter to submit the form
Other reference information:
Preliminary knowledge
1. Number 0 key value 48.. Number 9 key value 57
2. a key value 97.. z key value 122; A key value 65.. Z key value 90
3. Key value 43;-Key value 45;.Key value 46;Backspace 8;Tab key value 9;
4.event is global in IE, and is a temporary object in Firefox, and parameters need to be passed