Ok so what is the difference in .keypress and .keydown/.keyup? At present I am using .keydown which returns a .which value of 38 for my key, now if i change it to .keypress it returns a value of 109 for that same key. What is the difference and why are the values different for the same key?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
In normal cases, go for
keyup
:Reasons:
keydown
keeps firing when user holds the keys down, whilekeypress
andkeyup
fire only once.keypress
doesn't detect special keys (e.g.SHIFT
),keydown
andkeyup
do.I'll be d$%^@d, there really is a difference with keypress and all this time I never realized. lol
See my fiddle and try something like the letter "r"
http://jsfiddle.net/SpYk3/NePCm/
Somehow I never paid attention to this
KeyPress happens after KeyDown. So you can use KeyDown to determine what key it is, then KeyPress to disallow that character.
If you press a button it fires a
keydown
and releasing it fires akeyup
. Thekeypress
usually comes between those two.keydown
andkeyup
talk about which key has been changed.keypress
tells which character that key represents.Note that this is all browser-dependent!
See this article about the differences between the key events as implemented on various browsers.
You should read the following post : http://javascript.info/tutorial/keyboard-events