Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <h1>Keyboard Visualizer</h1>

<p>A bookmarklet that creates an overlay to display typed keys on the underlying page. To use it, bookmark this link:
  
<a href="javascript:(function()%7Bconst%20visualizerStyles%20%3D%20%60.sh-keyboard-viz%20%7Bposition%3A%20fixed%3Bbottom%3A%2020px%3Bright%3A%2020px%3Bwidth%3A%2050%25%3Bmax-width%3A%20300px%3Bbackground%3A%20rgba(0%2C%200%2C%200%2C%200.75)%3Bcolor%3A%20white%3Bpadding%3A%2010px%2020px%3Bborder-radius%3A%204px%3Bborder%3A%202px%20solid%20white%3Bbox-shadow%3A%200%200%205px%201px%20rgba(0%2C%200%2C%200%2C%200.75)%3Bfont-size%3A%2018px%3Bfont-weight%3A%20bold%3Bfont-family%3A%20sans-serif%3Bz-index%3A%20999999%3Bmax-height%3A%20calc(100vh%20-%2060px)%3Bdisplay%3A%20flex%3Bflex-direction%3A%20column%3B%7D.sh-keyboard-viz%20ul%20%7Bflex%3A%201%201%20auto%3Blist-style-type%3A%20none%3Bpadding%3A%200%3Bmargin%3A%200%3Boverflow-y%3A%20auto%3B%7D.sh-keyboard-viz%20li%20%7Bmargin-bottom%3A%208px%3B%7D.sh-keyboard-viz%20h3%20%7Bflex%3A%200%200%20auto%3Bfont-size%3A%2024px%3Bmargin%3A%200%3Bpadding%3A%2010px%200%2015px%3B%7D%60%3Bfunction%20createKeyLine(parentEl%2C%20contentString)%20%7Bconst%20keyEl%20%3D%20document.createElement('li')%3BkeyEl.innerHTML%20%3D%20renderKeyString(contentString)%3BparentEl.appendChild(keyEl)%3Bconst%20timerId%20%3D%20window.setTimeout(()%20%3D%3E%20%7BdestroyKeyLine(keyEl)%3B%7D%2C%205000)%3Breturn%20%7B%20element%3A%20keyEl%2C%20timerId%20%7D%3B%7Dfunction%20destroyKeyLine(keyLineElement)%20%7Bconst%20parentEl%20%3D%20keyLineElement.parentNode%3BparentEl%20%26%26%20parentEl.removeChild(keyLineElement)%3B%7Dfunction%20updateKeyLine(keyLine%2C%20contentString)%20%7Blet%20%7B%20element%2C%20timerId%20%7D%20%3D%20keyLine%3Belement.innerHTML%20%2B%3D%20renderKeyString(contentString)%3Bwindow.clearTimeout(timerId)%3BtimerId%20%3D%20window.setTimeout(()%20%3D%3E%20%7BdestroyKeyLine(element)%3B%7D%2C%205000)%3Breturn%20%7B%20element%2C%20timerId%20%7D%3B%7Dfunction%20isPrintableKey(key)%20%7Breturn%20key.length%20%3D%3D%3D%201%20%26%26%20key.match(%2F%5E%5Ba-z0-9!%22%23%24%25%26'()*%2B%2C.%5C%2F%3A%3B%3C%3D%3E%3F%40%5C%5B%5C%5D%20%5E_%60%7B%7C%7D~-%5D*%24%2Fi).length%20%3E%200%3B%7Dfunction%20isModifierKey(key)%20%7Bconst%20metaKeys%20%3D%20%5B'control'%2C%20'shift'%2C%20'os'%2C%20'alt'%2C%20'fn'%2C%20'meta'%5D%3Breturn%20metaKeys.includes(key.toLowerCase())%3B%7Dfunction%20renderKeyString(keyString)%20%7Blet%20isModifier%20%3D%20isModifierKey(keyString)%3Breturn%20isModifier%20%3F%20%60%24%7BkeyString%7D%20%60%20%3A%20keyString%3B%7Dfunction%20renderVisualizer()%20%7Bconst%20container%20%3D%20document.createElement('div')%3Bcontainer.className%20%3D%20'sh-keyboard-viz'%3Bcontainer.setAttribute('role'%2C%20'region')%3Bcontainer.setAttribute('aria-labelledby'%2C%20'sh-viz-heading')%3Bconst%20heading%20%3D%20document.createElement('h3')%3Bheading.id%20%3D%20'sh-viz-heading'%3Bheading.innerText%20%3D%20'Pressed%20Keys'%3Bcontainer.appendChild(heading)%3Bconst%20keyList%20%3D%20document.createElement('ul')%3Bcontainer.appendChild(keyList)%3Breturn%20container%3B%7Dfunction%20init()%20%7Blet%20visualizerEl%20%3D%20document.querySelector('.sh-keyboard-viz')%3Bif%20(!visualizerEl)%20%7BvisualizerEl%20%3D%20renderVisualizer()%3Bdocument.body.appendChild(visualizerEl)%3Bconst%20styles%20%3D%20document.createElement('style')%3Bstyles.appendChild(document.createTextNode(visualizerStyles))%3Bdocument.head.appendChild(styles)%3B%7Dconst%20keyLineParent%20%3D%20visualizerEl.querySelector('ul')%3Blet%20currentKeyLine%3Blet%20hasModifier%20%3D%20false%3Bdocument.body.addEventListener('keydown'%2C%20(event)%20%3D%3E%20%7Bconst%20keyString%20%3D%20event.key%20%3D%3D%3D%20'%20'%20%3F%20'Space'%20%3A%20event.key%3Bif%20(isModifierKey(keyString))%20%7Bif%20(!hasModifier)%20%7BcurrentKeyLine%20%3D%20undefined%3B%7DhasModifier%20%3D%20true%3B%7Dif%20(!isPrintableKey(keyString)%20%26%26%20!hasModifier)%20%7BcurrentKeyLine%20%3D%20undefined%3B%7Dif%20(currentKeyLine)%20%7BupdateKeyLine(currentKeyLine%2C%20keyString)%7Delse%20%7BcurrentKeyLine%20%3D%20createKeyLine(keyLineParent%2C%20keyString)%3B%7Dif%20(keyLineParent.scrollHeight%20%3E%20keyLineParent.clientHeight)%20%7BcurrentKeyLine.element.scrollIntoView()%3B%7Dif%20(!isPrintableKey(keyString)%20%26%26%20!hasModifier)%20%7BcurrentKeyLine%20%3D%20undefined%3B%7D%7D%2C%20true)%3Bdocument.body.addEventListener('keyup'%2C%20(event)%20%3D%3E%20%7Bif%20(isModifierKey(event.key))%20%7BhasModifier%20%3D%20false%3BcurrentKeyLine%20%3D%20undefined%3B%7D%7D%2C%20true)%3B%7Dinit()%7D)()">key visualizer bookmarklet</a>.
  
