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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    fun with hotkeys

    I have created some hotkeys for fun on my site, and to familiarize myself a little more with JS(im new to it). My hotkeys work, but i want it so that when ctrl is pressed in, the hotkeys arent activated... such as ctrl+a and ctrl+c are still accessible... my code so far:

    PHP Code:
    function checkEnter(e){ //e is event object passed from function invocation
    var characterCode //literal character code will be stored in this variable

    if(&& e.which){ //if which property of event object is supported (NN4)
    e
    characterCode 
    e.which //character code is contained in NN4's which property
    }
    else{
    event
    characterCode 
    e.keyCode //character code is contained in IE's keyCode property
    }


    if(
    characterCode == 65){ //Letter A
    window.location "army.php" //submit the form
    return false
    }
    if(
    characterCode == 72){//Letter H
    window.location "member.php" //submit the form
    return false
    }
    if(
    characterCode == 77){//Letter M
    window.location "pm.php" //submit the form
    return false
    }
    if(
    characterCode == 78){//Letter N
    window.location "news.php" //submit the form
    return false
    }
    if(
    characterCode == 67){//Letter C
    window.location "construc.php" //submit the form
    return false
    }
    if(
    characterCode == 82){//Letter R
    window.location "research.php" //submit the form
    return false
    }
    if(
    characterCode == 69){//Letter E
    window.location "explore.php" //submit the form
    return false
    }
    if(
    characterCode == 80){//Letter P
    window.location "population.php" //submit the form
    return false
    }
    if(
    characterCode == 75){//Letter K
    window.location "map.php" //submit the form
    return false
    }
    if(
    characterCode == 83){//Letter S
    window.location "market.php" //submit the form
    return false
    }
    if(
    characterCode == 66){//Letter B
    window.location "bank.php" //submit the form
    return false
    }
    if(
    characterCode == 84){//Letter T
    window.location "product.php" //submit the form
    return false
    }

    else{
    return 
    true
    }

    }

    <
    body onKeyDown="checkEnter(event)"
    Thank you in advance
    Last edited by king2k5; 08-13-2008 at 04:36 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    I don't think that you can detect ordinary keys modified by modifier keys in Javascript (Crtl is keycode 17).

    Have a look at:-

    http://journals.ecs.soton.ac.uk/java...ylistener.html for a java solution.

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Try this:
    Code:
    <html>
    	<head>
        <script type='text/javascript'>
        function keyJump()
        {
          var e = arguments[0] || window.event;
    
          if(!keyFromInput(e.target || e.srcElement) && !specialKeysPressed(e))
          {
            var
              keyMapping = {
                a : 'army.php',
                h : 'member.php',
                m : 'pm.php',
                n : 'news.php',
                c : 'construc.php',
                r : 'research.php',
                e : 'explore.php',
                p : 'population.php',
                k : 'map.php',
                s : 'market.php',
                b : 'bank.php',
                t : 'product.php'},
              key = String.fromCharCode((e.which === undefined) ? e.keyCode : e.which);
    
            if(key in keyMapping)
            {
              window.location.href = keyMapping[key];
              return false;
            }
          }
          return true;
        }
        function keyFromInput(el)
        {
          if(el.nodeType == 3)
          {
            el = el.parentNode;
          }
          switch(el.tagName.toLowerCase())
          {
            case 'textarea':
              return true;
            case 'input':
              if(el.type == 'text')
              {
                return true;
              }
          }
          return false;
        }
        function specialKeysPressed(e)
        {
          return e.altKey || e.ctrlKey || e.metaKey || e.shiftKey;
        }
        
        window.onload = function()
        {
          document.onkeypress = keyJump;
        }
    </script>
      </head>
      <body></body>
    </html>
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • Users who have thanked dumpfi for this post:

    Philip M (08-13-2008)

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks! that worked great!


  •  

    Posting Permissions

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