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

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

Closed
arve0 opened this issue May 7, 2016 · 7 comments
Closed
Labels
doc Issues and PRs related to the documentations. readline Issues and PRs related to the built-in readline module.

Comments

@arve0
Copy link
Contributor

arve0 commented May 7, 2016

  • 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.

@addaleax addaleax added the readline Issues and PRs related to the built-in readline module. label May 7, 2016
@addaleax
Copy link
Member

addaleax commented May 7, 2016

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

@arve0
Copy link
Contributor Author

arve0 commented May 7, 2016

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

@addaleax
Copy link
Member

addaleax commented May 7, 2016

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?

@arve0
Copy link
Contributor Author

arve0 commented May 7, 2016

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?

@arve0
Copy link
Contributor Author

arve0 commented May 7, 2016

To be more clear, this is needed in raw mode

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

but not when using a readline interface.

@addaleax
Copy link
Member

addaleax commented May 7, 2016

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.

@arve0
Copy link
Contributor Author

arve0 commented May 7, 2016

Aha, thanks!

@arve0 arve0 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 May 7, 2016
arve0 added a commit to arve0/node that referenced this issue May 12, 2016
`readline.emitKeypressEvents` needs `stream` to be in raw mode, ref nodejs#6626
evanlucas pushed a commit that referenced this issue May 17, 2016
`readline.emitKeypressEvents` needs `stream` to be in raw mode.

PR-URL: #6628
Fixes: #6626
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Roman Klauke <[email protected]>
@sam-github sam-github added doc Issues and PRs related to the documentations. and removed doc Issues and PRs related to the documentations. labels Dec 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. readline Issues and PRs related to the built-in readline module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants