Question

Searching here, I found this tutorial and it is really helpful. I could easily do document.onkeypress to get the key pressed, and String.fromCharCode() to convert the keycode to a readable char. But I was wondering how can I be able to detect specific words?

Like:

var words; // something to store the latest 3 words for example

// then somehow concatenate each key pressed
// separating in the array based on the space key

var all = words[0]+" "+words[1]+" "+words[2];
var lastTwo = words[1]+" "+words[2];
if(all == "i love you"){
   alert("I love you too :)"); 
}else if(lastTwo = "screw you"){
   alert("You should not say something like this to me");
}

EDIT: What I am really interested in is how can I concatenate the keys recursively?

Was it helpful?

Solution 2

My only problem is that I am editing the html file directly on the server with nano, so I just commit some mistakes that I could not recognize.

Basically doing:

var word = "";
function dump(e){
  var unicode = e.keyCode? e.keyCode : e.charCode;
  var actualkey = String.fromCharCode(unicode);
  word += actualkey;
  alert(word);
}
document.onkeypress = dump

I am able to see the keys concatenating which was my main problem. After that I will just adjust the values to an array and join them neatly like @Diodeus suggested.

OTHER TIPS

First you need to build your array elements into a string:

var phrase = words.join(" ");

Then see what's in there.

if(phrase.indexOf('sandwiches')>-1) {
     alert("found sandwiches! let's eat.");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top