Skip to content

Instantly share code, notes, and snippets.

@rchern
Created December 5, 2010 07:17
Show Gist options
  • Save rchern/728909 to your computer and use it in GitHub Desktop.
Save rchern/728909 to your computer and use it in GitHub Desktop.
Keyboard Shortcuts
function with_jquery(callback) {
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + callback.toString() + ")(jQuery)";
document.body.appendChild(script);
}
with_jquery(function ($) {
$(function () {
// plugin from http://www.gethifi.com/blog/konami-code-jquery-plugin-pointlessly-easy
// unbind removed
$.fn.konami = function (callback, code) {
if (code === undefined) {
code = "38,38,40,40,37,39,37,39,66,65";
}
return this.each(function () {
var kkeys = [];
$(this).keydown(function (e) {
kkeys.push(e.keyCode);
if (kkeys.toString().indexOf(code) >= 0) {
callback(e);
}
}, true);
});
};
var goToDestination = function (url) {
location = url;
};
var DestinationShortcuts = {
"72": { url: "/" },
"81": { url: "/questions" },
"84": { url: "/tags" },
"85": { url: "/users" },
"66": { url: "/badges" },
"78": { url: "/unanswered" },
"65": { url: "/ask" },
"82": { url: "/users/recent" },
"67": { url: "http://chat." + location.hostname },
"80": { url: $("hlinks-user a").eq(1).href },
"83": { url: "http://" + (location.hostname.indexOf("meta") === 0 ? location.hostname.substring(5) : "meta." + location.hostname) }
};
var destinationActivator = "71";
var $w = $(window);
$.each(DestinationShortcuts, function (code, item) {
$w.konami(function () { goToDestination(item.url) }, destinationActivator + "," + code);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment