// This listens for keypress of entire document
// If you change this to an element, it will
// only listen when that element is in focus
document.onkeypress = function (e) {
// These need to be numbers, not strings!
// Order doesn't matter
var myKeys = [20, 13, 11, 54];
// Which works in some browsers and
// keycode works in others. This will
// use whichever works.
var keyPressed = e.which || e.keyCode;
// indexOf will look inside the array for
// any occurance of e.keyCode, if none
// are found it returns -1. So this just
// checks if the keycode pressed exists
// in the array.
if (myKeys.indexOf(keyPressed) != -1) {
console.log("You pressed one of the keys!");
}
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.