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 = {
preventDefault: function() {},
stopPropagation: function() {},
type: 'keydown'
}
var evKeyPress = {
preventDefault: function() {},
stopPropagation: function() {},
type: 'keypress'
}
beforeEach(function() {
xterm.handler = function() {};
@@ -387,29 +392,39 @@ describe('xterm.js', function() {
bind: function() {
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);
xterm.attachCustomKeydownHandler(function (ev) {
assert.equal(xterm.keyPress(Object.assign({}, evKeyPress, { keyCode: 77 })), true);
xterm.attachCustomKeyEventHandler(function (ev) {
return ev.keyCode === 77;
});
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;
});
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) {
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);
xterm.reset();
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.scrollTop = 0;
this.scrollBottom = this.rows - 1;
this.customKeydownHandler = null;
this.customKeyEventHandler = null;
this.cursorBlinkInterval = null;
// 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
* 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 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.
*/
Terminal.prototype.keyDown = function(ev) {
if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) {
if (this.customKeyEventHandler && this.customKeyEventHandler(ev) === false) {
return false;
}
@@ -1801,6 +1813,10 @@ Terminal.prototype.setgCharset = function(g, charset) {
Terminal.prototype.keyPress = function(ev) {
var key;
if (this.customKeyEventHandler && this.customKeyEventHandler(ev) === false) {
return false;
}
this.cancel(ev);
if (ev.charCode) {
@@ -1826,7 +1842,7 @@ Terminal.prototype.keyPress = function(ev) {
this.showCursor();
this.handler(key);
return false;
return true;
};
/**
@@ -2259,10 +2275,10 @@ Terminal.prototype.reverseIndex = function() {
Terminal.prototype.reset = function() {
this.options.rows = this.rows;
this.options.cols = this.cols;
var customKeydownHandler = this.customKeydownHandler;
var customKeyEventHandler = this.customKeyEventHandler;
var cursorBlinkInterval = this.cursorBlinkInterval;
Terminal.call(this, this.options);
this.customKeydownHandler = customKeydownHandler;
this.customKeyEventHandler = customKeyEventHandler;
this.cursorBlinkInterval = cursorBlinkInterval;
this.refresh(0, this.rows - 1);
this.viewport.syncScrollArea();