From d4e9d34d2d49b50472906f545ed777e4dd900bc1 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 15 Jul 2016 16:16:37 -0700 Subject: [PATCH 1/4] Allow custom keydown handler to be attached Fixes #118 --- src/xterm.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/xterm.js b/src/xterm.js index 8633ff96e6..f506e8e14a 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -247,6 +247,7 @@ this.queue = ''; this.scrollTop = 0; this.scrollBottom = this.rows - 1; + this.customKeydownHandler = null; // modes this.applicationKeypad = false; @@ -2517,9 +2518,24 @@ this.write(data + '\r\n'); }; + /** + * Attaches a custom keydown handler which is run before keys are processed, giving consumers of + * xterm.js ultimate control as to what keys should be processed by the terminal and what keys + * should not. + * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a + * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent + * the default action. The function returns whether the event should be processed by xterm.js. + */ + Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) { + this.customKeydownHandler = customKeydownHandler; + } + // Key Resources: // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent Terminal.prototype.keyDown = function(ev) { + if (this.customKeydownHandler && !this.customKeydownHandler(ev)) { + return; + } var self = this; var result = this.evaluateKeyEscapeSequence(ev); From 9cb5b005bbf9b3db4e9690ba306a246119b5b9a7 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 15 Jul 2016 16:31:57 -0700 Subject: [PATCH 2/4] Add tests --- src/xterm.js | 2 +- test/test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/xterm.js b/src/xterm.js index 89473aaf0d..c0308b4045 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -2549,7 +2549,7 @@ */ Terminal.prototype.keyDown = function(ev) { if (this.customKeydownHandler && !this.customKeydownHandler(ev)) { - return; + return false; } var self = this; var result = this.evaluateKeyEscapeSequence(ev); diff --git a/test/test.js b/test/test.js index 5059a24284..7c0caa7c7a 100644 --- a/test/test.js +++ b/test/test.js @@ -57,6 +57,32 @@ describe('xterm.js', function() { }); }); + describe('attachCustomEventHandler', function () { + var evKeyDown = { + preventDefault: function() {}, + stopPropagation: function() {}, + type: 'keydown' + } + + beforeEach(function() { + xterm.handler = function() {}; + xterm.showCursor = function() {}; + xterm.clearSelection = function() {}; + }); + + it('should process the keydown event based on what the handler returns', function () { + assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), true); + xterm.attachCustomKeydownHandler(function (ev) { + return ev.keyCode === 77; + }); + assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), true); + xterm.attachCustomKeydownHandler(function (ev) { + return ev.keyCode !== 77; + }); + assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); + }); + }); + describe('evaluateCopiedTextProcessing', function () { it('should strip trailing whitespaces and replace nbsps with spaces', function () { var nonBreakingSpace = String.fromCharCode(160), From 577a3d81e5384392e4b90e4b7b8c57040333b502 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 21 Jul 2016 10:32:20 -0700 Subject: [PATCH 3/4] Use explicit boolean comparison --- src/xterm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xterm.js b/src/xterm.js index c0308b4045..16502e20a8 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -2548,7 +2548,7 @@ * @param {KeyboardEvent} ev The keydown event to be handled. */ Terminal.prototype.keyDown = function(ev) { - if (this.customKeydownHandler && !this.customKeydownHandler(ev)) { + if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) { return false; } var self = this; From e2d0de26866c609d2dd040f2e090e4399e889c33 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 22 Jul 2016 05:18:28 -0700 Subject: [PATCH 4/4] Fix test --- test/test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test.js b/test/test.js index b5ba13051d..d2842f81aa 100644 --- a/test/test.js +++ b/test/test.js @@ -68,6 +68,13 @@ describe('xterm.js', function() { xterm.handler = function() {}; xterm.showCursor = function() {}; xterm.clearSelection = function() {}; + xterm.compositionHelper = { + keydown: { + bind: function() { + return function () { return true; } + } + } + } }); it('should process the keydown event based on what the handler returns', function () {