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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript keypress for specific keys?

    I am trying to create a menu that allows keyboard shortcuts to send you to a certain site. My JavaScript book tells me that keypress information is stored in event.which when I press a key. However, when I use my script, shown below, it says that event is not defined. How can I place my code so that it is defined? The event.which works and is defined if it is put straight into an event handler, like so: onKeyPress="alert('You pressed' +String.fromCharCode(event.which));"

    But it won't work for my menu script:

    <html>
    <head>
    <title>Hi</title>

    <script type="text/javascript">
    <!--
    event.which=0;

    function google(){
    if (String.fromCharCode(event.which)=="g"){
    window.location="http://www.google.com";
    };

    }

    //-->
    </script>

    </head>

    <body onKeyPress="google()">

    Press G to go to Google
    </body>
    </html>


    Any help would be appreciated, thanks

    jsh7676

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    In Netscape/Gecko browsers, you have to pass the event object to the function. In IE, event is a global object.
    Code:
    function google(evt){
      var key = (evt) ? evt.which : event.keyCode;
      if (String.fromCharCode(key)=="g"){
        window.location="http://www.google.com";
      }
    }
    ...
    <body onKeyPress="google(event)">
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You said:

    var key = (evt) ? evt.which : event.keyCode;

    How can (evt) be an expression? I don't understand how it could be true or false.

    Thanks,

    jsh7676

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    In JavaScript, zero, null, and undefined are all casted to false and true otherwise. The condition I wrote was wrong though, it should have been:

    var key = (evt.which) ? evt.which : event.keyCode;

    evt.which will return undefined for IE so it evaluates to false and assigns event.keyCode to key.

    The above code is also equivalent to this:

    var key = (typeof evt.which != "undefined") ? evt.which : event.keyCode;
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Glenn, it works now. You've been very helpful.

    much thanks!!

    jsh7676

  • #6
    New to the CF scene
    Join Date
    May 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi, im hoping glenn is still here to give some additional help to his solution. it works great but i was wondering if it was possible to get keypress values for a combination of keys? mayb like alt+L or alt+another letter. i am trying to create a similar navigation but instead to bring the user to a hidden administrator page. thanks for ur help..

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    .which is for Netscape 4 only
    you should use event.keyCode

    PHP Code:
    function name(e){
    var 
    save_click;
    if(
    e.whichclick=e.which;
    if(
    e.keyCodeclick=e.keyCode;

    something like that

  • #9
    New to the CF scene
    Join Date
    May 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the great help guys.. im gonna try this out now and wil let u kno how it works. out..

  • #10
    New to the CF scene
    Join Date
    May 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok that worked out perfectly..


    here is the modified function

    Code:
    function onKeyDownH(e)
    {
    
       switch (emod) {
       case "IE4+":
          e = window.event;
          /*C*/
          alt = (e.altKey) ? true : false;
          ctrl = (e.ctrlKey) ? true : false;
          shift = (e.shiftKey) ? true : false;
    
          /*D*/
          if ((e.keyCode==76)&&(alt==true)) {
             mainContent.location="admin.php";
             alt = false;
             ctrl = false;
             shift = false; 
          }
       break;
       case "NN4":
          /*E*/
          alt = ((e.modifiers & Event.ALT_MASK) == Event.ALT_MASK);
          ctrl = ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK);
          shift = ((e.modifiers & Event.SHIFT_MASK) == Event.SHIFT_MASK);
    
          alert("NN4");
          /*F*/
          if ((e.which==76)&&(alt==true))
          {
             mainContent.location="admin.php";
          }
       break;
       case "W3C":
          alt = (e.altKey) ? true : false;
          ctrl = (e.ctrlKey) ? true : false;
          shift = (e.shiftKey) ? true : false;
    
          if ((e.which==76)&&(alt==true))
          {
            mainContent.location="admin.php";
          }
       break;
       default:
          if (e.target) {
              alt = (e.altKey) ? true : false;
              ctrl = (e.ctrlKey) ? true : false;
              shift = (e.shiftKey) ? true : false;
        
              if ((e.which==76)&&(alt==true))
              {
                mainContent.location="admin.php";
              }
          }
       break;
       }
        
       /*G*/
       return false;
    }
    it works great and i also added a default cause for firefox.. not sure if this is the best way to do it so pls feel free to comment. but thanks again for the great help..

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by jalleyne
    it works great and i also added a default cause for firefox.. not sure if this is the best way to do it so pls feel free to comment. but thanks again for the great help..
    Firefox should fall under "W3C" as it supports the eventPhase property of the event object.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    New to the CF scene
    Join Date
    May 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i thought so but after testing in FF and it did not work but worked in IE i figured ur original solution did not account for FF.. it works now but it would be interesting to findout why it did not fall under the case of "W3C"

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    If you alert(emod), what does FF say?

    Edit:
    I run the example I linked you to, in FF and it works under "W3C".
    Last edited by glenngv; 05-10-2005 at 08:00 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #14
    New to the CF scene
    Join Date
    May 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    as far as i can remember it was "unknown"

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by jalleyne
    as far as i can remember it was "unknown"
    Try it again.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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