blob: bead50e6697a2081bb3ccc3ba4acf06dca807a09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { useEffect } from 'react';
import {
useKeypressContext,
KeypressHandler,
Key,
} from '../contexts/KeypressContext.js';
export { Key };
/**
* A hook that listens for keypress events from stdin.
*
* @param onKeypress - The callback function to execute on each keypress.
* @param options - Options to control the hook's behavior.
* @param options.isActive - Whether the hook should be actively listening for input.
*/
export function useKeypress(
onKeypress: KeypressHandler,
{ isActive }: { isActive: boolean },
) {
const { subscribe, unsubscribe } = useKeypressContext();
useEffect(() => {
if (!isActive) {
return;
}
subscribe(onKeypress);
return () => {
unsubscribe(onKeypress);
};
}, [isActive, onKeypress, subscribe, unsubscribe]);
}
|