Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I preventDefault event on keydown but not on keyup with Javascript?

When using .keydown I can capture keydown event, then check and prevent default action (display the character).

When using .keyup I cannot?

I know the event is being captured as alert() fires when the code is inside the condition yet the preventDefault() doesn't prevent the action.

Here is a full DEMO

like image 994
Aaron Avatar asked Oct 30 '13 17:10

Aaron


People also ask

What is the difference between Keyup and Keydown in JavaScript?

The keydown event occurs when the key is pressed, followed immediately by the keypress event. Then the keyup event is generated when the key is released.

Should I use Keyup or Keydown?

Both are used as per the need of your program and as per the convenience of the user. keyup Fires when the user releases a key, after the default action of that key has been performed. keydown Fires when the user depresses a key.

What is the difference between keypress and Keydown in JavaScript?

The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

What does Keyup do in JavaScript?

The keyup event occurs when a keyboard key is released. The keyup() method triggers the keyup event, or attaches a function to run when a keyup event occurs. Tip: Use the event. which property to return which key was pressed.

What is a keyDown event in JavaScript?

The keydown events happens when a key is pressed down, and then keyup – when it’s released. The key property of the event object allows to get the character, while the code property of the event object allows to get the “physical key code”. For instance, the same key Z can be pressed with or without Shift.

Why preventDefault() method will not work in Keyup event?

The preventDefault () method will not work in keyup event, because it is fired after the default event has already occurred. So listening to keyup event is too late for calling preventDefault.

What is the use of preventDefault in JavaScript?

Definition and Usage. The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Note: Not all events are cancelable. Use the cancelable property to find out if an event is cancelable.

What is the difference between keyDown and Keyup?

Keydown and keyup. The keydown events happens when a key is pressed down, and then keyup – when it’s released. The key property of the event object allows to get the character, while the code property of the event object allows to get the “physical key code”.


1 Answers

In keyup event the character has been typed and can't be undone but in keydown nothing has been typed and the browser has intent to type the character, so you can cancel the browser intent.

Whenever you type a character the following events occur:

keydown --> keypress repeatedly until the key is released --> keyup

  • keydown -> can be prevented -> fired when press a key
  • keypress -> can be prevented -> fired when hold a key
  • keyup -> cannot be prevented -> fired when release a key
like image 75
frogatto Avatar answered Oct 29 '22 08:10

frogatto