Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't keypress handle the delete key and the backspace key

I'm not asking this because I need a work-a-around. I have one that works fine, but I want to know WHY it doesn't. Is this bug in Javascript (or JQuery because I was using the JQuery .keypress handler) or is there a specific reason why this is so?

like image 957
aarona Avatar asked Oct 12 '10 03:10

aarona


People also ask

How do I use Backspace as Delete key?

Holding down the backspace key deletes text until there's no more text to the left or above the cursor or you let go of the key. People often hold down the backspace key to delete a lot of text. However, it's faster to select all the text and then press the delete key once.

Does Backspace count as keypress?

Once you get the key of the pressed button, then match it with the Backspace key code. A Backspace key keycode it 8. Use keydown instead of keypress . The problem with backspace probably is, that the browser will navigate back on keyup and thus your page will not see the keypress event.

What will be the result if the delete Backspace key is pressed?

Pressing Backspace deletes text to the left (backwards) of the cursor. For example, if you click at the end of the example text below and press delete, nothing happens because there's no text to the right. However, if you press the Backspace key, the end of the text would begin to be deleted.


1 Answers

The keypress event is designed for handling a character typed by the user rather than detecting keyboard activity and the delete and backspace keys do not generate characters. Some browsers blur this line somewhat but the general principle is that the keyup and keydown events are there to detect any key being pressed and telling you about which key it is while keypress is for detecting an actual character being typed.

like image 85
Tim Down Avatar answered Oct 13 '22 22:10

Tim Down