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 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Jump out of an animation loop. Possible?

    I am working on a page where the user comes in on a running animation. In order to get past that animation they have to type in a certain word. (They do get a clue as to the answer.) What has me stumped is this: I need to figure out how to read the answer typed on the keyboard without using an input box. My reference book shows an "onKeyPress" event handler but is very vague about how to use it. Also, would it be easier to jump when the user is finished typing the word or when <return> is pressed.

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    window.event properties are probably worth looking up... checking for 'return' may be simpler, but it depends, I guess.
    *this message will self destruct in n-seconds*

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A little toy I had fun playing with...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>
    <style type='text/css'>
    </style>
    <script type='text/javascript'>
    var keys = '';
    var pswd = 'maf';
    window.onload = function()
    {
      document.onkeypress = docOnKeypress;
    }
    function docOnKeypress(evt)
    {
      var e = evt || window.event;
      if (e.keyCode == 27) { // ESC
        keys = '';
      }
      else {
        keys += String.fromCharCode(e.keyCode);
        if (keys == pswd) {
          alert('You win!');
          keys = '';
        }
      }
      if (keys.length > pswd.length) {
        keys = '';
      }
      window.status = keys;
    }
    </script>
    </head>
    <body>
    <h3>Guess The Password!</h3>
    </body>
    </html>

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy and still, it's trouble

    Mike,
    Thanks for the effort Still having problems though. Can't get your script to work. I'm on a Netscape browser, could that be a problem?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    What version of Netscape? 4 or 7?

    Well, I tried it in Firefox and it didn't work. To fix it for Firefox, you need to do the following. You may have to do the same things for Netscape 7 (except probably for the first item).

    First, you need to turn off the automatic Find when you begin typing as the document has focus. It is because the script is not invoked because of that feature. (Tools->Options->Advanced->Accessibility, uncheck "Begin finding when you begin typing" option.

    Second, you need to change onkeypress to onkeydown as keys are not detected when onkeypress is used.
    Code:
    document.onkeydown = docOnKeypress;
    Third, you need to make the case for the entered keys and password the same as onkeydown event always outputs uppercase letters.
    Code:
    if (keys == pswd.toUpperCase()) {
      alert('You win!');
      keys = '';
    }

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting closer, I can FEEL it

    Glenn, thanks for your suggestions. As to your question, it's Netscape 7.2. Also, the options heading is not under the Tools menu item. If it's there somewhere I can't find it.

    I followed your suggestions but I'm still not getting any action. I keep looking at the code and it looks like it should work. But being new at js I don't really think I know what I'm looking at.

    Should I be able to put commands into the header script to print something to the screen so I know where the program is going? The more this goes on the more questions seem to come up.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Just put an alert inside the function to test that it's being called. The script displays the entered keys in the status bar, so you should see something in the status bar as you type.

    I used to have NS7 installed in my PC but I uninstalled it in favor of Firefox .
    Does NS7 have that automatic Find feature when you type inside the document? What happens when you type something?

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    He he. I forgot to test it in Moz didn't I. It was a simple fix. You can use onkeypress or onkeydown, either one:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>
    <style type='text/css'>
    </style>
    <script type='text/javascript'>
    var keys = '';
    var pswd = 'maf';
    window.onload = function()
    {
      document.onkeypress = docOnKeypress;
    }
    function docOnKeypress(evt)
    {
      var e = evt || window.event;
      var key = e.keyCode || e.which; // this is the fix
      if (key == 27) { // ESC
        keys = '';
      }
      else {
        keys += String.fromCharCode(key);
        if (keys == pswd) {
          alert('You win!');
          keys = '';
        }
      }
      if (keys.length > pswd.length) {
        keys = '';
      }
      window.status = keys;
    }
    </script>
    </head>
    <body>
    <h3>Guess The Password!</h3>
    </body>
    </html>

  • #9
    New Coder
    Join Date
    Mar 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Yipee!

    Thanx guys,
    It works. Codegoboom: you were on the right track. Still, ya gotta admit, your comments were a little thin on details, at least for a newbie. Still again, thanks for responding. Glenn, we almost had it. Like they say "we wuz dis close!" Mike, I had a teacher in high school that got furious when we didn't check our work...completely. But hey, I won't report you. He He.

    And yes I do like that silly little picture of the little blue guy with the big thumb! So, sue me. He He He! (thanks again)

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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