To test it out, put focus within this page, and type.</p>

<button>focus me</button>
              
            
!

CSS

              
                body {
  padding: 2em;
  font-size: 1.25em;
}

/* Using pixels intentionally, so the visualizer does not scale with the UI. Normally this would be bad, but since the purpose of this is screen recording, it's more important to be able to zoom the UI in a recording without the visualizer also growing. */
.sh-keyboard-viz {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 50%;
  max-width: 300px;
  background: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  border: 2px solid white;
  box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.75);
  font-size: 18px;
  font-weight: bold;
  font-family: sans-serif;
  z-index: 999999;
  max-height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
}

.sh-keyboard-viz ul {
  flex: 1 1 auto;
  list-style-type: none;
  padding: 0;
  margin: 0;
  overflow-y: auto;
}

.sh-keyboard-viz li {
  margin-bottom: 8px;
}

.sh-keyboard-viz h3 {
  flex: 0 0 auto;
  font-size: 24px;
  margin: 0;
  padding: 10px 0 15px;
}
              
            
!

JS

              
                const visualizerStyles = `
.sh-keyboard-viz {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 50%;
  max-width: 300px;
  background: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  border: 2px solid white;
  box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.75);
  font-size: 18px;
  font-weight: bold;
  font-family: sans-serif;
  z-index: 999999;
  max-height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
}

.sh-keyboard-viz ul {
  flex: 1 1 auto;
  list-style-type: none;
  padding: 0;
  margin: 0;
  overflow-y: auto;
}

.sh-keyboard-viz li {
  margin-bottom: 8px;
}

.sh-keyboard-viz h3 {
  flex: 0 0 auto;
  font-size: 24px;
  margin: 0;
  padding: 10px 0 15px;
}
`;

