Skip to content

Document that readline.emitKeypressEvents only works when tty is in raw mode #6626

Closed

Description

@arve0
  • Version: 6.0.0
  • Platform: osx 10.11.4, uname -a: 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64
  • Subsystem: readline.emitKeypressEvents

process.stdin does not emit keypress after readline.emitKeypressEvents(process.stdin), but does when a readline interface created.

const readline = require('readline')

// const rl = readline.createInterface({
//  input: process.stdin,
//  output: process.stdout
// })
readline.emitKeypressEvents(process.stdin)
process.stdin.on('keypress', () => {
    process.stdout.write('.')
})

What happens: On keypress, event handler is not called. If a readline interface is created, the event handler is called.
Expected: stdin to start emitting keypress-events.

Activity

  1. added
    readlineIssues and PRs related to the built-in readline module.
    on May 7, 2016
  2. addaleax commented on May 7, 2016

    @addaleax
    Member

    Does setting process.stdin.setRawMode(true); help you?

  3. arve0 commented on May 7, 2016

    @arve0
    ContributorAuthor

    @addaleax Yes, it does. The event handler is called, and terminal echo turns off.

  4. addaleax commented on May 7, 2016

    @addaleax
    Member

    That is expected in raw mode, yes. I am not sure whether there’s another way to make process.stdin unbuffered/return characters as they are typed…

    Is your question answered by this?

  5. arve0 commented on May 7, 2016

    @arve0
    ContributorAuthor

    Kind of. I guess the documentation should state that raw mode or a readline interface is needed.

    A drawback with raw mode though, is that SIGINT is disabled. Any way around that, without implementing it oneself?

    This does not seem to work:

    process.stdin.on('keypress', (str, key) => {
        if (key.ctrl && key.name === 'c') {
            process.emit('SIGINT')
        }
    })

    Any idea why?

  6. arve0 commented on May 7, 2016

    @arve0
    ContributorAuthor

    To be more clear, this is needed in raw mode

    process.on('SIGINT', () => {
        process.exit()
    })

    but not when using a readline interface.

  7. addaleax commented on May 7, 2016

    @addaleax
    Member

    I guess the documentation should state that raw mode or a readline interface is needed.

    Yeah, I guess so… The readline interface sets raw mode itself if the input is a terminal, that’s why that’s working.

    To be more clear, this is needed in raw mode

    If you “just” want as close to the original Ctrl+C behaviour as possible, you may want to use process.kill(process.pid, 'SIGINT') rather than process.emit('SIGINT'). The latter one does not actually send the signal, just emits the event to any JS listeners for it.

  8. arve0 commented on May 7, 2016

    @arve0
    ContributorAuthor

    Aha, thanks!

  9. changed the title [-]readline.emitKeypressEvents only works when a readline interface is created[/-] [+]Document that readline.emitKeypressEvents only works when tty is in raw mode[/+] on May 7, 2016
  10. added
    docIssues and PRs related to Node.js documentation.
    and removed
    docIssues and PRs related to Node.js documentation.
    on Dec 1, 2016
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

    docIssues and PRs related to Node.js documentation.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