Skip to content
Browse files

node < v0.8.x support

  • Loading branch information...
1 parent 54c0e79 commit 0702ef8c86cc066476fe69aa0ca04fb031543339 @TooTallNate committed Jul 6, 2012
Showing with 33 additions and 2 deletions.
  1. +29 −1 index.js
  2. +4 −1 test.js
View
30 index.js
@@ -23,7 +23,7 @@ module.exports = keypress;
*/
function keypress(stream) {
- if (stream._emitKeypress) return;
+ if (isEmittingKeypress(stream)) return;
stream._emitKeypress = true;
function onData(b) {
@@ -50,6 +50,34 @@ function keypress(stream) {
}
}
+/**
+ * Returns `true` if the stream is already emitting "keypress" events.
+ * `false` otherwise.
+ */
+
+function isEmittingKeypress(stream) {
+ var rtn = stream._emitKeypress;
+ if (!rtn) {
+ // hack: check for the v0.6.x "data" event
+ stream.listeners('data').forEach(function (l) {
+ if (l.name == 'onData' && /emitKey/.test(l.toString())) {
+ rtn = true;
+ stream._emitKeypress = true;
+ }
+ });
+ }
+ if (!rtn) {
+ // hack: check for the v0.6.x "newListener" event
+ stream.listeners('newListener').forEach(function (l) {
+ if (l.name == 'onNewListener' && /keypress/.test(l.toString())) {
+ rtn = true;
+ stream._emitKeypress = true;
+ }
+ });
+ }
+ return rtn;
+}
+
/*
Some patterns seen in terminal key escape codes, derived from combos seen
View
5 test.js
@@ -1,7 +1,10 @@
require('./')(process.stdin)
-process.stdin.setRawMode(true)
+if (process.stdin.setRawMode)
+ process.stdin.setRawMode(true)
+else
+ require('tty').setRawMode(true)
process.stdin.on('keypress', function (c, key) {
console.log(0, c, key)

0 comments on commit 0702ef8

Please sign in to comment.
Something went wrong with that request. Please try again.