...

View Full Version : Jump out of an animation loop. Possible?



graphicsdude
03-08-2005, 07:17 PM
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. :confused:

codegoboom
03-08-2005, 08:50 PM
window.event properties are probably worth looking up... checking for 'return' may be simpler, but it depends, I guess.

MikeFoster
03-09-2005, 04:26 AM
A little toy I had fun playing with...


<!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>

graphicsdude
03-09-2005, 10:48 PM
Mike,
Thanks for the effort :thumbsup: Still having problems though. Can't get your script to work. I'm on a Netscape browser, could that be a problem?

glenngv
03-10-2005, 03:28 AM
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.


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.


if (keys == pswd.toUpperCase()) {
alert('You win!');
keys = '';
}

graphicsdude
03-10-2005, 04:59 AM
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.

glenngv
03-10-2005, 05:22 AM
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 :D.
Does NS7 have that automatic Find feature when you type inside the document? What happens when you type something?

MikeFoster
03-10-2005, 02:53 PM
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:


<!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>

graphicsdude
03-11-2005, 01:07 AM
Thanx guys:thumbsup:,
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! :thumbsup: (thanks again)

MikeFoster
03-11-2005, 01:34 PM
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum