Comparing version 0.0.2 to 0.1.0
38
index.js
@@ -18,5 +18,13 @@ | ||
*/ | ||
var exports = module.exports = keypress; | ||
module.exports = keypress; | ||
exports.enableMouse = function (stream) { | ||
stream.write('\x1b' +'[?1000h') | ||
} | ||
exports.disableMouse = function (stream) { | ||
stream.write('\x1b' +'[?1000l') | ||
} | ||
/** | ||
@@ -301,2 +309,26 @@ * accepts a readable Stream instance and makes it emit "keypress" events | ||
if (key.code == '[M') { | ||
key.name = 'mouse'; | ||
var s = key.sequence; | ||
var b = s.charCodeAt(3); | ||
key.x = s.charCodeAt(4) - 040; | ||
key.y = s.charCodeAt(5) - 040; | ||
key.scroll = 0; | ||
key.ctrl = !!(1<<4 & b); | ||
key.meta = !!(1<<3 & b); | ||
key.shift = !!(1<<2 & b); | ||
key.release = (3 & b) === 3; | ||
if (1<<6 & b) { //scroll | ||
key.scroll = 1 & b ? 1 : -1; | ||
} | ||
if (!key.release && !key.scroll) { | ||
key.button = b & 3; | ||
} | ||
} | ||
// Don't emit a key if no name was found | ||
@@ -311,5 +343,7 @@ if (key.name === undefined) { | ||
if (key || ch) { | ||
if (key && key.name == 'mouse') { | ||
stream.emit('mousepress', key) | ||
} else if (key || ch) { | ||
stream.emit('keypress', ch, key); | ||
} | ||
} |
{ | ||
"name": "keypress", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Make any Node ReadableStream emit \"keypress\" events", | ||
@@ -5,0 +5,0 @@ "author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)", |
@@ -7,3 +7,4 @@ keypress | ||
Previous to Node `v0.8.x`, there was an undocumented `"keypress"` event that | ||
`process.stdin` would emit when it was a TTY. | ||
`process.stdin` would emit when it was a TTY. Some people discovered this hidden | ||
gem, and started using it in their own code. | ||
@@ -17,2 +18,3 @@ Now in Node `v0.8.x`, this `"keypress"` event does not get emitted by default, | ||
__Bonus:__ Now with mouse support! | ||
@@ -34,2 +36,4 @@ Installation | ||
#### Listening for "keypress" events | ||
``` js | ||
@@ -44,3 +48,3 @@ var keypress = require('keypress'); | ||
console.log('got "keypress"', key); | ||
if (key.ctrl && key.name == 'c') { | ||
if (key && key.ctrl && key.name == 'c') { | ||
process.stdin.pause(); | ||
@@ -50,6 +54,29 @@ } | ||
process.stdin.setRawMode(true); | ||
process.stdin.resume(); | ||
``` | ||
#### Listening for "mousepress" events | ||
``` js | ||
var keypress = require('keypress'); | ||
// make `process.stdin` begin emitting "mousepress" (and "keypress") events | ||
keypress(process.stdin); | ||
// you must enable the mouse events before they will begin firing | ||
keypress.enableMouse(process.stdout); | ||
process.stdin.on('mousepress', function (info) { | ||
console.log('got "mousepress" event at %d x %d', info.x, info.y); | ||
}); | ||
process.on('exit', function () { | ||
// disable mouse on exit, so that the state | ||
// is back to normal for the terminal | ||
keypress.disableMouse(process.stdout); | ||
}); | ||
``` | ||
License | ||
@@ -56,0 +83,0 @@ ------- |
17
test.js
require('./')(process.stdin) | ||
var keypress = require('./') | ||
keypress(process.stdin) | ||
@@ -11,6 +12,18 @@ if (process.stdin.setRawMode) | ||
console.log(0, c, key) | ||
if (key.ctrl && key.name == 'c') { | ||
if (key && key.ctrl && key.name == 'c') { | ||
process.stdin.pause() | ||
} | ||
}) | ||
process.stdin.on('mousepress', function (mouse) { | ||
console.log(mouse) | ||
}) | ||
keypress.enableMouse(process.stdout) | ||
process.on('exit', function () { | ||
//disable mouse on exit, so that the state is back to normal | ||
//for the terminal. | ||
keypress.disableMouse(process.stdout) | ||
}) | ||
process.stdin.resume() | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13870
313
102