Click to See Complete Forum and Search --> : is this possible...? array help?
Code One
09-10-2003, 10:50 PM
Hey guys,
I was wondering if it is possible to do this: I would like to set up a script that would be able to log your key strokes in memory and then to have an array in which this script would search for the inputted chararcters, in which you have typed. Once the script has found the correct sequence, it would then look over at the property and would see that it is a music file, and would load this up and play it once the user has pressed the play button. Now I am wanting to place syllables as characters in this array, like this: Psy-cho , so once the first syllable (Psy) is typed this would be found in the array and logged in memory and the sound file on stand by, til someone pushes the play button, and of course the same is with the (cho) part. I hope you know what I'm talking about it.
Help is greatly appreciated,
Code One
Khalid Ali
09-11-2003, 09:37 AM
if a user still have to press a button, then how come you can not set the file to play at that time,why you have to capture the key strokes????
In reality if I understood you correctly,this could bring your browser down as well,becaue if a user is type stuff it all will be in brwosers memory....yikes
Code One
09-11-2003, 06:00 PM
Khalid,
Still I would like to try atleast. I would have to capture the key strokes so that a script would know when a certain sequence was typed, such as (psy)-(cho). Then when you push play the script would search the array for the specific sound file. Can you help?
Thanks,
Code One
Code One
09-11-2003, 10:21 PM
Hey,
I did some experimenting and this is what I got:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<html>
<head>
<title></title>
<script>
var syllables = new Array( "ay", "bay", "bih" );
var phrase = new Array;
var typed = "";
document.onkeypress = function ( evt ) {
var c = document.layers ? evt.which
: document.all ? event.keyCode
: evt.which;
typed += String.fromCharCode( c );
for( i=syllables.length; i > -1 ; i-- ) {
if ( typed.indexOf( syllables[i]) != -1 ) {
phrase.push(syllables[i]);
typed = "";
}
}
}
function playPhrase() {
if( phrase.length < 0 ) {
return;
}
for( i=0; i < phrase.length; i++ ) {
soundfile = document.getElementById(phrase[i]);
setTimeout("soundfile.play()", 500);
}
}
</script>
</head>
<body>
<textarea cols=12 rows=12></textarea>
<button onclick="playPhrase();">Play</button>
<button onclick="phrase = new Array();">Reset</button>
<embed id="ay" name="ay" src="sounds/a.wav" loop="false" autostart="false" hidden="true">
<embed id="bay" name="bay" src="sounds/b.wav" loop="false" autostart="false" hidden="true">
<embed id="bih" name="bih" src="sounds/c.wav" loop="false" autostart="false" hidden="true">
</body>
</html>
Now:
The only problem with this is that:
1. It overwrites the previous key strokes with the present ones, which mean when you push play only the last thing you typed will be played back to you, vs. all of the key strokes.
Conclusion: Awesome try, great effort, though not sufficient. :(
==========================================================
Ok so thats suggestion #1. So we move down to suggestion #2 posted by "wray" and he suggested that we change the play script like so:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<html>
<head>
<title></title>
<script>
var syllables = new Array( "ay", "bay", "bih" );
var phrase = new Array;
var typed = "";
document.onkeypress = function ( evt ) {
var c = document.layers ? evt.which
: document.all ? event.keyCode
: evt.which;
typed += String.fromCharCode( c );
for( i=syllables.length; i > -1 ; i-- ) {
if ( typed.indexOf( syllables[i]) != -1 ) {
phrase.push(syllables[i]);
typed = "";
}
}
}
function playPhrase() {
if( phrase.length > 0 )
alert(phrase.join( " - "));
phrase.reverse();
playSound();
}
function playSound() {
soundfile = document.getElementById(phrase.pop());
soundfile.play()
if( phrase.length > 0 ) { playSound() }
}
setTimeout("playSound();",500);
</script>
</head>
<body>
<textarea cols=12 rows=12></textarea>
<button onclick="playPhrase();">Play</button>
<button onclick="phrase = new Array();">Reset</button>
<embed id="ay" name="ay" src="sounds/a.wav" loop="false" autostart="false" hidden="true">
<embed id="bay" name="bay" src="sounds/b.wav" loop="false" autostart="false" hidden="true">
<embed id="bih" name="bih" src="sounds/c.wav" loop="false" autostart="false" hidden="true">
</body>
</html>
Now:
This is good also and actually accomplishes the play all the inputs objective. But still this script will not play the sounds apart. The settimeout is written wrongly some how, I'm not sure what it may be. What is happening is
that once you have inputted all the specified sequences and had pushed play, the sounds overlap one another, which is not good. Also this annoying alert box keeps popping up prior to each fireing of the play button. So the break down is:
1. Annoying alert box displays what you have typed before it plays eah specified sound.
2. The settimeout does not work.
Conclusion: Great try, almost working, still insufficient.
===========================================================
If anyone knows a way to fix these errors and feels kind enough to post them that would be awesome of you and is totally welcomed. I don't anticipate this to be a hard task to accomplish for a seasoned programmer, so if you have time, feel free to oblige me with your insight.
Thanks to everybody here,
Code One