|
| 1 | +/** |
| 2 | + * Copyright (c) 2026 The xterm.js authors. All rights reserved. |
| 3 | + * @license MIT |
| 4 | + */ |
| 5 | + |
| 6 | +import { assert } from 'chai'; |
| 7 | +import { evaluateKeyboardEventWin32, Win32ControlKeyState } from 'common/input/Win32InputMode'; |
| 8 | +import { IKeyboardEvent, KeyboardResultType } from 'common/Types'; |
| 9 | + |
| 10 | +type EventOpts = Partial<IKeyboardEvent>; |
| 11 | +const ev = (opts: EventOpts): IKeyboardEvent => ({ |
| 12 | + altKey: false, ctrlKey: false, shiftKey: false, metaKey: false, |
| 13 | + keyCode: 0, code: '', key: '', type: 'keydown', ...opts |
| 14 | +}); |
| 15 | + |
| 16 | +const parse = (seq: string) => { |
| 17 | + const m = seq.match(/^\x1b\[(\d+);(\d+);(\d+);(\d+);(\d+);(\d+)_$/); |
| 18 | + return m ? { vk: +m[1], sc: +m[2], uc: +m[3], kd: +m[4], cs: +m[5], rc: +m[6] } : null; |
| 19 | +}; |
| 20 | + |
| 21 | +const test = (opts: EventOpts, isDown: boolean, check: (p: ReturnType<typeof parse>) => void) => { |
| 22 | + const result = evaluateKeyboardEventWin32(ev(opts), isDown); |
| 23 | + const parsed = parse(result.key!); |
| 24 | + assert.ok(parsed); |
| 25 | + check(parsed); |
| 26 | +}; |
| 27 | + |
| 28 | +describe('Win32InputMode', () => { |
| 29 | + describe('evaluateKeyboardEventWin32', () => { |
| 30 | + describe('basic key encoding', () => { |
| 31 | + it('letter key press', () => { |
| 32 | + const result = evaluateKeyboardEventWin32(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true); |
| 33 | + assert.strictEqual(result.type, KeyboardResultType.SEND_KEY); |
| 34 | + assert.strictEqual(result.cancel, true); |
| 35 | + const p = parse(result.key!); |
| 36 | + assert.ok(p); |
| 37 | + assert.deepStrictEqual([p.vk, p.uc, p.kd, p.rc], [0x41, 97, 1, 1]); |
| 38 | + }); |
| 39 | + it('letter key release', () => test({ code: 'KeyA', key: 'a', keyCode: 65 }, false, p => assert.strictEqual(p!.kd, 0))); |
| 40 | + it('digit key', () => test({ code: 'Digit1', key: '1', keyCode: 49 }, true, p => assert.deepStrictEqual([p!.vk, p!.uc], [0x31, 49]))); |
| 41 | + it('Enter key', () => test({ code: 'Enter', key: 'Enter', keyCode: 13 }, true, p => assert.deepStrictEqual([p!.vk, p!.uc], [0x0D, 0]))); |
| 42 | + it('Escape key', () => test({ code: 'Escape', key: 'Escape', keyCode: 27 }, true, p => assert.strictEqual(p!.vk, 0x1B))); |
| 43 | + it('Space key', () => test({ code: 'Space', key: ' ', keyCode: 32 }, true, p => assert.deepStrictEqual([p!.vk, p!.uc], [0x20, 32]))); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('modifier encoding', () => { |
| 47 | + it('shift', () => test({ code: 'KeyA', key: 'A', keyCode: 65, shiftKey: true }, true, p => assert.ok(p!.cs & Win32ControlKeyState.SHIFT_PRESSED))); |
| 48 | + it('ctrl left', () => test({ code: 'KeyA', key: 'a', keyCode: 65, ctrlKey: true }, true, p => assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED))); |
| 49 | + it('ctrl right', () => test({ code: 'ControlRight', key: 'Control', keyCode: 17, ctrlKey: true }, true, p => { |
| 50 | + assert.ok(p!.cs & Win32ControlKeyState.RIGHT_CTRL_PRESSED); |
| 51 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 52 | + })); |
| 53 | + it('alt left', () => test({ code: 'KeyA', key: 'a', keyCode: 65, altKey: true }, true, p => assert.ok(p!.cs & Win32ControlKeyState.LEFT_ALT_PRESSED))); |
| 54 | + it('alt right', () => test({ code: 'AltRight', key: 'Alt', keyCode: 18, altKey: true }, true, p => { |
| 55 | + assert.ok(p!.cs & Win32ControlKeyState.RIGHT_ALT_PRESSED); |
| 56 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 57 | + })); |
| 58 | + it('multiple modifiers', () => test({ code: 'KeyA', key: 'A', keyCode: 65, shiftKey: true, ctrlKey: true, altKey: true }, true, p => { |
| 59 | + assert.ok(p!.cs & Win32ControlKeyState.SHIFT_PRESSED); |
| 60 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED); |
| 61 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_ALT_PRESSED); |
| 62 | + })); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('function keys', () => { |
| 66 | + it('F1', () => test({ code: 'F1', key: 'F1', keyCode: 112 }, true, p => assert.strictEqual(p!.vk, 0x70))); |
| 67 | + it('F5', () => test({ code: 'F5', key: 'F5', keyCode: 116 }, true, p => assert.strictEqual(p!.vk, 0x74))); |
| 68 | + it('F12', () => test({ code: 'F12', key: 'F12', keyCode: 123 }, true, p => assert.strictEqual(p!.vk, 0x7B))); |
| 69 | + it('Ctrl+F1', () => test({ code: 'F1', key: 'F1', keyCode: 112, ctrlKey: true }, true, p => { |
| 70 | + assert.strictEqual(p!.vk, 0x70); |
| 71 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED); |
| 72 | + })); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('navigation keys (ENHANCED_KEY)', () => { |
| 76 | + const navKeys: [string, string, number, number][] = [ |
| 77 | + ['ArrowUp', 'ArrowUp', 38, 0x26], |
| 78 | + ['ArrowDown', 'ArrowDown', 40, 0x28], |
| 79 | + ['ArrowLeft', 'ArrowLeft', 37, 0x25], |
| 80 | + ['ArrowRight', 'ArrowRight', 39, 0x27], |
| 81 | + ['Home', 'Home', 36, 0x24], |
| 82 | + ['End', 'End', 35, 0x23], |
| 83 | + ['PageUp', 'PageUp', 33, 0x21], |
| 84 | + ['PageDown', 'PageDown', 34, 0x22], |
| 85 | + ['Insert', 'Insert', 45, 0x2D], |
| 86 | + ['Delete', 'Delete', 46, 0x2E], |
| 87 | + ]; |
| 88 | + navKeys.forEach(([code, key, keyCode, vk]) => { |
| 89 | + it(code, () => test({ code, key, keyCode }, true, p => { |
| 90 | + assert.strictEqual(p!.vk, vk); |
| 91 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 92 | + })); |
| 93 | + }); |
| 94 | + it('Tab', () => test({ code: 'Tab', key: 'Tab', keyCode: 9 }, true, p => assert.strictEqual(p!.vk, 0x09))); |
| 95 | + it('Backspace', () => test({ code: 'Backspace', key: 'Backspace', keyCode: 8 }, true, p => assert.strictEqual(p!.vk, 0x08))); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('numpad keys', () => { |
| 99 | + it('Numpad0', () => test({ code: 'Numpad0', key: '0', keyCode: 96 }, true, p => assert.strictEqual(p!.vk, 0x60))); |
| 100 | + it('NumpadEnter (ENHANCED)', () => test({ code: 'NumpadEnter', key: 'Enter', keyCode: 13 }, true, p => { |
| 101 | + assert.strictEqual(p!.vk, 0x0D); |
| 102 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 103 | + })); |
| 104 | + it('NumpadAdd', () => test({ code: 'NumpadAdd', key: '+', keyCode: 107 }, true, p => assert.strictEqual(p!.vk, 0x6B))); |
| 105 | + it('NumpadSubtract', () => test({ code: 'NumpadSubtract', key: '-', keyCode: 109 }, true, p => assert.strictEqual(p!.vk, 0x6D))); |
| 106 | + it('NumpadMultiply', () => test({ code: 'NumpadMultiply', key: '*', keyCode: 106 }, true, p => assert.strictEqual(p!.vk, 0x6A))); |
| 107 | + it('NumpadDivide (ENHANCED)', () => test({ code: 'NumpadDivide', key: '/', keyCode: 111 }, true, p => { |
| 108 | + assert.strictEqual(p!.vk, 0x6F); |
| 109 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 110 | + })); |
| 111 | + it('NumpadDecimal', () => test({ code: 'NumpadDecimal', key: '.', keyCode: 110 }, true, p => assert.strictEqual(p!.vk, 0x6E))); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('unicode character', () => { |
| 115 | + it('printable', () => test({ code: 'KeyA', key: 'a', keyCode: 65 }, true, p => assert.strictEqual(p!.uc, 97))); |
| 116 | + it('shifted', () => test({ code: 'KeyA', key: 'A', keyCode: 65, shiftKey: true }, true, p => assert.strictEqual(p!.uc, 65))); |
| 117 | + it('non-printable is 0', () => test({ code: 'ArrowUp', key: 'ArrowUp', keyCode: 38 }, true, p => assert.strictEqual(p!.uc, 0))); |
| 118 | + it('extended ASCII', () => test({ code: 'KeyE', key: 'é', keyCode: 69 }, true, p => assert.strictEqual(p!.uc, 233))); |
| 119 | + it('symbol', () => test({ code: 'Digit4', key: '$', keyCode: 52, shiftKey: true }, true, p => assert.strictEqual(p!.uc, 36))); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('scan codes', () => { |
| 123 | + it('letter A', () => test({ code: 'KeyA', key: 'a', keyCode: 65 }, true, p => assert.strictEqual(p!.sc, 0x1E))); |
| 124 | + it('Escape', () => test({ code: 'Escape', key: 'Escape', keyCode: 27 }, true, p => assert.strictEqual(p!.sc, 0x01))); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('sequence format', () => { |
| 128 | + it('valid CSI format', () => { |
| 129 | + const result = evaluateKeyboardEventWin32(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true); |
| 130 | + assert.ok(result.key?.startsWith('\x1b[') && result.key.endsWith('_')); |
| 131 | + assert.strictEqual(result.key?.slice(2, -1).split(';').length, 6); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('standalone modifier keys', () => { |
| 136 | + it('ShiftLeft', () => test({ code: 'ShiftLeft', key: 'Shift', keyCode: 16, shiftKey: true }, true, p => { |
| 137 | + assert.strictEqual(p!.vk, 0x10); |
| 138 | + assert.ok(p!.cs & Win32ControlKeyState.SHIFT_PRESSED); |
| 139 | + })); |
| 140 | + it('ShiftRight', () => test({ code: 'ShiftRight', key: 'Shift', keyCode: 16, shiftKey: true }, true, p => { |
| 141 | + assert.strictEqual(p!.vk, 0x10); |
| 142 | + assert.ok(p!.cs & Win32ControlKeyState.SHIFT_PRESSED); |
| 143 | + })); |
| 144 | + it('ControlLeft', () => test({ code: 'ControlLeft', key: 'Control', keyCode: 17, ctrlKey: true }, true, p => { |
| 145 | + assert.strictEqual(p!.vk, 0x11); |
| 146 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED); |
| 147 | + })); |
| 148 | + it('ControlRight', () => test({ code: 'ControlRight', key: 'Control', keyCode: 17, ctrlKey: true }, true, p => { |
| 149 | + assert.strictEqual(p!.vk, 0x11); |
| 150 | + assert.ok(p!.cs & Win32ControlKeyState.RIGHT_CTRL_PRESSED); |
| 151 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 152 | + })); |
| 153 | + it('AltLeft', () => test({ code: 'AltLeft', key: 'Alt', keyCode: 18, altKey: true }, true, p => { |
| 154 | + assert.strictEqual(p!.vk, 0x12); |
| 155 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_ALT_PRESSED); |
| 156 | + })); |
| 157 | + it('AltRight', () => test({ code: 'AltRight', key: 'Alt', keyCode: 18, altKey: true }, true, p => { |
| 158 | + assert.strictEqual(p!.vk, 0x12); |
| 159 | + assert.ok(p!.cs & Win32ControlKeyState.RIGHT_ALT_PRESSED); |
| 160 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 161 | + })); |
| 162 | + it('modifier release', () => test({ code: 'ShiftLeft', key: 'Shift', keyCode: 16 }, false, p => assert.strictEqual(p!.kd, 0))); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('problem keys from spec', () => { |
| 166 | + it('Ctrl+Space', () => test({ code: 'Space', key: ' ', keyCode: 32, ctrlKey: true }, true, p => { |
| 167 | + assert.strictEqual(p!.vk, 0x20); |
| 168 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED); |
| 169 | + })); |
| 170 | + it('Shift+Enter', () => test({ code: 'Enter', key: 'Enter', keyCode: 13, shiftKey: true }, true, p => { |
| 171 | + assert.strictEqual(p!.vk, 0x0D); |
| 172 | + assert.ok(p!.cs & Win32ControlKeyState.SHIFT_PRESSED); |
| 173 | + })); |
| 174 | + it('Ctrl+Break', () => test({ code: 'Pause', key: 'Pause', keyCode: 19, ctrlKey: true }, true, p => { |
| 175 | + assert.strictEqual(p!.vk, 0x13); |
| 176 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED); |
| 177 | + })); |
| 178 | + it('Ctrl+Alt+/', () => test({ code: 'Slash', key: '/', keyCode: 191, ctrlKey: true, altKey: true }, true, p => { |
| 179 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_CTRL_PRESSED); |
| 180 | + assert.ok(p!.cs & Win32ControlKeyState.LEFT_ALT_PRESSED); |
| 181 | + })); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('meta key', () => { |
| 185 | + it('MetaLeft', () => test({ code: 'MetaLeft', key: 'Meta', keyCode: 91, metaKey: true }, true, p => { |
| 186 | + assert.strictEqual(p!.vk, 0x5B); |
| 187 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 188 | + })); |
| 189 | + it('MetaRight', () => test({ code: 'MetaRight', key: 'Meta', keyCode: 92, metaKey: true }, true, p => { |
| 190 | + assert.strictEqual(p!.vk, 0x5C); |
| 191 | + assert.ok(p!.cs & Win32ControlKeyState.ENHANCED_KEY); |
| 192 | + })); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments