Last active
October 12, 2015 15:08
-
-
Save rowanmanning/4045521 to your computer and use it in GitHub Desktop.
Simple Konami Code with jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"/> | |
<title>↑ ↑ ↓ ↓ ← → ← → B A</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script src="konami.js"></script> | |
<script> | |
// On document ready | |
$(function () { | |
// When the Konami Code is typed | |
$('body').on('konami', function () { | |
// Replace this with something awesome | |
alert('KONAMI!'); | |
}); | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
var sequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65]; | |
var keys = []; | |
// Bind events to body on load | |
$(function () { | |
var body = $('body'); | |
body.on('keydown', function (event) { | |
// Update pressed keys | |
keys.push(event.which); | |
if (keys.length > sequence.length) { | |
keys.shift(); | |
} | |
// Do our keys match the sequence? | |
if (keys.toString() === sequence.toString()) { | |
body.trigger('konami'); | |
} | |
}); | |
}); | |
} (jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment