Repetitive "onkeydown" and "onkeyup" while holding down SHIFT

willwang
willwang used Ask the Experts™
on
Hi All,

I'm trying to detect when a user holds down a shift key and lets go.  I trapped the "onkeydown" and "onkeyup" events, but when I hold down to the SHIFT key, the up and down events trigger repetitively and won't stop.  How can I write code that only detects the INITIAL key down, and FINAL key up?

In case you're confused, try holding down on a letter on your keyboard, and your computer will reptitively type that single character.  The same response is happening with the SHIFT key and I want to disable to 'repetitive' event triggering.

I'm on IE6.0

Thanks for any advice.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
thats what the on_keypress is for :)


-Brian

Author

Commented:
on key press doesn't work for modifier keys (such as CTRL, ALT, SHIFT).  Maybe I'm wrong, but onkeypress doesn't seem to work for me.

Tried it.

Author

Commented:
From what I understand, onkeypress only triggers when a non-modifier key is pressed.  One can still detect if a modifier key was used in the process, but it won't trigger unless I hit "A" or something.

Someone please correct me if I'm wrong.

Thanks!
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
For example CTRL-A will trigger OnKeyPress, but CTRL but itself would not.  PLEASE correct me if i'm wrong.

Author

Commented:
DOH.  I used the same handler function for both key down and up, hence that is what screwed me over.

Author

Commented:
I figured out this question myself.
<html>
<head>
      <title>huhuu</title>
      <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script>
var usa="none"
function kuku()
{
      if(event.keyCode==16) alert("you press shift")
      if(event.keyCode==17) alert("you press ctrl")
      if(event.keyCode==18) alert("you press alt")
}
</script>

</head>

<body onKeyDown="kuku()" bgcolor=ffffff marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 background="">



</body>
</html>
<body onKeyDown="return false;">
How did u achieve the result u wanted?

I am curious as i wanted to trap the arrow keys similarly. But they all seem to have keycode of zero.

Did the above program work for u?

post as soon as possible.

regards
hari
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
Commented:
PAQed, with points refunded (50)

modulo
Community Support Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial