onkeydown

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KublaiKhan

    onkeydown

    I am trying to figure out what values are returned when keys are
    pressed in Mozilla & IE. Here's the code I'm using. There are no
    alerts in either browser. Any clues would be greatly appreciated.
    Thanks. KK

    <html>
    <head><title>Re ading Keystrokes</title>

    <script language="JavaS cript">
    window.onkeyDow n = keyHit;
    function keyHit(evt) {
    if (evt && evt.which) { //NS
    thisKey = evt.which;
    }
    else if(window.event && window.event.ke yCode) { //IE
    thisKey=window. event.keyCode;
    }
    alert(thisKey)
    }
    </script>
    </head><body></body></html>
  • koethler

    #2
    Re: onkeydown

    The method I used to get your script to work is this. First I put the
    'onKeyDown' handler in the <body> tag. Second inside the keyHit() function
    I made the first line--

    var evt = window.event;

    --the code works in this format in IE, though I don't know about any cross
    browser application.

    Do you know anything about how to disable the Backspace Key from
    performing the history.back method? I can disable it for text but not for
    browsing.

    Comment

    • Grant Wagner

      #3
      Re: onkeydown

      KublaiKhan wrote:
      [color=blue]
      > I am trying to figure out what values are returned when keys are
      > pressed in Mozilla & IE. Here's the code I'm using. There are no
      > alerts in either browser. Any clues would be greatly appreciated.
      > Thanks. KK
      >
      > <html>
      > <head><title>Re ading Keystrokes</title>
      >
      > <script language="JavaS cript">[/color]

      <script type="text/javascript">
      [color=blue]
      > window.onkeyDow n = keyHit;[/color]

      document.onkeyd own = keyHit;
      [color=blue]
      > function keyHit(evt) {
      > if (evt && evt.which) { //NS
      > thisKey = evt.which;
      > }
      > else if(window.event && window.event.ke yCode) { //IE
      > thisKey=window. event.keyCode;
      > }
      > alert(thisKey)
      > }[/color]

      function keyHit(evt) {
      evt = evt || window.event;
      alert(evt.which ? evt.which : evt.keyCode);
      }
      [color=blue]
      > </script>
      > </head><body></body></html>[/color]

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • J. J. Cale

        #4
        Re: onkeydown


        "KublaiKhan " <birky99@hotmai l.com> wrote in message
        news:f06d5ef0.0 412021701.38220 271@posting.goo gle.com...[color=blue]
        > I am trying to figure out what values are returned when keys are
        > pressed in Mozilla & IE. Here's the code I'm using. There are no
        > alerts in either browser. Any clues would be greatly appreciated.
        > Thanks. KK
        >
        > <html>
        > <head><title>Re ading Keystrokes</title>
        >
        > <script language="JavaS cript">[/color]

        <script type="text/javascript">
        [color=blue]
        > window.onkeyDow n = keyHit;[/color]

        document.onkeyd own = keyHit;
        if you just want general keyboard response use the
        onkeypress handler. It handles upper/lower case
        numbers and punctuation. Onkeydown requires a solid
        understanding of handling key events.
        [color=blue]
        > function keyHit(evt) {[/color]
        [color=blue]
        > if (evt && evt.which) { //NS[/color]

        // you haven't declared thisKey and it will
        // be available as a global variable to the rest of
        // your script/s !!
        var thisKey; // now local variable
        [color=blue]
        > thisKey = evt.which;[/color]
        [color=blue]
        > }
        > else if(window.event && window.event.ke yCode) { //IE
        > thisKey=window. event.keyCode;
        > }
        > alert(thisKey)
        > }
        > </script>
        > </head><body></body></html>[/color]

        <script type="text/javascript">
        document.onkeyp ress = keyHit;

        function keyHit(evt) {
        var e = evt || window.event;
        var thisKey = e.which || e.keyCode;
        alert(String.fr omCharCode(this Key));
        }
        </script>
        HTH
        Jimbo


        Comment

        • KublaiKhan

          #5
          Re: onkeydown

          "koethler" <koethler@telus .net> wrote in message news:<4512bc649 891a103af229cb4 fbb96be6@localh ost.talkaboutpr ogramming.com>. ..[color=blue]
          > The method I used to get your script to work is this. First I put the
          > 'onKeyDown' handler in the <body> tag. Second inside the keyHit() function
          > I made the first line--
          >
          > var evt = window.event;
          >
          > --the code works in this format in IE, though I don't know about any cross
          > browser application.
          >
          > Do you know anything about how to disable the Backspace Key from
          > performing the history.back method? I can disable it for text but not for
          > browsing.[/color]

          I simplified the code on Grant Wagner's advice to:
          <html>
          <head><title>Re ading Keystrokes</title>

          <script type="text/javascript">
          document.onkeyd own = keyHit;
          function keyHit(evt) {
          evt = evt || window.event;
          alert(evt.which ? evt.which : evt.keyCode);
          }
          </script>
          </head><body></body></html>

          Turns out my original problem was I had document.onkeyD own instead of
          document.onkeyd own (lowercase). Now it works fine under Mozilla & IE6.
          Thanks folks. I am very new js. With that in mind, I have pages
          1,2,3,4. Link on p. 1 takes you p. 2 which checks for cookies & if
          false, does a
          this.location.h ref="page3.html "
          P. 3 first line is
          <meta http-equiv=refresh content="0;url= page4.html">
          A link in page 4 then takes you to p. 2. Backward navigation from p.2
          now takes you to p.1 with pages 3 & 4 erased from history.
          Something to think about.

          KK

          Comment

          Working...