473,503 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

onkeydown

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
4 9964
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
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3887
by: Z | last post by:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want to change it to another unicode key from a...
3
7375
by: euler | last post by:
why did the keydown event not fire in this simple example? <HTML> <HEAD><title>keydown_div</title> <script type="text/javascript"> function keydown() { alert("keydown"); } </script>
10
9128
by: b.dam | last post by:
I'm trying the following: function grid() { this._el = document.createElement("TABLE"); var self = this; document.addEventListener("onkeydown", function(event) {self.gridKeyDown(event);},...
2
5917
by: Iver Erling Årva | last post by:
I have come across a problem with the onKeyDown event in some of my forms. I'm using onKeyDown in <form> as a standard method to open my help screen system throughout my system, but I have...
5
3786
by: Roger Withnell | last post by:
I'm using the following code to start a function on key down. document.onkeydown = OnKeyDown; function OnKeyDown() { vKeyCode = event.keyCode; ..code... }
0
1113
by: =?Utf-8?B?Q0dX?= | last post by:
I have a .NET 1.1 application which uses several grids of text boxs (in repeaters and datagrids) for entering arrays of time values. I use onkeydown to allow users to navigate through the grids...
0
1168
by: CGW | last post by:
I posted this in a .NET general newsgroup, but then found this group and I think it would be more appropriately posted here. Even though the problem is partially with client side behavior, I'm...
1
5726
eboyjr14
by: eboyjr14 | last post by:
I have this UserScript for Grease monkey. but I can't get the onleydown event to fire in FIREFOX only. I've looked everywhere! // ==UserScript== // @name iGoogle Suggest //...
8
4252
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.