Keyboard shortcut
Also called hotkey or keybinding
A key combination that runs an action directly, like ⌘S to save or ⌘K to open search, so frequent users can skip the mouse.
import { useEffect, useState } from 'react';
import { Dialog } from '@base-ui/react/dialog';
const IS_MAC = /Mac|iPhone|iPad/.test(navigator.userAgent);
export function SearchShortcut() {
const [open, setOpen] = useState(false);
useEffect(() => {
function onKeyDown(event: KeyboardEvent) {
// ⌘K on a Mac, Ctrl+K everywhere else.
const mod = IS_MAC ? event.metaKey : event.ctrlKey;
if (!mod || event.key.toLowerCase() !== 'k') return;
event.preventDefault(); // only for the combination we handle
setOpen((current) => !current);
}
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, []);
return (
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger className='flex w-56 items-center justify-between rounded-lg border px-3 py-1.5 text-sm text-muted-foreground hover:bg-muted'>
Search
<kbd className='rounded border bg-muted px-1.5 font-sans text-xs'>
{IS_MAC ? '⌘' : 'Ctrl'} K
</kbd>
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Backdrop className='fixed inset-0 bg-black/30 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0' />
{/* Esc closes it; Base UI handles that. */}
<Dialog.Popup className='fixed top-24 left-1/2 w-full max-w-lg -translate-x-1/2 rounded-xl border bg-background p-2 shadow-xl transition-[opacity,scale] duration-150 data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0'>
<Dialog.Title className='sr-only'>Search</Dialog.Title>
<input
placeholder='Search projects and commands'
className='w-full bg-transparent px-3 py-2 text-sm outline-none'
/>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>
);
}Add keyboard shortcuts: ⌘K (Ctrl+K on Windows) opens the command palette, ⌘S saves the document and shows a "Saved" badge, and Esc closes any open dialog. Show ⌘ or Ctrl in menus and tooltips based on the platform, call preventDefault only for these combinations, ignore single-key shortcuts while typing in inputs, and add a "?" panel that lists every shortcut.
Paste into Layerwise or any AI coding tool.
What is Keyboard shortcut?
Keyboard shortcuts let people act without reaching for the mouse: ⌘S (Ctrl+S on Windows) saves, ⌘K opens search or a command palette, ⌘B makes text bold, and Esc closes whatever is open. They make an app faster for the people who use it every day.
Follow the conventions people already know before inventing new ones, and make shortcuts easy to discover: show them next to menu items and in tooltips, and list them all in a help panel, often opened with ?. Show ⌘ on a Mac and Ctrl everywhere else.
When building them, check metaKey on a Mac and ctrlKey elsewhere, call preventDefault() only for the combinations you handle, and ignore single-key shortcuts while focus is in a text field so typing doesn’t trigger them. Avoid combinations the browser or operating system keeps for itself, such as ⌘W and ⌘Q.
Often confused with
- Command palette
- A command palette is a searchable list of actions, often opened with ⌘K. A keyboard shortcut is any key combination that runs an action directly.
Related terms
All termsCommand palette
A search box that opens with a shortcut like ⌘K and lets you find and run any command or page by typing a few letters.
Focus ring
An outline that marks which element has keyboard focus, so people moving through a page with the Tab key can see where they are.
Tooltip
A small label that appears next to an element when you hover over it or focus it, naming an icon button or adding a short hint.
Interaction states
The set of looks a control takes as people use it, such as default, hover, pressed, focused, and disabled, each showing what will happen.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.