Early access. Read the blog post

useGlobalKeyDown

A utility to quickly setup hotkeys and such.

import {useGlobalKeyDown} from '@sanity/ui'
import isHotkey from 'is-hotkey'
import React, {useCallback} from 'react'

const isSaveHotkey = isHotkey('mod+s')

function App () {
  const handleGlobalKeyDown = useCallback(
    (event: KeyboardEvent) => {
      if (isSaveHotkey(event)) {
        event.preventDefault()
        // Sanity autosaves your work!
      }
    },
    []
  )
  
  useGlobalKeyDown(handleGlobalKeyDown)
  
  return <>...</>
}

Type  

(
  callback: (event: KeyboardEvent) => void;
) => void
Updated Dec 16, 2020 @ 04:29
On this page

Made withby folks at