Skip to content

Readline: Function keys (e.g. F1) return "undefined" key name #35251

@elie-g

Description

@elie-g

What steps will reproduce the bug?

  1. Execute the code below in a terminal.
  2. Press Alt+F3 (see Additionnal information).
const { StringDecoder } = require('string_decoder')
const { emitKeypressEvents } = require('readline')

const stdin = process.openStdin();
emitKeypressEvents(stdin);
if (stdin.isTTY) {
  console.log('Standard input is a TTY');
  stdin.setRawMode(true);
}

const strDecoder = new StringDecoder('utf8');

const escapeEsc = (str) => str?.replace(/\x1b/g, 'ESC');

stdin.on('keypress', (str, key) => {
  // key.name === "undefined" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE
  console.log('KEYPRESS => data: «%o», key: %o', escapeEsc(str), key);
});

stdin.on('data', (data) => {
  const str = strDecoder.write(data);
  console.log('DATA => decoded: «%s», raw: %o', escapeEsc(str), data);
});

How often does it reproduce? Is there a required condition?

Every time!

What is the expected behavior?

key.name should contain the name of the function key (e.g. "f1").
See Additional information.

What do you see instead?

key.name contains "undefined" (as a string / not undefined).
See Additional information.

Output after pressing Alt + F3
webstorm-output

Additional information

Tested key combinations (expected/current result)
Key combo Expected
key.name
Current
key.name
Matches
expectation?
Alt + F1 "f1" ??? ???
Alt + F2 "f2" ??? ???
Alt + F3 "f3" "undefined" N
Alt + F4 "f4" ??? ???
Alt + F5 "f5" "f5" Y
Alt + F6 "f6" ??? ???
Alt + F7 "f7" "f7" Y
Alt + F8 "f8" ??? ???
Alt + F9 "f9" "f9" Y
Alt + F10 "f10" "f10" Y
Alt + F11 "f11" ??? ???
Alt + F12 "f12" "f12" Y
Alt + Shift + F1 "f1" "undefined" N
Alt + Shift + F2 "f2" ??? ???
Alt + Shift + F3 "f3" "undefined" N
Alt + Shift + F4 "f4" "undefined" N
Alt + Shift + F5 "f5" "f5" Y
Alt + Shift + F6 "f6" ??? ???
Alt + Shift + F7 "f7" "f7" Y
Alt + Shift + F8 "f8" "f8" Y
Alt + Shift + F9 "f9" "f9" Y
Alt + Shift + F10 "f10" ??? ???
Alt + Shift + F11 "f11" ??? ???
Alt + Shift + F12 "f12" "f12" Y
Ctrl + F1 "f1" "undefined" N
Ctrl + F2 "f2" ??? ???
Ctrl + F3 "f3" "undefined" N
Ctrl + F4 "f4" "undefined" N
Ctrl + F5 "f5" "f5" Y
Ctrl + F6 "f6" "f6" Y
Ctrl + F7 "f7" "f7" Y
Ctrl + F8 "f8" "f8" Y
Ctrl + F9 "f9" "f9" Y
Ctrl + F10 "f10" "f10" Y
Ctrl + F11 "11" ??? ???
Ctrl + F12 "f12" "f12" Y
Ctrl + Shift + F1 "f1" "undefined" N
Ctrl + Shift + F2 "f2" ??? ???
Ctrl + Shift + F3 "f3" "undefined" N
Ctrl + Shift + F4 "f4" "undefined" N
Ctrl + Shift + F5 "f5" "f5" Y
Ctrl + Shift + F6 "f6" "f6" Y
Ctrl + Shift + F7 "f7" "f7" Y
Ctrl + Shift + F8 "f8" "f8" Y
Ctrl + Shift + F9 "f9" "f9" Y
Ctrl + Shift + F10 "f10" ??? ???
Ctrl + Shift + F11 "11" ??? ???
Ctrl + Shift + F12 "f12" "f12" Y

The key combinations where the 3rd and 4th columns contain ??? could not be tested cause they were already bound to something else like Alt + F4 which closes the application.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions