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

onkeydown

P: n/a
I am trying to figure out what values are returned when keys are
pressed in Mozilla & IE. Here's the code I'm using. There are no
alerts in either browser. Any clues would be greatly appreciated.
Thanks. KK

<html>
<head><title>Reading Keystrokes</title>

<script language="JavaScript">
window.onkeyDown = keyHit;
function keyHit(evt) {
if (evt && evt.which) { //NS
thisKey = evt.which;
}
else if(window.event && window.event.keyCode) { //IE
thisKey=window.event.keyCode;
}
alert(thisKey)
}
</script>
</head><body></body></html>
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The method I used to get your script to work is this. First I put the
'onKeyDown' handler in the <body> tag. Second inside the keyHit() function
I made the first line--

var evt = window.event;

--the code works in this format in IE, though I don't know about any cross
browser application.

Do you know anything about how to disable the Backspace Key from
performing the history.back method? I can disable it for text but not for
browsing.

Jul 23 '05 #2

P: n/a
KublaiKhan wrote:
I am trying to figure out what values are returned when keys are
pressed in Mozilla & IE. Here's the code I'm using. There are no
alerts in either browser. Any clues would be greatly appreciated.
Thanks. KK

<html>
<head><title>Reading Keystrokes</title>

<script language="JavaScript">
<script type="text/javascript">
window.onkeyDown = keyHit;
document.onkeydown = keyHit;
function keyHit(evt) {
if (evt && evt.which) { //NS
thisKey = evt.which;
}
else if(window.event && window.event.keyCode) { //IE
thisKey=window.event.keyCode;
}
alert(thisKey)
}
function keyHit(evt) {
evt = evt || window.event;
alert(evt.which ? evt.which : evt.keyCode);
}
</script>
</head><body></body></html>


--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3

P: n/a

"KublaiKhan" <bi*****@hotmail.com> wrote in message
news:f0**************************@posting.google.c om...
I am trying to figure out what values are returned when keys are
pressed in Mozilla & IE. Here's the code I'm using. There are no
alerts in either browser. Any clues would be greatly appreciated.
Thanks. KK

<html>
<head><title>Reading Keystrokes</title>

<script language="JavaScript">
<script type="text/javascript">
window.onkeyDown = keyHit;
document.onkeydown = keyHit;
if you just want general keyboard response use the
onkeypress handler. It handles upper/lower case
numbers and punctuation. Onkeydown requires a solid
understanding of handling key events.
function keyHit(evt) { if (evt && evt.which) { //NS
// you haven't declared thisKey and it will
// be available as a global variable to the rest of
// your script/s !!
var thisKey; // now local variable
thisKey = evt.which; }
else if(window.event && window.event.keyCode) { //IE
thisKey=window.event.keyCode;
}
alert(thisKey)
}
</script>
</head><body></body></html>


<script type="text/javascript">
document.onkeypress = keyHit;

function keyHit(evt) {
var e = evt || window.event;
var thisKey = e.which || e.keyCode;
alert(String.fromCharCode(thisKey));
}
</script>
HTH
Jimbo
Jul 23 '05 #4

P: n/a
"koethler" <ko******@telus.net> wrote in message news:<45******************************@localhost.t alkaboutprogramming.com>...
The method I used to get your script to work is this. First I put the
'onKeyDown' handler in the <body> tag. Second inside the keyHit() function
I made the first line--

var evt = window.event;

--the code works in this format in IE, though I don't know about any cross
browser application.

Do you know anything about how to disable the Backspace Key from
performing the history.back method? I can disable it for text but not for
browsing.


I simplified the code on Grant Wagner's advice to:
<html>
<head><title>Reading Keystrokes</title>

<script type="text/javascript">
document.onkeydown = keyHit;
function keyHit(evt) {
evt = evt || window.event;
alert(evt.which ? evt.which : evt.keyCode);
}
</script>
</head><body></body></html>

Turns out my original problem was I had document.onkeyDown instead of
document.onkeydown (lowercase). Now it works fine under Mozilla & IE6.
Thanks folks. I am very new js. With that in mind, I have pages
1,2,3,4. Link on p. 1 takes you p. 2 which checks for cookies & if
false, does a
this.location.href="page3.html"
P. 3 first line is
<meta http-equiv=refresh content="0;url=page4.html">
A link in page 4 then takes you to p. 2. Backward navigation from p.2
now takes you to p.1 with pages 3 & 4 erased from history.
Something to think about.

KK
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.