/* returns a data object of { element, timerId } */
function createKeyLine(parentEl, contentString) {
  const keyEl = document.createElement('li');
  keyEl.innerHTML = renderKeyString(contentString);
  parentEl.appendChild(keyEl);
  
  const timerId = window.setTimeout(() => {
    destroyKeyLine(keyEl);
  }, 5000);
  
  return { element: keyEl, timerId };
}

/* Removes the passed-in element from the DOM */
function destroyKeyLine(keyLineElement) {
  const parentEl = keyLineElement.parentNode;
  parentEl && parentEl.removeChild(keyLineElement);
}

/* takes and returns a data object of { element, timerId } */
function updateKeyLine(keyLine, contentString) {
  let { element, timerId } = keyLine;

  // update the text
  element.innerHTML += renderKeyString(contentString);
  
  // reset the timeout
  window.clearTimeout(timerId);
  timerId = window.setTimeout(() => {
    destroyKeyLine(element);
  }, 20000);
  
  return { element, timerId };
}

function isPrintableKey(key) {
  return key.length === 1 && key.match(/^[a-z0-9!"#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]*$/i).length > 0;
}

function isModifierKey(key) {
  const metaKeys = ['control', 'shift', 'os', 'alt', 'fn', 'meta'];
  return metaKeys.includes(key.toLowerCase());
}

function renderKeyString(keyString) {
  let isModifier = isModifierKey(keyString);
  return isModifier ? `${keyString} ` : keyString;
}

function renderVisualizer() {
  const container = document.createElement('div');
  container.className = 'sh-keyboard-viz';
  container.setAttribute('role', 'region');
  container.setAttribute('aria-labelledby', 'sh-viz-heading');
  
  // heading
  const heading = document.createElement('h3');
  heading.id = 'sh-viz-heading';
  heading.innerText = 'Pressed Keys';
  container.appendChild(heading);
  
  // list
  const keyList = document.createElement('ul');
  container.appendChild(keyList);
  
  return container;
}

function init() {
  // if a keyboard visualizer element already exists, use that
  let visualizerEl = document.querySelector('.sh-keyboard-viz');
  
  // otherwise, create one
  if (!visualizerEl) {
    visualizerEl = renderVisualizer();
    document.body.appendChild(visualizerEl);
    
    // add stylesheet
    const styles = document.createElement('style');
    styles.appendChild(document.createTextNode(visualizerStyles));
    document.head.appendChild(styles);
  }
  
  // update keylines on keydown
  const keyLineParent = visualizerEl.querySelector('ul');
  
  // save ref to the keyline that should be used.
  // this is cleared if the next key should use a new line
  let currentKeyLine;
  let hasModifier = false;
  
  document.body.addEventListener('keydown', (event) => {
    const keyString = event.key === ' ' ? 'Space' : event.key;

    // if the key is a modifier key, clear currentKeyLine
    if (isModifierKey(keyString)) {
      if (!hasModifier) {
        currentKeyLine = undefined;
      }
      hasModifier = true;
    }
    
    // if the key is not a printable key, clear currentKeyLine
    if (!isPrintableKey(keyString) && !hasModifier) {
      currentKeyLine = undefined;
    }
    
    // create or update keyline
    if (currentKeyLine) {
      updateKeyLine(currentKeyLine, keyString)
    }
    else {
      currentKeyLine = createKeyLine(keyLineParent, keyString);
    }
    
    // scroll into view, if needed
    if (keyLineParent.scrollHeight > keyLineParent.clientHeight) {
      currentKeyLine.element.scrollIntoView();
    }
    
    // clear next key line for non-printable keys
    if (!isPrintableKey(keyString) && !hasModifier) {
      currentKeyLine = undefined;
    }
  }, true);
  
  document.body.addEventListener('keyup', (event) => {
    // clear currentKeyLine if the modifier key is released
    if (isModifierKey(event.key)) {
      hasModifier = false;
      currentKeyLine = undefined;
    }
  }, true);
}

init();
              
            
!
999px

Console