Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts

    ev.keyCode || ev.which?

    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.

    Code:
    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?

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

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    This will work for opera, ie, and firefox
    Code:
    <script type="text/javascript">
    document.onkeypress = function(ev)
    {	
       var key = (document.all)? event.keyCode:ev.charCode;
       alert(key);
    }
    </script>

  • The Following 2 Users Say Thank You to rwedge For This Useful Post:

    Kirl (08-21-2007), Philip M (08-21-2007)

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    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?
    Last edited by Kirl; 08-21-2007 at 01:21 PM.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>
    Last edited by Mr J; 08-21-2007 at 02:11 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • Users who have thanked Mr J for this post:

    Kirl (08-21-2007)

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    document.onkeydown = function(ev){	
        var
            key;
        ev = ev || event;
        key = ev.keyCode;
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • The Following 2 Users Say Thank You to liorean For This Useful Post:

    Kirl (08-21-2007), Mr J (08-22-2007)

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    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.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Kirl View Post
    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 http://www.codingforums.com/showthread.php?p=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.
    Last edited by liorean; 08-21-2007 at 11:39 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    Kirl (08-22-2007)

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by rwedge View Post
    This will work for opera, ie, and firefox
    Code:
    <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

    Code:
    if (document.all && !document.getElementById) alert ('IE4');
    to set a value to the current event use:

    Code:
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • The Following 2 Users Say Thank You to felgall For This Useful Post:

    Kirl (08-22-2007), Philip M (08-22-2007)

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Quote Originally Posted by felgall View Post
    That code will not work in Opera
    Does for me with Opera 9, I checked it before I posted
    Quote Originally Posted by felgall View Post
    The only references to document.all should read
    Code:
    if (document.all && !document.getElementById) alert ('IE4');
    Only seems a little strong

  • #10
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    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?
    Last edited by Kirl; 08-22-2007 at 05:46 PM.

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by rwedge View Post
    Does for me with Opera 9, I checked it before I posted
    Opera 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 strong
    Not 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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    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?
    Last edited by Kirl; 08-23-2007 at 12:41 PM.

  • #13
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    This does nothing on arrow keys on IE (6 at least), how to solve that ?
    Code:
     document.keypress = function(ev)
     {	
        var key;
        ev = ev || event;
        key = ev.keyCode;
        alert(key)
     }
    Last edited by BubikolRamios; 11-22-2008 at 07:49 AM.

  • #14
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    found the answer on this forum:

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •