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

M

Matthew

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?
 
L

Lasse Reichstein Nielsen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top