On 5/21/07, Richard Worth <[EMAIL PROTECTED]> wrote:

On 5/21/07, james_027 <[EMAIL PROTECTED]> wrote:
>
>
> Hi,
>
> Is there a difference between to two? Any guidelines on which one to
> use on a certion situation?
>
>
There are three javascript events that can fire when a key is pressed:

keydown
keyup
keypress

Here's a great reference for figuring out what happens on each one
depending on what key you're looking at/for:

http://www.quirksmode.org/js/keys.html

Generally keydown is needed to capture special keys (Ctrl, Alt, Caps,
Arrows) that don't fire keypress, and sometimes to prevent the normal
behavior of a keypress/combination (by returning false or stopping
propagation). Ex: prevent enter from adding a newline in a textarea or
editable div.

- Richard D. Worth


Also, when you hold down a key for repeating, keypress fires for each
repeat. So, given the following code:

$('textarea').keydown(function(){$('body').append('keydown<br>');});
$('textarea').keypress(function(){$('body').append('keypress<br>');});
$('textarea').keyup(function(){$('body').append('keyup<br>');});

if you are in a textarea and hold down the 'a' key and type: aaaaaa, you'll
get this output:

keydown
keypress
keypress
keypress
keypress
keypress
keypress
keyup

- Richard D. Worth

Reply via email to