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

on Windows10 only: stdin keypress event lost after escape pressed #38663

Closed
ui3o opened this issue May 13, 2021 · 9 comments
Closed

on Windows10 only: stdin keypress event lost after escape pressed #38663

ui3o opened this issue May 13, 2021 · 9 comments
Labels
readline Issues and PRs related to the built-in readline module. windows Issues and PRs related to the Windows platform.

Comments

@ui3o
Copy link

ui3o commented May 13, 2021

Description

After escape pressed and close the keypress event listener the second key press will be lost!

  • Version: v16.1.0:
  • Platform: Windows 10:

What steps will reproduce the bug?

const readline = require('readline');

async function keytester(index) {
    console.log(`keytester ${index}`)
    return new Promise(res => {
        const rl = readline.createInterface({ input: process.stdin, escapeCodeTimeout: 50 });

        readline.emitKeypressEvents(process.stdin, rl);

        if (process.stdin.isTTY)
            process.stdin.setRawMode(true);

        const kpl = (chunk, key) => {
            console.log(key.name);
            if (key && (key.name == 'escape' || key.name == 'return')) {
                process.stdin.removeListener('keypress', kpl);
                if (process.stdin.isTTY)
                    process.stdin.setRawMode(false);
                rl.close();
                res();
            }
        };
        process.stdin.on('keypress', kpl);
    });
}
async function test() {
    await keytester(1);
    await keytester(2);
    await keytester(3);
}
test();
  • Run the program above on windows in standard terminal
  • After the program start press esc
  • Second key need to be return
  • Third return will be lost!

What is the expected behavior?

  • Third return have to be catched in keypress handler!
@Ayase-252
Copy link
Member

Ayase-252 commented May 13, 2021

Unable to reproduce in MacOS, is it a windows-specific issue?

Outputs

➜  node -v       
v16.1.0
➜  node index.js 
keytester 1
escape
keytester 2
return
keytester 3
return

@ui3o
Copy link
Author

ui3o commented May 13, 2021

Yes it is! I tried on linux also and there is no issue.

@ui3o ui3o changed the title stdin keypress event lost after escape pressed on Windows10: stdin keypress event lost after escape pressed May 13, 2021
@ui3o ui3o changed the title on Windows10: stdin keypress event lost after escape pressed on Windows10 only: stdin keypress event lost after escape pressed May 13, 2021
@dulyts
Copy link

dulyts commented May 13, 2021

Almost same on windows 10 with node 12.16.1 and 12.16.2, after escape doesn't catch any enter.

c:\tmp>node -v
v12.16.1

c:\tmp>node node_issue.js
keytester 1
escape

With 14.17.0 first enter catched but second not:

c:\tmp>node -v
v14.17.0

c:\tmp>node node_issue.js
keytester 1
escape
keytester 2
return
keytester 3

@Ayase-252 Ayase-252 added readline Issues and PRs related to the built-in readline module. windows Issues and PRs related to the Windows platform. labels May 13, 2021
@helio-frota
Copy link
Contributor

helio-frota commented Jun 8, 2021

yeah I tested last week and I saw the same weird behavior

C:\Users\test1\code
λ node --version
v12.19.0

C:\Users\test1\code
λ node test.js
keytester 1
return | meta: false
keytester 2
return | meta: false
keytester 3
return | meta: false

C:\Users\test1\code
λ node test.js
keytester 1
escape | meta: true
keytester 2
return | meta: false
keytester 3

return | meta: false

C:\Users\test1\code

@jehincastic
Copy link

I have tried the above mentioned issued with a small change and it works on Windows fine but I don't know why

Node Version => v16.4.2

Code:

const readline = require('readline');

async function keytester(index) {
  console.log(`keytester ${index}`)
  return new Promise(res => {
    const rl = readline.createInterface({
      input: process.stdin,
      escapeCodeTimeout: 50
    });

    readline.emitKeypressEvents(process.stdin, rl);

    if (process.stdin.isTTY)
      process.stdin.setRawMode(true);

    const kpl = (chunk, key) => {
      console.log(key.name);
      if (key && (key.name == 'escape' || key.name == 'return')) {
        process.stdin.removeListener('keypress', kpl);
        // if (process.stdin.isTTY)
        //   process.stdin.setRawMode(false);
        rl.close();
        res();
      }
    };
    process.stdin.on('keypress', kpl);
  });
}
async function test() {
  await keytester(1);
  await keytester(2);
  await keytester(3);
}
test();

@ui3o
Copy link
Author

ui3o commented Jul 10, 2021

If you do not call process.stdin.setRawMode(false); and do something normal work between the keytester(x); methods, you can not terminate the normal program.

CTRL+C will not effect. See here: https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode

@0x00032
Copy link

0x00032 commented Jul 24, 2021

Tested on windows Node v14.17.0
the first enter cached but the second one wasnt

keytester 1
escape
keytester 2
return
keytester 3

@jehincastic
Copy link

If you do not call process.stdin.setRawMode(false); and do something normal work between the keytester(x); methods, you can not terminate the normal program.

CTRL+C will not effect. See here: https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode

Thank you for the clarification.
My point being that without these two lines the code works perfectly.

This works fine too

await keytester(1);
if (process.stdin.isTTY)
  process.stdin.setRawMode(false);
await keytester(2);
if (process.stdin.isTTY)
  process.stdin.setRawMode(false);
await keytester(3);
if (process.stdin.isTTY)
  process.stdin.setRawMode(false);

@StefanStojanovic
Copy link
Contributor

This issue has been stale for a long time, and there is a suggested workaround in the comments. I will close it now, but if you face similar issue feel free to reopen it or open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
readline Issues and PRs related to the built-in readline module. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

7 participants