Skip to content

Instantly share code, notes, and snippets.

View danielgary's full-sized avatar

Daniel Gary danielgary

  • Gateway Apps
  • Jonesboro, AR
View GitHub Profile
@danielgary
danielgary / useKeyboardShortcut.ts
Created July 19, 2021 18:05
React Hook for Control + Key
import { useCallback, useEffect, useState } from 'react'
export const useKeyboardShortcut = (key: string, callback: (keyEvent: KeyboardEvent) => void) => {
const [pressed, setPressed] = useState(false)
const keyPressHandler = useCallback(
(ev: KeyboardEvent) => {
if (ev.ctrlKey || ev.metaKey) {
if (ev.key.toLowerCase() === key.toLowerCase()) {
ev.preventDefault()