From 95aed8fcb0953440973fabbcf96391bb6c4b80b4 Mon Sep 17 00:00:00 2001 From: CHaBou Date: Thu, 15 Jun 2017 08:38:29 +0200 Subject: [PATCH 1/2] Add customKeypressHandler --- src/test/test.js | 27 +++++++++++++++++++++++++++ src/xterm.js | 21 ++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/test/test.js b/src/test/test.js index 1644e5800f..12cd9dc24a 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -377,6 +377,11 @@ describe('xterm.js', function() { stopPropagation: function() {}, type: 'keydown' } + var evKeyPress = { + preventDefault: function() {}, + stopPropagation: function() {}, + type: 'keypress' + } beforeEach(function() { xterm.handler = function() {}; @@ -387,6 +392,11 @@ describe('xterm.js', function() { bind: function() { return function () { return true; } } + }, + keypress: { + bind: function() { + return function () { return true; } + } } } }); @@ -403,13 +413,30 @@ describe('xterm.js', function() { assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); }); + it('should process the keypress event based on what the handler returns', function () { + assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true); + xterm.attachCustomKeypressHandler(function (ev) { + return ev.keyCode === 77; + }); + assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true); + xterm.attachCustomKeypressHandler(function (ev) { + return ev.keyCode !== 77; + }); + assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), false); + }); + it('should alive after reset(ESC c Full Reset (RIS))', function () { xterm.attachCustomKeydownHandler(function (ev) { return ev.keyCode !== 77; }); + xterm.attachCustomKeypressHandler(function (ev) { + return ev.keyCode !== 77; + }); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); + assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), false); xterm.reset(); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); + assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), false); }); }); diff --git a/src/xterm.js b/src/xterm.js index 0abcbeaeb5..647d0c6dc6 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -168,6 +168,7 @@ function Terminal(options) { this.scrollTop = 0; this.scrollBottom = this.rows - 1; this.customKeydownHandler = null; + this.customKeypressHandler = null; this.cursorBlinkInterval = null; // modes @@ -1316,6 +1317,18 @@ Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) { this.customKeydownHandler = customKeydownHandler; } +/** + * Attaches a custom keypress 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} customKeypressHandler 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.attachCustomKeypressHandler = function(customKeypressHandler) { + this.customKeypressHandler = customKeypressHandler; +} + /** * Attaches a http(s) link handler, forcing web links to behave differently to * regular tags. This will trigger a refresh as links potentially need to be @@ -1777,6 +1790,10 @@ Terminal.prototype.setgCharset = function(g, charset) { Terminal.prototype.keyPress = function(ev) { var key; + if (this.customKeypressHandler && this.customKeypressHandler(ev) === false) { + return false; + } + this.cancel(ev); if (ev.charCode) { @@ -1802,7 +1819,7 @@ Terminal.prototype.keyPress = function(ev) { this.showCursor(); this.handler(key); - return false; + return true; }; /** @@ -2236,9 +2253,11 @@ Terminal.prototype.reset = function() { this.options.rows = this.rows; this.options.cols = this.cols; var customKeydownHandler = this.customKeydownHandler; + var customKeypressHandler = this.customKeypressHandler; var cursorBlinkInterval = this.cursorBlinkInterval; Terminal.call(this, this.options); this.customKeydownHandler = customKeydownHandler; + this.customKeypressHandler = customKeypressHandler; this.cursorBlinkInterval = cursorBlinkInterval; this.refresh(0, this.rows - 1); this.viewport.syncScrollArea(); From 8e79d53d7839108fcb0ef5db10ab77e4d1b20224 Mon Sep 17 00:00:00 2001 From: CHaBou Date: Mon, 19 Jun 2017 22:03:19 +0200 Subject: [PATCH 2/2] Add attachCustomKeyEventHandler() Use a uniq customKeyEventHandler for keyDown and keyPress events Mark and warn attachCustomKeydownHandler() as deprecated. --- src/test/test.js | 26 +++++++------------------- src/xterm.js | 27 ++++++++++++--------------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/test/test.js b/src/test/test.js index 12cd9dc24a..cad1dfc416 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -371,7 +371,7 @@ describe('xterm.js', function() { }); }); - describe('attachCustomEventHandler', function () { + describe('attachCustomKeyEventHandler', function () { var evKeyDown = { preventDefault: function() {}, stopPropagation: function() {}, @@ -401,35 +401,23 @@ describe('xterm.js', function() { } }); - it('should process the keydown event based on what the handler returns', function () { + it('should process the keydown/keypress 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); - }); - - it('should process the keypress event based on what the handler returns', function () { assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true); - xterm.attachCustomKeypressHandler(function (ev) { + xterm.attachCustomKeyEventHandler(function (ev) { return ev.keyCode === 77; }); + assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), true); assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true); - xterm.attachCustomKeypressHandler(function (ev) { + xterm.attachCustomKeyEventHandler(function (ev) { return ev.keyCode !== 77; }); + assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), false); }); it('should alive after reset(ESC c Full Reset (RIS))', function () { - xterm.attachCustomKeydownHandler(function (ev) { - return ev.keyCode !== 77; - }); - xterm.attachCustomKeypressHandler(function (ev) { + xterm.attachCustomKeyEventHandler(function (ev) { return ev.keyCode !== 77; }); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); diff --git a/src/xterm.js b/src/xterm.js index 647d0c6dc6..007b8bc9f8 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -167,8 +167,7 @@ function Terminal(options) { this.queue = ''; this.scrollTop = 0; this.scrollBottom = this.rows - 1; - this.customKeydownHandler = null; - this.customKeypressHandler = null; + this.customKeyEventHandler = null; this.cursorBlinkInterval = null; // modes @@ -1306,27 +1305,27 @@ Terminal.prototype.writeln = function(data) { }; /** - * 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. + * DEPRECATED: only for backward compatibility. Please use attachCustomKeyEventHandler() instead. * @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; + let message = 'attachCustomKeydownHandler() is DEPRECATED and will be removed soon. Please use attachCustomKeyEventHandler() instead.'; + console.warn(message); + this.attachCustomKeyEventHandler(customKeydownHandler); } /** - * Attaches a custom keypress handler which is run before keys are processed, giving consumers of + * Attaches a custom key event 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} customKeypressHandler 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.attachCustomKeypressHandler = function(customKeypressHandler) { - this.customKeypressHandler = customKeypressHandler; +Terminal.prototype.attachCustomKeyEventHandler = function(customKeyEventHandler) { + this.customKeyEventHandler = customKeyEventHandler; } /** @@ -1425,7 +1424,7 @@ Terminal.prototype.selectAll = function() { * @param {KeyboardEvent} ev The keydown event to be handled. */ Terminal.prototype.keyDown = function(ev) { - if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) { + if (this.customKeyEventHandler && this.customKeyEventHandler(ev) === false) { return false; } @@ -1790,7 +1789,7 @@ Terminal.prototype.setgCharset = function(g, charset) { Terminal.prototype.keyPress = function(ev) { var key; - if (this.customKeypressHandler && this.customKeypressHandler(ev) === false) { + if (this.customKeyEventHandler && this.customKeyEventHandler(ev) === false) { return false; } @@ -2252,12 +2251,10 @@ Terminal.prototype.reverseIndex = function() { Terminal.prototype.reset = function() { this.options.rows = this.rows; this.options.cols = this.cols; - var customKeydownHandler = this.customKeydownHandler; - var customKeypressHandler = this.customKeypressHandler; + var customKeyEventHandler = this.customKeyEventHandler; var cursorBlinkInterval = this.cursorBlinkInterval; Terminal.call(this, this.options); - this.customKeydownHandler = customKeydownHandler; - this.customKeypressHandler = customKeypressHandler; + this.customKeyEventHandler = customKeyEventHandler; this.cursorBlinkInterval = cursorBlinkInterval; this.refresh(0, this.rows - 1); this.viewport.syncScrollArea();