2

I am wondering if it's possible to detect words input. I have 2 options:

  1. The word to be written in an input, and at Enter press to fire a certain command like animate or go to url.

  2. The word to be written anywhere on the page, like GTA cheats, or some YouTube Easter Eggs.

If I'm not being clear, just say and i'll edit.

4
  • when you mean written, do you mean just to detect a series of keypresses anywhere on the page? Commented Aug 11, 2013 at 9:45
  • Yes, it's possible. Are you having problems with your implementation? Commented Aug 11, 2013 at 9:49
  • @JustinL. Yes, a series of keys. Or a word in the input. Commented Aug 11, 2013 at 9:54
  • @Blender Yes, can you help me with some code? An example? Commented Aug 11, 2013 at 9:55

3 Answers 3

6

Add a keypress listener to body, append the chars to a string and compare it to a target word:

var word = "hello";
var input = "";
document.body.addEventListener('keypress',function(ev){
    input += String.fromCharCode(ev.keyCode);
    console.log(input);
    if(input == word){
        alert('typed hello');
        input = "";
    }
});

// reset input when pressing esc
document.body.addEventListener('keyup',function(ev){
    if(ev.keyCode == 27) input = "";
});

Demo:

http://jsfiddle.net/9GC4N/

Sign up to request clarification or add additional context in comments.

4 Comments

It works! Thanks man! :) You derserve a beer! photoshopistul.netii.net try writing the name of the red plummer who's always high on shrooms ;)
@yomisimie Nice! Glad i could help. thanks i'll get myself a beer if you insist :D
Hey. Small problem: the code works, but if i make it play a sound, the input does not reset. I can't insert another input word. I have to press escape. If I make it change pages, it works because the code reloads. Any ideas?
@yomisimie hey, i saw on your page you're creating a new eventlistener for each of your eastereggs. it would be better though if you only used one and then check for each word in an if..elseif or switch block. I don't know if that's your problem exactly but it could mess it up.
1

An improvement:

var seq = "rainbow"
var input = ""
window.addEventListener("keypress", function(e) {
    input += String.fromCharCode(e.keyCode)

    for (var i = 0; i < seq.length; i++) {
        if (input[i] != seq[i] && input[i] != undefined) {
            input = ""
        }
    }

    if (input == seq) {
        alert("EASTER EGG!")

        input = ""
    }
})

Comments

0

I think it makes more sense to use a queue as that way we always compare the last x characters with the target sequence after every key press.

E.g.

var seq = 'upsidedown';
var input = '';
window.addEventListener('keypress', function(e) {
    // Add the latest character to the end of the queue
    input += String.fromCharCode(e.keyCode);

    // If the queue is longer than the target sequence remove the first character
    if(input.length > seq.length) {
        input = input.substr(1);
    }

    // Check for a match
    if(input == seq) {
        alert("EASTER EGG!")
    }

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.