userden
userden

Reputation: 1693

How to trigger an event from the keyboard on keypress?

I'm wondering how e.g. this website did the following 'easter egg':

When you type 'dance' on your keyboard on this page: http://wistia.com/about/yearbook the images change to people dancing.

How could I implement something similar? E.g.

$('#image').bind('keypress', function(e) {
    if(e.keyCode==70){
        // pressed 'f', do something...
    }
});

Upvotes: 0

Views: 87

Answers (1)

user2010925
user2010925

Reputation:

Just append the input to a string, and check to verify that the last bit of the input = the characters you are looking for.

var secret = "6865786769"; //dance
var input = "";

$(document).keyup(function(e) {
    input += e.which;

    check_input();
});

function check_input() {
    if(input.substr(input.length-secret.length) == secret) {
        //the secret code
        alert('Dance');
    }
}

Here is a working jsfiddle.

Upvotes: 1

Related Questions