Keypressing Not Working

  • Thread starter Dave (DreamIsle)
  • Start date
D

Dave (DreamIsle)

I can't figure out why my keypress script is not working. I used the
following code:

document.onkeypress = KeyCheck;

function KeyCheck(e) {
var KeyID = (window.event) ? event.keyCode : e.keyCode;
//alert(KeyID);
switch(KeyID) {
case 13: title(); break;
case 37: move(-1); break;
case 38: toggle(1); break;
case 39: move(1); break;
case 40: toggle(1); break;
}

}

Here's my results:
-----------------------------------------------------------------------
Firefox: | Works fine.
IE: | Enter key works but arrow keys don't trigger the function.
Opera: | No keypresses work.

When I experienced problems, I tested the keypressing with the alert
you see in the comments of the code, but aside from working in FireFox
and the initial enter keypress in IE, nothing happened.

The whole page can be seen at http://thegriddle.net/rcr/ if you need to
see any of the other functions or the HTML or CSS. Any ideas or
suggestions would be great :)
 
R

RobG

Dave said:
I can't figure out why my keypress script is not working. I used the
following code:

document.onkeypress = KeyCheck;

Try:

document.onkeyup


Tested in IE and Firefox - sorry, no Opera here:

<div id="xx"></div>
<script type="text/javascript">

document.onkeyup = keyCheck;

function keyCheck(e) {
var e = e || window.event;
document.getElementById('xx').innerHTML = e.keyCode;
}
</script>
 
D

Dave (DreamIsle)

Sorry Rob, but that isn't working for me either. Even the enter key
won't work with that.
 
R

RobG

Dave said:
Sorry Rob, but that isn't working for me either. Even the enter key
won't work with that.

Please quote what you are replying to. The code I posted 'works' in IE
and Firefox, do you mean it doesn't work in Opera?

Maybe you have other stuff that is stopping the event from bubbling,
have you tried onkeydown?
 
D

Dave (DreamIsle)

The code that you posted did not work in Internet Explorer. I have
changed the code to onkeydown and the up and down arrow keys work in IE
now, but the left and right keys do not work.

------------------------------------

document.onkeydown = KeyCheck;

function KeyCheck(e) {
var e = e || window.event;
var KeyID = e.keyCode;
//alert(KeyID);
switch(KeyID) {
case 13: title(); break;
case 37: move(-1); break;
case 38: toggle(1); break;
case 39: move(1); break;
case 40: toggle(1); break;
}
}
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top