By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.