Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update useInput() to include name in key #674

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,27 @@ Type: `object`

Handy information about a key that was pressed.

###### key.name
depeele marked this conversation as resolved.
Show resolved Hide resolved

Type: `string`

The interpreted name of the key that was pressed. This is primarily useful for meta keys (e.g. Home, End, Insert).
For example, if the user presses the Home key, `key.name` will be set to `home`.

For non-meta keys, the `key.name` will be equivalent to the lower-cased version of `input`.

Currently identified meta keys:
- f1 - f12
- escape
- number (for all number keys 0-9)
- return
- up, down, left, right (arrow keys)
- insert, delete
- home, end
- pageup, pagedown
- space
depeele marked this conversation as resolved.
Show resolved Hide resolved


###### key.leftArrow

###### key.rightArrow
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import useStdin from './use-stdin.js';
* Handy information about a key that was pressed.
*/
export type Key = {
/**
* The name of the key that was pressed.
*/
name: string;

/**
* Up arrow key was pressed.
*/
Expand Down Expand Up @@ -140,6 +145,7 @@ const useInput = (inputHandler: Handler, options: Options = {}) => {
const keypress = parseKeypress(data);

const key = {
name: keypress.name,
upArrow: keypress.name === 'up',
downArrow: keypress.name === 'down',
leftArrow: keypress.name === 'left',
Expand Down Expand Up @@ -172,6 +178,10 @@ const useInput = (inputHandler: Handler, options: Options = {}) => {
input = input.slice(1);
}

if (!key.name && input) {
key.name = input;
}

if (
input.length === 1 &&
typeof input[0] === 'string' &&
Expand Down