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

get keycode for keydown event

P: n/a
Hi,

In Javascript, the simple way to check user input is to get the keycode.
But for cross browser, its seems we need to have two different keycodes
for the same key.

There is a web page for get keycode vs event types.

http://www.w3.org/2002/09/tests/keys-cancel2.html

Try open this URL in IE and FX, input + this character. For the keydown
event, in IE is 187 but in FX is 61.

Do you have any idea without create two sets for different browsers? For
example, I created a allowed keycode array for validation, but if the
keycode for keydown event is browser relative, it is very bad and take
time to create different set for validation.

Thanks,
Ming
Dec 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
mi*********@gmail.com said the following on 12/3/2006 6:17 AM:
Hi,

In Javascript, the simple way to check user input is to get the keycode.
No, it isn't. You use the onchange event handler and validate it after
it gets changed, not while the user types.

P.S. How would you check my input if I paste it into the input using
<shift><enter>?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 3 '06 #2

P: n/a
Randy Webb <Hi************@aol.comwrote:
No, it isn't. You use the onchange event handler and validate it after
it gets changed, not while the user types.

P.S. How would you check my input if I paste it into the input using
<shift><enter>?
ok, there are two ways for user input validation. One is invoke validation
function on keydown and one validation rule is for check characters for
this field. For your question, this is second validation. In my deisgn, to
check the user input while user press keyboard, for the copy and paste, use
another function to do it. This one is triggered by on paste (IE) and on
input (FX).

Everything is working fine except the capture keycode for keydown event in
different browsers.

Cheers,
Ming
Dec 3 '06 #3

P: n/a
ASM
mi*********@gmail.com a écrit :
There is a web page for get keycode vs event types.

http://www.w3.org/2002/09/tests/keys-cancel2.html

Try open this URL in IE and FX, input + this character. For the keydown
event, in IE is 187 but in FX is 61.
Tried this page and with FF on my Mac

[+] on numeric keypad I get : keyDown = keyUp = 107

Shift -ShiftKey ok and KeyDown = 16

Shift + key [+ =] --ShiftKey ok and keyDown = keyUp = 0
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 3 '06 #4

P: n/a
Jim
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>key code alert</title>
<script type="text/javascript">
function KeyCheck(evt) {
evt = (evt) ? evt : ((event) ? event : null);
var evver = (evt.target) ? evt.target : ((evt.srcElement)
?evt.srcElement : null );
var keynumber = evt.keyCode;
if(keynumber){
alert(keynumber);
}
}
</script>
</head>
<body onkeyup = "KeyCheck(event)">
Type in the form area below to view alerts as to which key is
pressed;<br/>
for cross browser compliance, this code detects the onKeyUp event.<br/>
<form>
<textarea rows="8" cols="30" style="background-color:cyan; color:navy;
border:3px red ridge;"></textarea>
</form>
</body>
</html>

Dec 3 '06 #5

P: n/a
mi*********@gmail.com wrote:
In Javascript, the simple way to check user input is to get the
keycode. But for cross browser, its seems we need to have two
different keycodes for the same key.
Capturing keypresses is tricky, and I've yet to find a good solution that
normalizes the browser and event differences so you can just get the text of
the key pressed. It's on my to-do list, but I can't believe someone has
created such a beast yet. It doesn't exist in any of the common js
"frameworks" that I've seen, nor in any stand-alonw routines. If anyone
knows of a good solution, I'd love to see it.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 3 '06 #6

P: n/a
Matt Kruse <ne********@mattkruse.comwrote:
Capturing keypresses is tricky, and I've yet to find a good solution that
normalizes the browser and event differences so you can just get the text of
the key pressed. It's on my to-do list, but I can't believe someone has
created such a beast yet. It doesn't exist in any of the common js
"frameworks" that I've seen, nor in any stand-alonw routines. If anyone
knows of a good solution, I'd love to see it.
As you mentioned, to normalizes it for cross browser is the only
solutoin. Just a little bit worry is the same browser but different
behavior between different platform.

Cheers,
--
Ming-Chin Chen
Dec 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.