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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Key Event Preferes....

    Hi you all

    Im started to program JS 2 weeks ago and here is a code that asposed to be simple:
    Code:
    <html>
    <head>
    <script language="javascript">
    <!--
    
    /*B*/
    function onKeyDownH(e)
    {
       var output_string;
    	
    
       switch (emod) {
       case "IE4+":
          e = window.event;
          /*C*/
          alt = (e.altKey) ? true : false;
          ctrl = (e.ctrlKey) ? true : false;
          shift = (e.shiftKey) ? true : false;
    
          output_string = " alt: "+alt+"\n ctrl: "+ctrl+"\n shift: "+shift;
    
          /*D*/
          if ((e.keyCode<16)||(e.keyCode>18)) {
             output_string += "\n 'keyCode' attribute: "+e.keyCode;
             document.forms[0].elements[0].value = output_string;
             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);
    
          output_string = " alt: "+alt+"\n ctrl: "+ctrl+"\n shift: "+shift;
    
          /*F*/
          if ((e.which)&&(e.which!=0))
          {
             output_string += "\n 'which' attribute: "+e.which;
             document.forms[0].elements[0].value = output_string;
          }
       break;
       case "W3C":
          alt = (e.altKey) ? true : false;
          ctrl = (e.ctrlKey) ? true : false;
          shift = (e.shiftKey) ? true : false;
    
          output_string = " alt: "+alt+"\n ctrl: "+ctrl+"\n shift: "+shift;
    
          if ((e.which<16)||(e.which>18))
          {
             output_string += "\n 'which' attribute: "+e.which;
             document.forms[0].elements[0].value = output_string;
          }
       break; }
    
       /*G*/
       return false;
    }
    
    function onloadH(e)
    {
       /*get the event model*/
       emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";
    
       /*A*/
       if (emod == "NN4") {
          document.captureEvents(Event.KEYDOWN); }
    
       document.onkeydown = onKeyDownH;
       return true;
    }
    
    /*define the event handler for the onload event*/
    window.onload = onloadH;
    
    /*global vars*/
    var emod; /*the event model*/
    var alt = false;
    var ctrl = false;
    var shift = false;
    
    //-->
    </script>
    
    </head>
    
    <body>
       <form>
          <textarea rows=14 cols=40 readonly></textarea>
       </form>
    <input type=text >
    </body>
    
    </html>
    The problem is that when I try to write at the input its not allow me right
    and presents on the TEXTAREA the predictable output.

    How can I have both events:
    1 - Writing in the input
    2 - The Key event

    thanks to the helpers :P
    Ofir.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    you may use onfocus and onblur to change a Boolean. Here's a simplified example of preventing event capture while focusing on a text field control:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Captura key code</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    var b=true;// a Boolean
    function capturekey(e){
    if(b){
    var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
    alert('keycode : '+key);
    }
    }
    if(navigator.appName!= "Mozilla"){document.onkeyup=capturekey}
    else{document.addEventListener("keypress",capturekey,true);}
    </script>
    </head>
    <body>
    <input type="text" onfocus="b=false" onblur="b=true">
    </body> 
    </html>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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