diff --git a/lib/readline.js b/lib/readline.js index bd241fe93111..0276b5a0a2d7 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -804,24 +804,29 @@ function emitKeypressEvents(stream) { stream._emitKeypress = true; function onData(b) { - if (stream.listeners('keypress').length > 0) { - emitKey(stream, b); - } else { - // Nobody's watching anyway - stream.removeListener('data', onData); - stream.on('newListener', onNewListener); - } + emitKey(stream, b); } function onNewListener(event) { if (event == 'keypress') { stream.on('data', onData); + stream.on('removeListener', onRemovedListener); stream.removeListener('newListener', onNewListener); } } + + function onRemovedListener(event) { + if (event == 'keypress' && stream.listeners('keypress').length == 0) { + // Nobody's watching anyway + stream.removeListener('data', onData); + stream.removeListener('removeListener', onRemovedListener); + stream.on('newListener', onNewListener); + } + } if (stream.listeners('keypress').length > 0) { stream.on('data', onData); + stream.on('removeListener', onRemovedListener); } else { stream.on('newListener', onNewListener); }