1.0.2 • Published 1 year ago

@vulcancreative/keyboard-event-bus v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Keyboard Event Bus

A lightweight package for handling keyboard events and triggering user-defined actions in React applications

Demo

https://vulcancreative.github.io/keyboard-event-bus/

Instalation

npm install @vulcancreative/keyboard-event-bus

or

yarn add @vulcancreative/keyboard-event-bus

How to use

Wrap your React app with KeyboardEventBusProvider

import {KeyboardEventBusProvider} from "@vulcancreative/keyboard-event-bus";

<KeyboardEventBusProvider>
  <App />
</KeyboardEventBusProvider>

Inside your React app, use the useKeyboardEventBus hook to add new shortcut.

import { useKeyboardEventBus, Key } from "@vulcancreative/keyboard-event-bus";

const App = () => {
  const shortcut = useKeyboardEventBus();

  useEffect(() => {
    const removeShortcut = shortcut?.add([Key.CMD_CTRL, Key.SHIFT, Key.CHAR_K], () => {
      alert("This will show when you hit your shortcut");
    });
    return () => {
      if (removeShortcut) {
        removeShortcut();
      }
    }
  }, []);
}

The useKeyboardEventBus's add method returns a callback to remove the shortcut from the keyboard event bus list. Use it when you want to remove the shortcut, for example on component re-render.

List of available keys

KeyDescription
SHIFT_LEFTLeft Shift
SHIFT_RIGHTRight Shift
SHIFTAll Shift
TABTab
CTRL_LEFTLeft Control
CTRL_RIGHTRight Control
CTRLControl
CMD_CTRLCommand / Control (Use Command on MacOs and Control on Windows)
ALT_LEFTLeft Alt
ALT_RIGHTRight Alt
ALTAll Alt
CMD_LEFTLeft Meta / Command
CMD_RIGHTRight Meta / Command
CMDAll Meta / Command
BACKSPACEBackspace
ENTEREnter
CAPSLOCKCapsLock
CHAR_Aa
CHAR_Bb
CHAR_Cc
CHAR_Dd
CHAR_Ee
CHAR_Ff
CHAR_Gg
CHAR_Hh
CHAR_Ii
CHAR_Jj
CHAR_Kk
CHAR_Ll
CHAR_Mm
CHAR_Nn
CHAR_Oo
CHAR_Pp
CHAR_Qq
CHAR_Rr
CHAR_Ss
CHAR_Tt
CHAR_Uu
CHAR_Vv
CHAR_Ww
CHAR_Xx
CHAR_Yy
CHAR_Zz
NUM_00
NUM_11
NUM_22
NUM_33
NUM_44
NUM_55
NUM_66
NUM_77
NUM_88
NUM_99
BACKQUOTE`
SLASH/
BACKSLASH\
1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago