TanStack

useKeyLockState

ts
function useKeyLockState(key): boolean | null;

Defined in: useKeyLockState.ts:33

React hook that returns whether a lock key, such as Caps Lock, is on.

Lock keys toggle instead of being held, so this reads the lock state that the browser reports on key and mouse events. It returns null until the user presses a key, clicks, or moves the mouse, after the window loses focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS.

Parameters

key

LockKey

'CapsLock', 'NumLock', or 'ScrollLock'

Returns

boolean | null

true when the lock is on, false when it is off, or null

Example

tsx
function PasswordField() {
  const capsLock = useKeyLockState('CapsLock')

  return (
    <label>
      Password
      <input type="password" />
      {capsLock && <span role="status">Caps Lock is on</span>}
    </label>
  )
}