Merge pull request #712 from chabou/customKeypressHandler

Add attachCustomKeyEventsHandler
This commit is contained in:
Paris Kasidiaris
2017-06-22 16:16:23 +03:00
committed by GitHub
2 changed files with 45 additions and 14 deletions
+20 -5
View File
@@ -371,12 +371,17 @@ describe('xterm.js', function() {
}); });
}); });
describe('attachCustomEventHandler', function () { describe('attachCustomKeyEventHandler', function () {
var evKeyDown = { var evKeyDown = {
preventDefault: function() {}, preventDefault: function() {},
stopPropagation: function() {}, stopPropagation: function() {},
type: 'keydown' type: 'keydown'
} }
var evKeyPress = {
preventDefault: function() {},
stopPropagation: function() {},
type: 'keypress'
}
beforeEach(function() { beforeEach(function() {
xterm.handler = function() {}; xterm.handler = function() {};
@@ -387,29 +392,39 @@ describe('xterm.js', function() {
bind: function() { bind: function() {
return function () { return true; } return function () { return true; }
} }
},
keypress: {
bind: function() {
return function () { return true; }
}
} }
} }
}); });
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); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), true);
xterm.attachCustomKeydownHandler(function (ev) { assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true);
xterm.attachCustomKeyEventHandler(function (ev) {
return ev.keyCode === 77; return ev.keyCode === 77;
}); });
assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), true); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), true);
xterm.attachCustomKeydownHandler(function (ev) { assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true);
xterm.attachCustomKeyEventHandler(function (ev) {
return ev.keyCode !== 77; return ev.keyCode !== 77;
}); });
assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); 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 () { it('should alive after reset(ESC c Full Reset (RIS))', function () {
xterm.attachCustomKeydownHandler(function (ev) { xterm.attachCustomKeyEventHandler(function (ev) {
return ev.keyCode !== 77; return ev.keyCode !== 77;
}); });
assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false);
assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), false);
xterm.reset(); xterm.reset();
assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false); assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false);
assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), false);
}); });
}); });
+25 -9
View File
@@ -167,7 +167,7 @@ function Terminal(options) {
this.queue = ''; this.queue = '';
this.scrollTop = 0; this.scrollTop = 0;
this.scrollBottom = this.rows - 1; this.scrollBottom = this.rows - 1;
this.customKeydownHandler = null; this.customKeyEventHandler = null;
this.cursorBlinkInterval = null; this.cursorBlinkInterval = null;
// modes // modes
@@ -1329,15 +1329,27 @@ Terminal.prototype.writeln = function(data) {
}; };
/** /**
* Attaches a custom keydown handler which is run before keys are processed, giving consumers of * DEPRECATED: only for backward compatibility. Please use attachCustomKeyEventHandler() instead.
* 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 * @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 * 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. * the default action. The function returns whether the event should be processed by xterm.js.
*/ */
Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) { 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 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.attachCustomKeyEventHandler = function(customKeyEventHandler) {
this.customKeyEventHandler = customKeyEventHandler;
} }
/** /**
@@ -1436,7 +1448,7 @@ Terminal.prototype.selectAll = function() {
* @param {KeyboardEvent} ev The keydown event to be handled. * @param {KeyboardEvent} ev The keydown event to be handled.
*/ */
Terminal.prototype.keyDown = function(ev) { Terminal.prototype.keyDown = function(ev) {
if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) { if (this.customKeyEventHandler && this.customKeyEventHandler(ev) === false) {
return false; return false;
} }
@@ -1801,6 +1813,10 @@ Terminal.prototype.setgCharset = function(g, charset) {
Terminal.prototype.keyPress = function(ev) { Terminal.prototype.keyPress = function(ev) {
var key; var key;
if (this.customKeyEventHandler && this.customKeyEventHandler(ev) === false) {
return false;
}
this.cancel(ev); this.cancel(ev);
if (ev.charCode) { if (ev.charCode) {
@@ -1826,7 +1842,7 @@ Terminal.prototype.keyPress = function(ev) {
this.showCursor(); this.showCursor();
this.handler(key); this.handler(key);
return false; return true;
}; };
/** /**
@@ -2259,10 +2275,10 @@ Terminal.prototype.reverseIndex = function() {
Terminal.prototype.reset = function() { Terminal.prototype.reset = function() {
this.options.rows = this.rows; this.options.rows = this.rows;
this.options.cols = this.cols; this.options.cols = this.cols;
var customKeydownHandler = this.customKeydownHandler; var customKeyEventHandler = this.customKeyEventHandler;
var cursorBlinkInterval = this.cursorBlinkInterval; var cursorBlinkInterval = this.cursorBlinkInterval;
Terminal.call(this, this.options); Terminal.call(this, this.options);
this.customKeydownHandler = customKeydownHandler; this.customKeyEventHandler = customKeyEventHandler;
this.cursorBlinkInterval = cursorBlinkInterval; this.cursorBlinkInterval = cursorBlinkInterval;
this.refresh(0, this.rows - 1); this.refresh(0, this.rows - 1);
this.viewport.syncScrollArea(); this.viewport.syncScrollArea();