SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When I pressed to F1 key,...

    When I pressed to F1 (or F2 or F3 ) key, Google home page will open. How can I do?

    if (e.keyCode == A number will be here. Which?)

    location.href ="http://www.google.com.tr"
    Code:
    <script type="text/javaScript">
    
    document.onkeydown= openPage ;
    
    function openPage(e) {
    
    e= window.event ? event : e;
    
    if (e.keyCode == 112 ) { location.href="http://www.google.com.tr" ; }
    
    }
    
     </script>
    I found this page:
    http://www.mediaevent.de/javascript/needful_tables.html
    Last edited by muazzez; Jan 18, 2007 at 10:09.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    112 according to this list: http://www.cambiaresearch.com/cambia...charcodes.aspx
    You can test it and it works. Might be different on a turkish keyboard though.

  3. #3
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ruffless,
    Thanks a lot!
    The script I wanted to write, is in link you gave.

  4. #4
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Don't mess with the F* key! They have important functions to the application or OS the user is using, messing with them can cause confusion. Which is bad btw.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use this little page to find the keyCode of any of the keys on your keyboard.
    When you press a key you will get one or 2 alerts- the first is the keydown value, the second is the keypress (if any) value.

    <html>
    <head>
    <title>Key Codes</title>
    <script type="text/javascript">
    onload=function(){document.onkeydown=function(e){
    e=(window.event)? event:e;
    var k=e.keyCode;
    if(k==16 ||k==17 || k==18)return false;
    if(e.shiftKey)k='shift(16) + '+k;
    if(e.ctrlKey) k='ctrl(17) + '+k;
    if(e.altKey) k='alt(18) + '+k;
    alert('keydown='+k);
    }
    document.onkeypress=function(e){
    e=(window.event)? event.keyCode: (e.charCode || e.charCode===0)? e.charCode:e.keyCode;
    if(e)alert('keypress='+e);
    return false;
    }

    }
    </script>
    </head>
    <body >
    <h1>Key Codes</h1>
    <p>Press any key.</p>
    </body>
    </html>
    Last edited by mrhoo; Jan 20, 2007 at 11:15. Reason: corrected for modifier keys

  6. #6
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo,
    I tried your code. When I pressed function keys (f1-f12) and number keys (0,1,2,3,4,5,6,7,8,9) , keydown value is equal to keypress value.
    But if I press L key, it writes
    keydown=76
    keypress=108

    If I press P key, it writes
    keydown=80
    keypress=112

    Why are they not equal?
    Code:
     
    <html>
    <head>
    <title>Key Codes</title>
    <script type="text/javascript">
    
    onload=function(){document.onkeydown=function(e){
    
    e=(window.event)? event:e;
    
    var d = document.getElementById('mydiv');
    
    var k=e.keyCode;
    if(k==16 ||k==17 || k==18)return false;
    if(e.shiftKey)k='shift(16) + '+k;
    if(e.ctrlKey) k='ctrl(17) + '+k;
    if(e.altKey) k='alt(18) + '+k;
    
    d.innerHTML = 'keydown='+k ;
    
    // alert('keydown='+k);
    }
    document.onkeypress=function(e){
    
    e=(window.event)? event.keyCode: (e.charCode)? e.charCode:e.keyCode;
    
    var d2 = document.getElementById('mydiv2');
    d2.innerHTML = 'keypress='+e  ;
    
    // alert('keypress='+e);
    return false;
    }
    
    }
    </script>
    </head>
    <body >
    <h1>Key Codes</h1>
    <p>Press any key.</p>
    <div id="mydiv"></div>
    
    <div id="mydiv2"></div>
    
    </body>
    </html>

  7. #7
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have on disc for this one script, but I dont know author so sorry to him.
    Code:
    <script type="text/javascript">
    <!--
    this.focus();
    var moz = false;
    if (navigator.appName == "Mozilla" || (navigator.appName == "Netscape" && navigator.appVersion.indexOf("4.") == -1))
    moz = true;
    if (moz)
    document.addEventListener("keyup",keypress,true);
    else if(navigator.appName == "Netscape" && navigator.appVersion.indexOf("4.x") != -1)
    document.captureEvents(Event.KEYPRESS);
    if (!moz)
    document.onkeypress=keypress;
    function keypress(e) {
    if (navigator.appName == "Microsoft Internet Explorer") 
    tmp = window.event.keyCode;
    else if (moz)
    tmp = e.keyCode;
    document.getElementById('text').value = tmp;
    }
    //-->
    </script>
    <input type=text id="text" name="conversion">

  8. #8
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.
    I have no problem.

  9. #9
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A keypress event returns a printable character, which is the result of the key plus any modifier keys, like shift being pressed at the same time.

    the keydown keyCode for capital A is the same as that for lower case a,
    you are pressing the same key down- but when the keypress is returned, it ammends the code based on the shiftKey, so keypress returns different codes for A and a.

    The keys that have no printable characters return no value for the keyCode on keypress, so you must catch them on keydown.

    keydown= the key on the keyboard
    keypress= printable character produced from all the keydowns

  10. #10
    SitePoint Addict Jack Matier's Avatar
    Join Date
    Feb 2005
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was doing something like this a while ago, bookmarked.

  11. #11
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44° 56.537' W 123° 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Desktop environments often don't take kindly to applications hijacking the function keys. What you may get to work on Win XP might not fly on 2000 or Linux. It's simply not a reliable practice to alter the functionality they already have assigned. You may find the following useful in assessing which browsers support which keyboard events:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    td,th{border:2px solid #aaa;}
    </style>
    <script type="text/javascript">
    var t_cel,tc_ln;
    if(document.addEventListener){ //code for Moz
      document.addEventListener("keydown",keyCapt,false); 
      document.addEventListener("keyup",keyCapt,false);
      document.addEventListener("keypress",keyCapt,false);
    }else{
      document.attachEvent("onkeydown",keyCapt); //code for IE
      document.attachEvent("onkeyup",keyCapt); 
      document.attachEvent("onkeypress",keyCapt); 
    }
    function keyCapt(e){
      if(typeof window.event!="undefined"){
        e=window.event;//code for IE
      }
      if(e.type=="keydown"){
        t_cel[0].innerHTML=e.keyCode;
        t_cel[3].innerHTML=e.charCode;
      }else if(e.type=="keyup"){
        t_cel[1].innerHTML=e.keyCode;
        t_cel[4].innerHTML=e.charCode;
      }else if(e.type=="keypress"){
        t_cel[2].innerHTML=e.keyCode;
        t_cel[5].innerHTML=e.charCode;
      }
    }
    window.onload=function(){
      t_cel=document.getElementById("tblOne").getElementsByTagName("td");
      tc_ln=t_cel.length;
    }
    </script>
    </head>
    <body>
    <table id="tblOne">
    <tr>
    <th style="border:none;"></th><th>onkeydown</th><th>onkeyup</th><th>onkeypress</td>
    </tr>
    <tr>
    <th>keyCode</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
    <tr>
    <th>charCode</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
    </tr>
    </table>
    <button onclick="for(i=0;i<tc_ln;i++){t_cel[i].innerHTML='&nbsp;'};">CLEAR</button>
    </body>
    </html>
    Note that Konqueror and Safari only report values for the keyCode value of the onkeydown event.


Bookmarks

Posting Permissions

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