Skip to content

getKeyLockState

ts
function getKeyLockState(key): SvelteKeyLockState;

Defined in: packages/svelte-hotkeys/src/getKeyLockState.svelte.ts:54

Svelte function that returns reactive access to 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. Its value is 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

SvelteKeyLockState

Object with a reactive value property: true when the lock is on, false when it is off, or null

Example

svelte
<script>
  import { getKeyLockState } from '@tanstack/svelte-hotkeys'

  const capsLock = getKeyLockState('CapsLock')
</script>

<label>
  Password
  <input type="password" />
  {#if capsLock.value}
    <span role="status">Caps Lock is on</span>
  {/if}
</label>