473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 9943
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.