---
title: "Global Key Commands"
description: "One shared, typing-safe document keydown listener - df$.shadcn.shared.bindGlobalKeys - that components and pages register shortcuts into. Registration order, stop semantics, unbinding, and every shortcut the components ship."
slug: "global-keys"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeWindow, CodeCard, TableCard } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="global-keys" title="Global Key Commands" introStyle="max-width:38rem;">
  Page-wide shortcuts - <kbd>⌘B</kbd> for the sidebar, arrow keys for a deck, your own <kbd>?</kbd> for help - all run through <strong>one</strong> shared <code>keydown</code> listener in the runtime: <code>df$.shadcn.shared.bindGlobalKeys</code>. By default it never fires while someone is typing in a field, so a shortcut can't eat a letter meant for a form - and a handler that needs keys there too opts in with <code>{"{ editable: true }"}</code>.
</PageHeader>

<H2>The shared listener</H2>

<p class="text-sm text-muted-foreground mb-5"> <code>bindGlobalKeys(handler)</code> adds a handler to a single document-level <code>keydown</code> listener that <code>core.js</code> installs on the first registration and removes again after the last unbind. Instead of every component hand-rolling its own listener and guard, they all share this one, which means: </p>

<TableCard title="how handlers run" mb="1.5rem" rows={[
  ['Rule', 'What it means'],
  ['Typing is never a shortcut (by default)', 'Keys aimed at an input, textarea, select or contenteditable element are not dispatched - unless the handler was bound with the editable: true option; it then decides itself (isEditableTarget(event))'],
  ['Registration order', 'Handlers run in the order they were bound'],
  ['return true stops the rest', 'A handler that took the key returns true - later handlers skip that event'],
  ['Unbind', 'bindGlobalKeys returns its own unbind function; the last unbind removes the document listener'],
  ['Safe before a document exists', 'Binding outside a browser is a no-op that still returns a working unbind'],
]} />

<CodeWindow lang="javascript" title="the API" mb="2.5rem" code={"const { bindGlobalKeys } = df$.shadcn.shared; // installed by core.js / all.js\n\nconst unbind = bindGlobalKeys((event) => {\n  if (event.key !== '?') return;      // not ours - let later handlers see it\n  event.preventDefault();\n  df$('#help').get(0).showModal();    // df$ selects; showModal() stays native\n  return true;                        // ours - stop here\n});\n\n// later, e.g. when the view that owns the shortcut goes away\nunbind();"} />

<H2>Your own shortcuts</H2>

<p class="text-sm text-muted-foreground mb-3"> By default the shared listener is <strong>typing-safe</strong>: a key pressed while an <code>&lt;input&gt;</code>, <code>&lt;textarea&gt;</code>, <code>&lt;select&gt;</code> or <code>contenteditable</code> element has focus is <em>never handed to any handler</em>. That is deliberate - otherwise a <kbd>?</kbd> or <kbd>/</kbd> typed into a search box would open help or jump focus instead of being typed. The catch: with the default, <kbd>?</kbd> and <kbd>/</kbd> simply do nothing while the field has focus. </p>

<p class="text-sm text-muted-foreground mb-3"> When a shortcut <em>should</em> work inside a field, the handler opts in per registration with <code>{"{ editable: true }"}</code> - and then owns the decision itself: <code>df$.shadcn.shared.isEditableTarget(event)</code> tells it the key came from a field, and it lets ordinary typing through. The example below does exactly that: </p>

<ul class="text-sm text-muted-foreground mb-5 ps-5">
  <li>outside the field: <kbd>?</kbd> opens help, <kbd>/</kbd> focuses the search</li>
  <li>in the <strong>empty</strong> field: <kbd>?</kbd> still opens help (a lone <kbd>?</kbd> is not a search), <kbd>/</kbd> is ignored (you are already there)</li>
  <li>in a field <strong>with text</strong>: both are just characters - the handler returns early and the browser types them</li>
  <li><kbd>Esc</kbd> in the field leaves it (blur), so <kbd>?</kbd> / <kbd>/</kbd> work again right away</li>
</ul>

<CodeWindow lang="javascript" title="opting in to keys typed into fields" mb="2.5rem" code={"const { bindGlobalKeys, isEditableTarget } = df$.shadcn.shared;\n\nbindGlobalKeys((event) => {\n  const inField = isEditableTarget(event);\n  if (inField && event.target.value !== '' && event.key !== 'Escape') return; // typing - hands off\n  if (event.key === '?') { event.preventDefault(); openHelp(); return true; }\n  if (event.key === '/' && !inField) { event.preventDefault(); search.focus(); return true; }\n  if (event.key === 'Escape' && inField) { event.target.blur(); return true; }\n}, { editable: true }); // ← without this, keys typed into fields never arrive here"} />

