Skip to content

Readline keypress event is triggered only after pressing Esc key 3 times #7379

Description

@sindresorhus
  • Version: 6.2.2 and 4.4.5
  • Platform: macOS 10.11.5
  • Subsystem: readline

Test case

const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', console.log);

Expected

After pressing Esc once, I would get this output:

undefined { sequence: '\u001b',
  name: 'escape',
  ctrl: false,
  meta: true,
  shift: false }

Actual

After pressing Esc three times, I get this output:

undefined { sequence: '\u001b\u001b\u001b',
  name: 'escape',
  ctrl: false,
  meta: true,
  shift: false }

It works fine on 0.10.44 and 0.12.13. After pressing Esc once:

{ name: 'escape',
  ctrl: false,
  meta: false,
  shift: false,
  sequence: '\u001b' }

Also notice that meta is false here.

Activity

  1. added a commit that references this issue on Jun 23, 2016
  2. added
    confirmed-bugIssues and PRs for confirmed bugs.
    readlineIssues and PRs related to the built-in readline module.
    on Jun 23, 2016
  3. self-assigned this
    on Jun 23, 2016
  4. MylesBorins commented on Jun 23, 2016

    @MylesBorins
    Contributor

    I've dug into this a bit and can confirm the bug exists in v4.x and v5.x

    It would appear to have been introduced in v2.0.2 I'll dig into that release and see where it came from

  5. MylesBorins commented on Jun 23, 2016

    @MylesBorins
    Contributor

    So it would appear that escape started acting weird after aed6bce906 landed.

    Doesn't appear to be easily revertable... I'm digging into what is going on with escape

  6. MylesBorins commented on Jun 23, 2016

    @MylesBorins
    Contributor

    Found the offending section

    https://github.com/nodejs/node/blob/master/lib/internal/readline.js#L144-L151

    The original PR introduced this logic which yields to more results if the first character is an escape... which is problematic if your entire payload is an escape. It allows for double escaped characters, which is why things work on the third press

    Not sure the best way to approach this, but I'll revisit in the morning

  7. princejwesley commented on Jun 23, 2016

    @princejwesley
    Contributor

    @thealphanerd It was mentioned in the PR itself.

  8. MylesBorins commented on Jun 23, 2016

    @MylesBorins
    Contributor

    @princejwesley sigh... I wish I had read the pr instead of just the commit and then digging into the code.

    @cjihrig you have the blame on most of internal/readline.js would you specify this as a wont-fix rather than as a bug?

  9. princejwesley commented on Jun 23, 2016

    @princejwesley
    Contributor

    @thealphanerd I'll fix it!

  10. 3 remaining items

  11. silverwind commented on Jun 23, 2016

    @silverwind
    Contributor

    @cjihrig I accepted that drawback as the primary use of esc in a terminal is to start an escape sequence, but I now see the issue with applications relying on that key.

    @princejwesley I guess a timeout-based approach to triggering the key is the way to go. I just don't know if 700ms is the correct value, seems a tad high.

  12. princejwesley commented on Jun 23, 2016

    @princejwesley
    Contributor

    @silverwind
    Default timeout values for zsh is 400ms and for vim & ncurses, its 1sec. @sindresorhus suggested to kepp the middle value between 400 - 1000. Shall I update with the middle value of '400 - 700' (550ms) or 400ms? One second is too high.

  13. silverwind commented on Jun 23, 2016

    @silverwind
    Contributor

    How about 500ms, like in GNU readline (http://man7.org/linux/man-pages/man3/readline.3.html)?

  14. princejwesley commented on Jun 23, 2016

    @princejwesley
    Contributor
  15. added a commit that references this issue on Aug 13, 2016
    ccfc2c4
  16. MylesBorins commented on Sep 30, 2016

    @MylesBorins
    Contributor

    This has been backported to v4.x in 68a2979

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions