Skip to content

Commit

Permalink
Merge pull request #240 from sourcelair/issue/239
Browse files Browse the repository at this point in the history
Fix alt + arrow key movement
  • Loading branch information
parisk authored Aug 22, 2016
2 parents 4c3ccc2 + 5d6e035 commit 243ce59
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
48 changes: 36 additions & 12 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2907,39 +2907,63 @@
break;
// left-arrow
case 37:
if (modifiers)
if (modifiers) {
result.key = '\x1b[1;' + (modifiers + 1) + 'D';
else if (this.applicationCursor)
// HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
// http://unix.stackexchange.com/a/108106
if (result.key == '\x1b[1;3D') {
result.key = '\x1b[1;5D';
}
} else if (this.applicationCursor) {
result.key = '\x1bOD';
else
} else {
result.key = '\x1b[D';
}
break;
// right-arrow
case 39:
if (modifiers)
if (modifiers) {
result.key = '\x1b[1;' + (modifiers + 1) + 'C';
else if (this.applicationCursor)
// HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
// http://unix.stackexchange.com/a/108106
if (result.key == '\x1b[1;3C') {
result.key = '\x1b[1;5C';
}
} else if (this.applicationCursor) {
result.key = '\x1bOC';
else
} else {
result.key = '\x1b[C';
}
break;
// up-arrow
case 38:
if (modifiers)
if (modifiers) {
result.key = '\x1b[1;' + (modifiers + 1) + 'A';
else if (this.applicationCursor)
// HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
// http://unix.stackexchange.com/a/108106
if (result.key == '\x1b[1;3A') {
result.key = '\x1b[1;5A';
}
} else if (this.applicationCursor) {
result.key = '\x1bOA';
else
} else {
result.key = '\x1b[A';
}
break;
// down-arrow
case 40:
if (modifiers)
if (modifiers) {
result.key = '\x1b[1;' + (modifiers + 1) + 'B';
else if (this.applicationCursor)
// HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
// http://unix.stackexchange.com/a/108106
if (result.key == '\x1b[1;3B') {
result.key = '\x1b[1;5B';
}
} else if (this.applicationCursor) {
result.key = '\x1bOB';
else
} else {
result.key = '\x1b[B';
}
break;
// insert
case 45:
Expand Down
22 changes: 21 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ describe('xterm.js', function() {
it('should return \\x1b[5C for ctrl+right', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ ctrlKey: true, keyCode: 39 }).key, '\x1b[1;5C'); // CSI 5 C
});
it('should return \\x1b[5A for ctrl+up', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ ctrlKey: true, keyCode: 38 }).key, '\x1b[1;5A'); // CSI 5 A
});
it('should return \\x1b[5B for ctrl+down', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ ctrlKey: true, keyCode: 40 }).key, '\x1b[1;5B'); // CSI 5 B
});
// Evalueate alt + arrow key movement, which is a feature of terminal emulators but not VT100
// http://unix.stackexchange.com/a/108106
it('should return \\x1b[5D for alt+left', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ altKey: true, keyCode: 37 }).key, '\x1b[1;5D'); // CSI 5 D
});
it('should return \\x1b[5C for alt+right', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ altKey: true, keyCode: 39 }).key, '\x1b[1;5C'); // CSI 5 C
});
it('should return \\x1b[5A for alt+up', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ altKey: true, keyCode: 38 }).key, '\x1b[1;5A'); // CSI 5 A
});
it('should return \\x1b[5B for alt+down', function() {
assert.equal(xterm.evaluateKeyEscapeSequence({ altKey: true, keyCode: 40 }).key, '\x1b[1;5B'); // CSI 5 B
});
});

describe('attachCustomEventHandler', function () {
Expand Down Expand Up @@ -88,7 +108,7 @@ describe('xterm.js', function() {
});
assert.equal(xterm.keyDown(Object.assign({}, evKeyDown, { keyCode: 77 })), false);
});

it('should alive after reset(ESC c Full Reset (RIS))', function () {
xterm.attachCustomKeydownHandler(function (ev) {
return ev.keyCode !== 77;
Expand Down

0 comments on commit 243ce59

Please sign in to comment.