()
| 11 | import './index.css' |
| 12 | |
| 13 | function App() { |
| 14 | const heldKeys = useHeldKeys() |
| 15 | const heldCodes = useHeldKeyCodes() |
| 16 | |
| 17 | // Track history of key combinations |
| 18 | const [history, setHistory] = React.useState<Array<string>>([]) |
| 19 | |
| 20 | React.useEffect(() => { |
| 21 | if (heldKeys.length > 0) { |
| 22 | const combo = heldKeys |
| 23 | .map((k) => formatForDisplay(k, { useSymbols: true })) |
| 24 | .join(' + ') |
| 25 | setHistory((h) => { |
| 26 | // Only add if different from last entry |
| 27 | if (h[h.length - 1] !== combo) { |
| 28 | return [...h.slice(-9), combo] |
| 29 | } |
| 30 | return h |
| 31 | }) |
| 32 | } |
| 33 | }, [heldKeys]) |
| 34 | |
| 35 | console.log('heldKeys', heldKeys) |
| 36 | |
| 37 | return ( |
| 38 | <div className="app"> |
| 39 | <header> |
| 40 | <h1>useHeldKeys</h1> |
| 41 | <p> |
| 42 | Returns an array of all currently pressed keys. Useful for displaying |
| 43 | key combinations or building custom shortcut recording. |
| 44 | </p> |
| 45 | </header> |
| 46 | |
| 47 | <main> |
| 48 | <section className="demo-section"> |
| 49 | <h2>Currently Held Keys</h2> |
| 50 | <div className="key-display"> |
| 51 | {heldKeys.length > 0 ? ( |
| 52 | heldKeys.map((key, index) => { |
| 53 | const code = heldCodes[key] |
| 54 | return ( |
| 55 | <React.Fragment key={key}> |
| 56 | {index > 0 && <span className="plus">+</span>} |
| 57 | <kbd className="large"> |
| 58 | {formatForDisplay(key, { useSymbols: true })} |
| 59 | {code && code !== key && ( |
| 60 | <small className="code-label">{code}</small> |
| 61 | )} |
| 62 | </kbd> |
| 63 | </React.Fragment> |
| 64 | ) |
| 65 | }) |
| 66 | ) : ( |
| 67 | <span className="placeholder">Press any keys...</span> |
| 68 | )} |
| 69 | </div> |
| 70 | <div className="stats"> |
nothing calls this directly
no test coverage detected