Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <div id="keys"></div>
  <h1>JS SHORTCUT KEYS</h1>
  <div>
    PRESS <code>CONTROL-ALT-F</code> &amp;
    <code>CONTROL-ALT-B</code>
  </div>

  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/shortcut-keys-javascript/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (X) DOES NOT MATTER - COSMETICS */
/* PAGE & BODY */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1531347334762-59780ece5c76?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MTcwMDY3MQ&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  text-align: center;
}

/* WIDGET */
.widget-wrap {
  max-width: 500px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.4);
}
code {
  padding: 3px;
  background: #fea;
}

/* SVG */
#keys {
  width: 100%; height:100px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 576 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M528 448H48c-26.51 0-48-21.49-48-48V112c0-26.51 21.49-48 48-48h480c26.51 0 48 21.49 48 48v288c0 26.51-21.49 48-48 48zM128 180v-40c0-6.627-5.373-12-12-12H76c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm-336 96v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm-336 96v-40c0-6.627-5.373-12-12-12H76c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12zm288 0v-40c0-6.627-5.373-12-12-12H172c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h232c6.627 0 12-5.373 12-12zm96 0v-40c0-6.627-5.373-12-12-12h-40c-6.627 0-12 5.373-12 12v40c0 6.627 5.373 12 12 12h40c6.627 0 12-5.373 12-12z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                var shortcut = {
  // (A) PROPERTIES
  pressed : [], // current key presses
  actions : {}, // shortcut keys & functions to call

  // (B) KEYPRESS TRACKER
  track : (key, down) => {
    // (B1) ON KEY DOWN
    if (down) { if (!shortcut.pressed.includes(key)) {
      shortcut.pressed.push(key);
      let sequence = shortcut.pressed.join("-");
      if (shortcut.actions[sequence]) {
        shortcut.reset();
        shortcut.actions[sequence]();
      }
    }}

    // (B2) ON KEY UP
    else {
      let idx = shortcut.pressed.indexOf(key);
      if (idx != -1) { shortcut.pressed.splice(idx, 1); }
    }
  },

  // (C) START/PAUSE LISTENING TO KEY PRESSES
  up : e => shortcut.track(e.key.toLowerCase(), false),
  down : e => shortcut.track(e.key.toLowerCase(), true),
  start : () => {
    window.addEventListener("keyup", shortcut.up);
    window.addEventListener("keydown", shortcut.down);
    window.addEventListener("blur", shortcut.reset);
  },
  pause : () => {
    window.removeEventListener("keyup", shortcut.up);
    window.removeEventListener("keydown", shortcut.down);
    window.removeEventListener("blur", shortcut.reset);
    shortcut.reset();
  },
  reset : () => shortcut.pressed = [],

  // (D) ADD SHORTCUT KEY ACTION(S)
  add : keys => {
    for (let [sc, fn] of Object.entries(keys)) {
      if (shortcut.actions[sc] == undefined) { shortcut.actions[sc] = fn; }
    }
  },

  // (E) REMOVE SHORTCUT KEY ACTION(S)
  del : keys => {
    if (typeof keys == "string") { keys = [keys]; }
    for (let sc of keys) {
      if (shortcut.actions[sc] != undefined) { delete shortcut.actions[sc]; }
    }
  }
};

// (F) DEMO
window.onload = () => {
  // (F1) REGISTER SHORTCUT KEYS & START
  shortcut.add({
    "control-alt-f" : () => alert("FOO!"),
    "control-alt-b" : () => alert("BAR!")
  });
  shortcut.start();

  // (F2) PAUSE SHORTCUT KEYS
  // shortcut.pause();

  // (F3) REMOVE SHORTCUT KEYS
  // shortcut.del("alt-f");
  // shortcut.del(["alt-f", "alt-b"]);
};
              
            
!
999px

Console