```html example schema="none" label="Help on ?, search on /" hint="One bindGlobalKeys handler registered with { editable: true }: ? opens help everywhere except inside a field that already holds text, / jumps into the search, Esc leaves the field. Click into the example first - keyboard focus has to be inside it." height="16"
<div class="flex flex-col gap-3" style="max-width: 24rem; outline: none" tabindex="-1" id="keys-demo">
  <p class="text-sm m-0">Press <kbd>?</kbd> for help or <kbd>/</kbd> to search.</p>
  <input class="input" type="search" id="keys-search" placeholder="Search… (Esc to leave)" aria-label="Search" />
  <p class="text-sm text-muted-foreground m-0">Last shortcut: <output id="keys-last">none</output></p>
</div>
<dialog class="dialog" id="keys-help" aria-labelledby="keys-help-title">
  <div class="dialog-content">
    <div class="dialog-header">
      <h2 class="dialog-title" id="keys-help-title">Keyboard shortcuts</h2>
      <p class="dialog-description"
        ><kbd>?</kbd> this help · <kbd>/</kbd> focus search · <kbd>Esc</kbd> leave the field / close</p
      >
    </div>
    <div class="dialog-footer">
      <button class="btn" data-variant="default" data-dialog-close>Got it</button>
    </div>
  </div>
</dialog>
<script type="module">
  const { bindGlobalKeys, isEditableTarget } = df$.shadcn.shared;
  // df$ selects (get(0) = the element); focus() and showModal() stay native
  const search = df$('#keys-search').get(0);
  const last = df$('#keys-last');
  bindGlobalKeys(
    (event) => {
      const inField = isEditableTarget(event);
      // a field that holds text: ? and / are characters - let the browser type them
      if (inField && event.target.value !== '' && event.key !== 'Escape') return;
      if (event.key === '?') {
        event.preventDefault(); // keep a ? out of the empty field
        df$('#keys-help').get(0).showModal();
        last.val(inField ? '? - help (from the empty field)' : '? - help');
        return true;
      }
      if (event.key === '/' && !inField) {
        event.preventDefault(); // or the "/" would be typed into the field we focus
        search.focus();
        last.val('/ - search');
        return true;
      }
      if (event.key === 'Escape' && inField) {
        event.target.blur();
        df$('#keys-demo').get(0).focus();
        last.val('Esc - left the field');
        return true;
      }
    },
    { editable: true },
  );
  df$('#keys-demo').get(0).focus({ focusVisible: false });
</script>
```

<H2>Shortcuts the components ship</H2>

<TableCard title="built-in global keys" mb="1.5rem" rows={[
  ['Component', 'Keys', 'Listener'],
  ['Sidebar', '⌘B / Ctrl+B - dock / undock the first .app-sidebar', 'shared (bindGlobalKeys)'],
  ['Presentation', '→ PageDown Space next · ← PageUp previous · Home End first / last · N notes · F fullscreen', 'shared (bindGlobalKeys)'],
  ['Animation Canvas', '← → ↑ ↓ to the neighbouring slide · O / Esc overview', 'shared (bindGlobalKeys)'],
  ['Command', '⌘K / Ctrl+K - open / close the command palette', 'shared, { editable: true } - ⌘K must work while typing in a field (the palette input too)'],
]} />

<p class="text-sm text-muted-foreground mb-5"> Components only take a key when it is theirs - the sidebar only claims <kbd>⌘B</kbd> when the page has a sidebar, a deck only claims arrow keys when it has a slide to go to - so the browser's own shortcuts keep working everywhere else. </p>

<H2>Good shortcuts</H2>

<ul class="text-sm text-muted-foreground mb-5 ps-5">
  <li>Return <code>true</code> only when you actually handled the key, and call <code>preventDefault()</code> then - never for keys you ignore.</li>
  <li>Prefer modifier shortcuts (<kbd>⌘</kbd>/<kbd>Ctrl</kbd>+key) for page-wide actions. A single-character shortcut (<kbd>?</kbd>, <kbd>/</kbd>, <kbd>N</kbd>) must be possible to turn off or remap, or only be active while its widget has focus (WCAG 2.1.4, Character Key Shortcuts) - speech-input users otherwise trigger it by accident.</li>
  <li>List your shortcuts somewhere visible - a help dialog on <kbd>?</kbd> is the common convention.</li>
  <li>Unbind when the view that owns the shortcut goes away (an SPA route change, a closed panel).</li>
</ul>

</DocPage>
