useKeyModifier
React Sensor Hook that tracks state of any of the supported modifiers - see Browser Compatibility notes
useKeyModifier uses KeyboardEvent.getModifierState() to reactively track whether a specific modifier key (Shift, Control, Alt, CapsLock, Meta, etc.) is currently pressed. It returns a boolean that updates on keydown, keyup, mousedown, and mouseup events. You can customize which events trigger updates and set an initial value.
When to Use
- Building keyboard shortcut indicators that visually highlight when modifier keys are held
- Showing different tooltips or cursor styles based on active modifier keys
- Implementing modifier-aware interactions (e.g., Ctrl+Click for multi-select)
Notes
- SSR-safe: Returns the
initialvalue (default:false) during server-side rendering. No event listeners are attached on the server. - Supported modifiers: Supports
Alt,AltGraph,CapsLock,Control,Fn,FnLock,Meta,NumLock,ScrollLock,Shift,Symbol, andSymbolLock. Not all modifiers are available in all browsers — see MDN compatibility. - Event customization: The default events (
mousedown,mouseup,keydown,keyup) can be overridden via theeventsoption.
Usage
Live Editor
function Demo() { const Button = (props: { mode: boolean; name: string }) => { const { mode, name } = props; return ( <button style={mode ? { background: "var(--c-input-border-focus)" } : {}}> {name} </button> ); }; const capsLock = useKeyModifier("CapsLock"); const numLock = useKeyModifier("NumLock"); const scrollLock = useKeyModifier("ScrollLock"); const shift = useKeyModifier("Shift"); const control = useKeyModifier("Control"); const alt = useKeyModifier("Alt"); return ( <div> <div style={{ marginBottom: 10 }}> <Button mode={capsLock} name="CapsLock" /> <Button mode={numLock} name="NumLock" /> <Button mode={scrollLock} name="ScrollLock" /> </div> <div> <Button mode={shift} name="Shift" /> <Button mode={control} name="Control" /> <Button mode={alt} name="Alt" /> </div> </div> ); };
Result
API
useKeyModifier
Returns
boolean: Whether the key is pressed
Arguments
| Argument | Description | Type | DefaultValue |
|---|---|---|---|
| modifier | key modifier | KeyModifier (Required) | - |
| options | optional params | UseModifierOptions | undefined | - |
UseModifierOptions
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| events | Event names that will prompt update to modifier states | (keyof WindowEventMap)[] | ['mousedown', 'mouseup', 'keydown', 'keyup'] |
| initial | Initial value of the returned ref | boolean | false |
KeyModifier
export type KeyModifier = 'Alt' | 'AltGraph' | 'CapsLock' | 'Control' | 'Fn' | 'FnLock' | 'Meta' | 'NumLock' | 'ScrollLock' | 'Shift' | 'Symbol' | 'SymbolLock';