From 2e266e1b8f6563b175199c34bb7469c2df5984a9 Mon Sep 17 00:00:00 2001 From: "D. Elmo Peele" Date: Thu, 26 Sep 2024 13:57:07 -0400 Subject: [PATCH 1/5] Update useInput() to include `name` in `key` Some key values (e.g. 'insert', 'home', 'end') were basically lost. In these cases, the `input` value would be an empty string and the generated `key` data would contain no additional information about the value. To resolve this, directly include `keypress.name` in the generated `key` data so this information is not lost. --- src/hooks/use-input.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hooks/use-input.ts b/src/hooks/use-input.ts index a636d688..23ba110e 100644 --- a/src/hooks/use-input.ts +++ b/src/hooks/use-input.ts @@ -140,6 +140,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', From 62f1093ce5593279201aaa8510b9e0d3bf9ce110 Mon Sep 17 00:00:00 2001 From: "D. Elmo Peele" Date: Thu, 26 Sep 2024 16:15:04 -0400 Subject: [PATCH 2/5] Update types / docs. Update the `Key` type in src/hooks/use-input.ts to include the new `name` field. Update `useInput()` documentation for theh `inputHandler()` callback to include a description of the new `key.name`. --- readme.md | 21 +++++++++++++++++++++ src/hooks/use-input.ts | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/readme.md b/readme.md index 71cd8077..63e937a6 100644 --- a/readme.md +++ b/readme.md @@ -1459,6 +1459,27 @@ Type: `object` Handy information about a key that was pressed. +###### key.name + +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 + + ###### key.leftArrow ###### key.rightArrow diff --git a/src/hooks/use-input.ts b/src/hooks/use-input.ts index 23ba110e..5ab7b841 100644 --- a/src/hooks/use-input.ts +++ b/src/hooks/use-input.ts @@ -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. */ From 7b12d04842b8586cb6e62047041dd5d28a8481c5 Mon Sep 17 00:00:00 2001 From: "D. Elmo Peele" Date: Thu, 26 Sep 2024 16:23:20 -0400 Subject: [PATCH 3/5] Update handleData() to use `input` if `key.name` is empty. For cases where `parseKeypress()` does not provide `keypress.name` *and* `input` is not empty, set `keypress.name` to `input`. --- src/hooks/use-input.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hooks/use-input.ts b/src/hooks/use-input.ts index 5ab7b841..550d4137 100644 --- a/src/hooks/use-input.ts +++ b/src/hooks/use-input.ts @@ -178,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' && From 69526ab8e8442a886c9f982f437bd3a5538cf37b Mon Sep 17 00:00:00 2001 From: "D. Elmo Peele" Date: Fri, 15 Nov 2024 12:44:44 -0500 Subject: [PATCH 4/5] Address request from @sindresorhus --- readme.md | 24 +++++++++++++++--------- src/hooks/use-input.ts | 26 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index 63e937a6..238a9a3a 100644 --- a/readme.md +++ b/readme.md @@ -1469,15 +1469,21 @@ 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 +- `f1` - `f12` +- `escape` +- `number` (for all number keys 0-9) +- `return` +- `up` (arrow keys) +- `down` (arrow keys) +- `left` (arrow keys) +- `right` (arrow keys) +- `insert` +- `delete` +- `home` +- `end` +- `pageup` +- `pagedown` +- `space` ###### key.leftArrow diff --git a/src/hooks/use-input.ts b/src/hooks/use-input.ts index 550d4137..a27ed40e 100644 --- a/src/hooks/use-input.ts +++ b/src/hooks/use-input.ts @@ -8,7 +8,31 @@ import useStdin from './use-stdin.js'; */ export type Key = { /** - * The name of the key that was pressed. + * 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` (arrow keys) + * - `down` (arrow keys) + * - `left` (arrow keys) + * - `right` (arrow keys) + * - `insert` + * - `delete` + * - `home` + * - `end` + * - `pageup` + * - `pagedown` + * - `space` */ name: string; From daba8616ea1924880792d0231cf93e875b6396ee Mon Sep 17 00:00:00 2001 From: "D. Elmo Peele" Date: Fri, 15 Nov 2024 12:48:12 -0500 Subject: [PATCH 5/5] Convert white-space to tabs to match source. --- src/hooks/use-input.ts | 50 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/hooks/use-input.ts b/src/hooks/use-input.ts index a27ed40e..9b16b43e 100644 --- a/src/hooks/use-input.ts +++ b/src/hooks/use-input.ts @@ -8,31 +8,31 @@ import useStdin from './use-stdin.js'; */ export type Key = { /** - * 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` (arrow keys) - * - `down` (arrow keys) - * - `left` (arrow keys) - * - `right` (arrow keys) - * - `insert` - * - `delete` - * - `home` - * - `end` - * - `pageup` - * - `pagedown` - * - `space` + * 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` (arrow keys) + * - `down` (arrow keys) + * - `left` (arrow keys) + * - `right` (arrow keys) + * - `insert` + * - `delete` + * - `home` + * - `end` + * - `pageup` + * - `pagedown` + * - `space` */ name: string;