decode mouse events and responses properly.

This commit is contained in:
Christopher Jeffrey 2013-08-13 11:30:44 -05:00
parent d42adf7e32
commit 070cbd9001
2 changed files with 25 additions and 7 deletions

View File

@ -9,6 +9,7 @@
*/
var EventEmitter = require('events').EventEmitter
, StringDecoder = require('string_decoder').StringDecoder
, util = require('util')
, Tput = require('./tput')
, colors = require('./colors')
@ -105,9 +106,7 @@ Program.prototype._log = function(pre, msg) {
Program.prototype.setupDump = function() {
var self = this
, write = this.output.write
, decoder;
decoder = new (require('string_decoder')).StringDecoder('utf8');
, decoder = new StringDecoder('utf8');
function stringify(data) {
return caret(data
@ -349,8 +348,17 @@ Program.prototype.removeKey = function(key, listener) {
// left click: ^[[M 3<^[[M#3<
// mousewheel up: ^[[M`3>
Program.prototype.bindMouse = function() {
this.on('data', this._bindMouse.bind(this));
this.bindMouse = function() {};
if (this._boundMouse) return;
this._boundMouse = true;
var decoder = new StringDecoder('utf8')
, self = this;
this.on('data', function(data) {
data = decoder.write(data);
if (!data) return;
self._bindMouse(data);
});
};
Program.prototype._bindMouse = function(s) {
@ -611,8 +619,17 @@ Program.prototype._bindMouse = function(s) {
// All possible responses from the terminal
Program.prototype.bindResponse = function() {
this.on('data', this._bindResponse.bind(this));
this.bindResponse = function() {};
if (this._boundResponse) return;
this._boundResponse = true;
var decoder = new StringDecoder('utf8')
, self = this;
this.on('data', function(data) {
data = decoder.write(data);
if (!data) return;
self._bindResponse(data);
});
};
Program.prototype._bindResponse = function(s) {

View File

@ -30,6 +30,7 @@ program.key(['q', 'escape', 'C-c'], function() {
});
program.on('keypress', function(ch, data) {
if (data.name === 'mouse') return;
program.clear();
program.cup(0, 0);
console.log(data);