Skip to content

Commit

Permalink
Added macOptionIsEscape according to xtermjs#4904
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-baranger committed Apr 17, 2024
1 parent d2b8e6c commit 1ff84e2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/browser/Terminal.ts
Expand Up @@ -1007,7 +1007,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
}

// Ignore composing with Alt key on Mac when macOptionIsMeta is enabled
const shouldIgnoreComposition = this.browser.isMac && this.options.macOptionIsMeta && event.altKey;
const shouldIgnoreComposition = this.browser.isMac && this.options.macOptionIsMeta && this.options.macOptionIsEscape && event.altKey;

if (!shouldIgnoreComposition && !this._compositionHelper!.keydown(event)) {
if (this.options.scrollOnUserInput && this.buffer.ybase !== this.buffer.ydisp) {
Expand All @@ -1020,7 +1020,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
this._unprocessedDeadKey = true;
}

const result = evaluateKeyboardEvent(event, this.coreService.decPrivateModes.applicationCursorKeys, this.browser.isMac, this.options.macOptionIsMeta);
const result = evaluateKeyboardEvent(event, this.coreService.decPrivateModes.applicationCursorKeys, this.browser.isMac, this.options.macOptionIsMeta, this.options.macOptionIsEscape);

this.updateCursorStyle(event);

Expand Down Expand Up @@ -1084,7 +1084,7 @@ export class Terminal extends CoreTerminal implements ITerminal {

private _isThirdLevelShift(browser: IBrowser, ev: KeyboardEvent): boolean {
const thirdLevelKey =
(browser.isMac && !this.options.macOptionIsMeta && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
(browser.isMac && !this.options.macOptionIsMeta && !this.options.macOptionIsEscape && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
(browser.isWindows && ev.altKey && ev.ctrlKey && !ev.metaKey) ||
(browser.isWindows && ev.getModifierState('AltGraph'));

Expand Down
15 changes: 13 additions & 2 deletions src/common/input/Keyboard.test.ts
Expand Up @@ -20,6 +20,7 @@ function testEvaluateKeyboardEvent(partialEvent: {
applicationCursorMode?: boolean;
isMac?: boolean;
macOptionIsMeta?: boolean;
macOptionIsEscape?: boolean;
} = {}): IKeyboardResult {
const event: IKeyboardEvent = {
altKey: partialEvent.altKey || false,
Expand All @@ -34,9 +35,10 @@ function testEvaluateKeyboardEvent(partialEvent: {
const options = {
applicationCursorMode: partialOptions.applicationCursorMode || false,
isMac: partialOptions.isMac || false,
macOptionIsMeta: partialOptions.macOptionIsMeta || false
macOptionIsMeta: partialOptions.macOptionIsMeta || false,
macOptionIsEscape: partialOptions.macOptionIsEscape || false
};
return evaluateKeyboardEvent(event, options.applicationCursorMode, options.isMac, options.macOptionIsMeta);
return evaluateKeyboardEvent(event, options.applicationCursorMode, options.isMac, options.macOptionIsMeta, options.macOptionIsEscape);
}

describe('Keyboard', () => {
Expand Down Expand Up @@ -175,6 +177,15 @@ describe('Keyboard', () => {
assert.equal(testEvaluateKeyboardEvent({ altKey: true, keyCode: 13 }, { isMac: true, macOptionIsMeta: true }).key, '\x1b\r');
});
});
describe('with macOptionIsEscape', () => {
it('should return \\x80 for alt+@', () => {
assert.equal(testEvaluateKeyboardEvent({ altKey: true, keyCode: 64 }, { isMac: true, macOptionIsEscape: true }).key, '\x80');
});

it('should return \\x9F for alt+_', () => {
assert.equal(testEvaluateKeyboardEvent({ altKey: true, keyCode: 95 }, { isMac: true, macOptionIsEscape: true }).key, '\x9F');
});
});

it('should return \\x1b[5A for alt+up', () => {
assert.equal(testEvaluateKeyboardEvent({ altKey: true, keyCode: 38 }).key, '\x1b[1;5A'); // CSI 5 A
Expand Down
11 changes: 8 additions & 3 deletions src/common/input/Keyboard.ts
Expand Up @@ -39,7 +39,8 @@ export function evaluateKeyboardEvent(
ev: IKeyboardEvent,
applicationCursorMode: boolean,
isMac: boolean,
macOptionIsMeta: boolean
macOptionIsMeta: boolean,
macOptionIsEscape: boolean
): IKeyboardResult {
const result: IKeyboardResult = {
type: KeyboardResultType.SEND_KEY,
Expand Down Expand Up @@ -346,11 +347,15 @@ export function evaluateKeyboardEvent(
} else if (ev.keyCode === 221) {
result.key = C0.GS;
}
} else if ((!isMac || macOptionIsMeta) && ev.altKey && !ev.metaKey) {
} else if ((!isMac || macOptionIsMeta || macOptionIsEscape) && ev.altKey && !ev.metaKey) {
// On macOS this is a third level shift when !macOptionIsMeta. Use <Esc> instead.
const keyMapping = KEYCODE_KEY_MAPPINGS[ev.keyCode];
const key = keyMapping?.[!ev.shiftKey ? 0 : 1];
if (key) {
if (macOptionIsEscape && ev.keyCode >= 64 && ev.keyCode <= 95) {
result.key = String.fromCharCode(128 + ev.keyCode - 64);
console.log(12);
}
else if (key) {
result.key = C0.ESC + key;
} else if (ev.keyCode >= 65 && ev.keyCode <= 90) {
const keyCode = ev.ctrlKey ? ev.keyCode - 64 : ev.keyCode + 32;
Expand Down
1 change: 1 addition & 0 deletions src/common/services/OptionsService.ts
Expand Up @@ -36,6 +36,7 @@ export const DEFAULT_OPTIONS: Readonly<Required<ITerminalOptions>> = {
scrollSensitivity: 1,
screenReaderMode: false,
smoothScrollDuration: 0,
macOptionIsEscape: false,
macOptionIsMeta: false,
macOptionClickForcesSelection: false,
minimumContrastRatio: 1,
Expand Down

0 comments on commit 1ff84e2

Please sign in to comment.