Dove Man Dove Man - 1 month ago 7
CSS Question

Keyboard combination that triggers a HTML event?

So on my website I want sort of an easter egg, like lets say the user double taps the number 4 on the keyboard and the HTML code will activate.

Here's the code I want triggered, note I don't want it playing before hand.

<div style="text-align:center" id='imageFall'><script type="text/javascript" src="http://apearlibrary.weebly.com/uploads/8/5/2/5/85251306/tools.php?id=1005&img=1&waft=150&num=20&fallSpeed=5">


Does anyone know how to do this? I would very much appreciate some help.

(If you do have a code for this, would you please combine it with mine in your answer. Thanks!)

Answer

You could do something like this:

var keys = 0;
document.onkeydown = function(e){
    if (e.keyCode === 52) {
        if (++keys > 1) {
            document.onkeydown = null;
            window.scr = document.createElement("script");
            scr.src = "http://apearlibrary.weebly.com/uploads/8/5/2/5/85251306/tools.php?id=1005&img=1&waft=150&num=20&fallSpeed=5";
            document.body.insertBefore(src,document.body.childNodes[0]);

        }
        setTimeout(function(){
            keys--;
        },100);
    }
};

The keys variable is incremented when the user presses 4, and then decremented back down after 100ms. If they press the number 4 100ms after a previous press, it will trigger the easter egg.

Comments