468,317 Members | 1,521 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,317 developers. It's quick & easy.

Trying to figure out how to add a second KeyCode to an onkeydown event

I have this code snippet that is ignoring a Tab key and executing
something(); on all other key-presses. How would I efficiently modify
this to include a second key, such as backspace (KeyCode 8)?

onkeydown="if ((event.keyCode||event.which) != 9) { something(); }"

Also, not a big deal -- but how would I also factor a "Shift-Tab" into
the mix? I know Tab is a KeyCode 9, but how do you express the
Shift(16) into that?
Jul 20 '05 #1
1 1533
ma******@yahoo.com (Matthew) writes:
I have this code snippet that is ignoring a Tab key and executing
something(); on all other key-presses. How would I efficiently modify
this to include a second key, such as backspace (KeyCode 8)?

onkeydown="if ((event.keyCode||event.which) != 9) { something(); }"
If you need to test more than just one key, you should name the large
expression:
onkeydown="var theKey=event.keyCode||event.which;
if (theKey == 8 || theKey == 9) {something();}"

(newlines are allowed inside attributes, but if it gets much bigger,
it should be moved to a separate function).
Also, not a big deal -- but how would I also factor a "Shift-Tab" into
the mix? I know Tab is a KeyCode 9, but how do you express the
Shift(16) into that?


See other post :)
The event has a "shiftKey" property that is true if shift is pressed
while the other key is activated. Ditto for ctrlKey and altKey.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by oeyvind toft | last post: by
6 posts views Thread by Z | last post: by
17 posts views Thread by Julia Briggs | last post: by
6 posts views Thread by rich_poppleton | last post: by
6 posts views Thread by Stefan Mueller | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.