Permalink
Browse files

add support for "mousepress" events

Squashed commit of the following:

commit ac285a1
Author: Dominic Tarr <dominic.tarr@gmail.com>
Date:   Wed Aug 8 23:16:48 2012 +0200

    period is not part of escape sequence, oops.

commit 9016c9f
Author: Dominic Tarr <dominic.tarr@gmail.com>
Date:   Wed Aug 8 21:36:43 2012 +0200

    add support for mouse press events

Closes #1.
  • Loading branch information...
1 parent 1c5d913 commit 149a17d9000da4bdb289dd24732eb2a39bfb6cf6 @dominictarr dominictarr committed with Aug 8, 2012
Showing with 75 additions and 3 deletions.
  1. +25 −0 README.md
  2. +36 −2 index.js
  3. +14 −1 test.js
View
@@ -14,6 +14,7 @@ extension, the `repl`) module.
This module is the exact logic from the node `v0.8.x` releases ripped out into its
own module.
+__Bonus:__ Now with mouse support!
Installation
------------
@@ -30,6 +31,8 @@ Or add it to the `"dependencies"` section of your _package.json_ file.
Example
-------
+#### Listening for "keypress" events
+
``` js
var keypress = require('keypress');
@@ -48,6 +51,28 @@ 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
-------
View
@@ -15,8 +15,16 @@
* });
* proces.stdin.resume();
*/
+var exports = module.exports = keypress;
+
+exports.enableMouse = function (stream) {
+ stream.write('\x1b' +'[?1000h')
+}
+
+exports.disableMouse = function (stream) {
+ stream.write('\x1b' +'[?1000l')
+}
-module.exports = keypress;
/**
* accepts a readable Stream instance and makes it emit "keypress" events
@@ -297,6 +305,30 @@ function emitKey(stream, s) {
return;
}
+ 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
if (key.name === undefined) {
key = undefined;
@@ -306,7 +338,9 @@ function emitKey(stream, s) {
ch = s;
}
- if (key || ch) {
+ if (key && key.name == 'mouse') {
+ stream.emit('mousepress', key)
+ } else if (key || ch) {
stream.emit('keypress', ch, key);
}
}
View
@@ -1,5 +1,6 @@
-require('./')(process.stdin)
+var keypress = require('./')
+keypress(process.stdin)
if (process.stdin.setRawMode)
process.stdin.setRawMode(true)
@@ -12,4 +13,16 @@ process.stdin.on('keypress', function (c, key) {
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()
+

0 comments on commit 149a17d

Please sign in to comment.