...

View Full Version : ev.keyCode || ev.which?



Kirl
08-20-2007, 11:34 PM
The bit of code below works in Opera, but IE generates an error. I'm trying to get this to work in both (ideally all) browsers.




document.onkeypress = function(ev)
{
var key =(ev.which||ev.keyCode);



I tried changing it to this (below) to check if it either is supported (like I've seen it done) but this didn't work. Any ideas?


document.onkeypress = function(ev)
{
if(ev.which)
key = ev.which;
else if(ev.keyCode)
key = ev.keyCode;

rwedge
08-21-2007, 02:01 AM
This will work for opera, ie, and firefox
<script type="text/javascript">
document.onkeypress = function(ev)
{
var key = (document.all)? event.keyCode:ev.charCode;
alert(key);
}
</script>

Kirl
08-21-2007, 12:58 PM
Great, thanks a lot!

Can you explain what is going on exactly?


[edit] Hmmm, it seems the arrow key's aren't being detected, any idea how I can read them?

Mr J
08-21-2007, 01:54 PM
You could also try it this way and check for the event instead of the browser

<script type="text/javascript">
document.onkeypress = function(ev){
var key = (!ev? event.keyCode:ev.which);
alert(key);
}
</script>

On a side note, to detect the arrow keys you may have to use onkeydown

<script type="text/javascript">
document.onkeydown = function(ev){
var key = (!ev? event.keyCode:ev.which);

//alert(key);

switch (key) {
case 37: alert("LEFT");
break;
case 38: alert("UP");
break;
case 39: alert("RIGHT");
break;
case 40: alert("DOWN");
break;
}

}
</script>

liorean
08-21-2007, 04:20 PM
document.onkeydown = function(ev){
var key = (!ev? event.keyCode:ev.which);
}To my knowledge, eventObject.which is the nn4 way and really shouldn't be used for any other browser.

For modern browsers this makes more sense:

document.onkeydown = function(ev){
var
key;
ev = ev || event;
key = ev.keyCode;
}

Kirl
08-21-2007, 10:43 PM
Thank you all, it works!

Anywhere I can find the exact functional difference between onkeydown and onkeypress?

I guess one is for holding and another for a single press, but their behaviour doesn't always seem to work like that.

liorean
08-21-2007, 11:37 PM
Anywhere I can find the exact functional difference between onkeydown and onkeypress?Sadly, nowhere. The keypress event is not standard, it's just ubiquitous. To add to the problem, it doesn't work the same in different browsers.

I guess one is for holding and another for a single press, but their behaviour doesn't always seem to work like that.See my explanation of them in 376469. However, the best way is to try things out in each browser and handle things accordingly. I posted a link in that post to a good page for examining the browser behaviours.

felgall
08-22-2007, 07:57 AM
This will work for opera, ie, and firefox
<script type="text/javascript">
document.onkeypress = function(ev)
{
var key = (document.all)? event.keyCode:ev.charCode;
alert(key);
}
</script>


That code will not work in Opera as Opera expects ev.charCode and not event.keyCode and recognises document.all

When using feature sensing in Javascript to see if feature X is supported don't test for if D is supported and assume that because D then X as well because that isn't always true.

The only references to document.all should read


if (document.all && !document.getElementById) alert ('IE4');

to set a value to the current event use:


ev = ev || event;

which grabs the window.event object for IE which passes events that way rather than as a parameter.

The difference between the keydown and keypress events is that keydown detects that a key has been pressed but the code to identify which key has not yet been passed so that event doesn't know which key was pressed. The keypress event is triggered after the keycode is passed back from the key.

rwedge
08-22-2007, 11:45 AM
That code will not work in Opera
Does for me with Opera 9, I checked it before I posted

The only references to document.all should read

if (document.all && !document.getElementById) alert ('IE4');
Only seems a little strong

Kirl
08-22-2007, 04:50 PM
Thanks for the link liorean, that test page will be very usefull!
Thank you for the explanation, felgal, very enlightening!

However as rwedge pointed out, keyCode does seem to work in Opera as well.

A weird thing I just re-discovered is that onkeydown events can't cancel default behaviour ("preventDefault()" or "return false"). Should I include a seperate onkeypress function to return false from, or is there a more elegant solution to prevent the default behaviour of specific keys from within an onkeydown?

liorean
08-22-2007, 04:50 PM
Does for me with Opera 9, I checked it before I postedOpera is a moving target. There's several things that work considerably different between Opera 9.0 and Opera 9.23. So you may very well both be right.
Only seems a little strongNot at all. There are severely bad compatibility for doing tests of document.all. One example is that moz has a quirk where you can detect docuemnt.all as if it existed but if you try to use it for anything, you find it doesn't work. Opera's implementation works for the basic uses of it, but not the advanced stuff. And Konqueror and Safari have their own buggy quirks for it too.

All in all any use of document.all at all is bad practice and should be avoided for all practice where ie4w compatibility is not strictly needed.

Kirl
08-23-2007, 12:37 PM
Any ideas how I can cancel specific key events from within an onkeydown?

I can't get onkeyPRESS to detect keycodes in all browsers, and onkeyDOWN doesn't seem to cancel the default behaviour of the arrow keys.

Any ideas how I might solve this perdicament?

BubikolRamios
11-22-2008, 07:43 AM
This does nothing on arrow keys on IE (6 at least), how to solve that ?


document.keypress = function(ev)
{
var key;
ev = ev || event;
key = ev.keyCode;
alert(key)
}

BubikolRamios
11-22-2008, 07:48 AM
found the answer on this forum:

In IE, arrow keys don't fire a keypress event, only onkeydown and onkeyup



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum