Merge pull request #5635 from Tyriar/win32

Move win32 input mode into a class to avoid init cost
This commit is contained in:
Daniel Imms
2026-01-30 07:23:01 -08:00
committed by GitHub
3 changed files with 268 additions and 266 deletions
+12 -3
View File
@@ -6,7 +6,7 @@
import { IKeyboardService } from 'browser/services/Services';
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
import { evaluateKeyboardEventKitty, KittyKeyboardEventType, KittyKeyboardFlags, shouldUseKittyProtocol } from 'common/input/KittyKeyboard';
import { evaluateKeyboardEventWin32 } from 'common/input/Win32InputMode';
import { Win32InputMode } from 'common/input/Win32InputMode';
import { isMac } from 'common/Platform';
import { ICoreService, IOptionsService } from 'common/services/Services';
import { IKeyboardResult } from 'common/Types';
@@ -14,16 +14,25 @@ import { IKeyboardResult } from 'common/Types';
export class KeyboardService implements IKeyboardService {
public serviceBrand: undefined;
private _win32InputMode: Win32InputMode | undefined;
constructor(
@ICoreService private readonly _coreService: ICoreService,
@IOptionsService private readonly _optionsService: IOptionsService
) {
}
private _getWin32InputMode(): Win32InputMode {
if (!this._win32InputMode) {
this._win32InputMode = new Win32InputMode();
}
return this._win32InputMode;
}
public evaluateKeyDown(event: KeyboardEvent): IKeyboardResult {
// Win32 input mode takes priority (most raw)
if (this.useWin32InputMode) {
return evaluateKeyboardEventWin32(event, true);
return this._getWin32InputMode().evaluateKeyboardEvent(event, true);
}
const kittyFlags = this._coreService.kittyKeyboard.flags;
return this.useKitty
@@ -34,7 +43,7 @@ export class KeyboardService implements IKeyboardService {
public evaluateKeyUp(event: KeyboardEvent): IKeyboardResult | undefined {
// Win32 input mode sends key up events
if (this.useWin32InputMode) {
return evaluateKeyboardEventWin32(event, false);
return this._getWin32InputMode().evaluateKeyboardEvent(event, false);
}
const kittyFlags = this._coreService.kittyKeyboard.flags;
if (this.useKitty && (kittyFlags & KittyKeyboardFlags.REPORT_EVENT_TYPES)) {
+7 -5
View File
@@ -4,7 +4,7 @@
*/
import { assert } from 'chai';
import { evaluateKeyboardEventWin32, Win32ControlKeyState } from 'common/input/Win32InputMode';
import { Win32InputMode, Win32ControlKeyState } from 'common/input/Win32InputMode';
import { IKeyboardEvent, KeyboardResultType } from 'common/Types';
type EventOpts = Partial<IKeyboardEvent>;
@@ -18,18 +18,20 @@ const parse = (seq: string) => {
return m ? { vk: +m[1], sc: +m[2], uc: +m[3], kd: +m[4], cs: +m[5], rc: +m[6] } : null;
};
const win32 = new Win32InputMode();
const test = (opts: EventOpts, isDown: boolean, check: (p: ReturnType<typeof parse>) => void) => {
const result = evaluateKeyboardEventWin32(ev(opts), isDown);
const result = win32.evaluateKeyboardEvent(ev(opts), isDown);
const parsed = parse(result.key!);
assert.ok(parsed);
check(parsed);
};
describe('Win32InputMode', () => {
describe('evaluateKeyboardEventWin32', () => {
describe('evaluateKeyboardEvent', () => {
describe('basic key encoding', () => {
it('letter key press', () => {
const result = evaluateKeyboardEventWin32(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
const result = win32.evaluateKeyboardEvent(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
assert.strictEqual(result.type, KeyboardResultType.SEND_KEY);
assert.strictEqual(result.cancel, true);
const p = parse(result.key!);
@@ -135,7 +137,7 @@ describe('Win32InputMode', () => {
describe('sequence format', () => {
it('valid CSI format', () => {
const result = evaluateKeyboardEventWin32(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
const result = win32.evaluateKeyboardEvent(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
assert.ok(result.key?.startsWith('\x1b[') && result.key.endsWith('_'));
assert.strictEqual(result.key?.slice(2, -1).split(';').length, 6);
});
File diff suppressed because it is too large Load Diff