Skip to content

Commit 401c5d4

Browse files
committed
fix: remove cast
1 parent 9ad3f1e commit 401c5d4

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/tools/input.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import type {McpContext, TextSnapshotNode} from '../McpContext.js';
88
import {zod} from '../third_party/index.js';
9-
import type {ElementHandle, KeyInput} from '../third_party/index.js';
9+
import type {ElementHandle} from '../third_party/index.js';
1010
import {parseKey} from '../utils/keyboard.js';
1111

1212
import {ToolCategory} from './categories.js';
@@ -288,15 +288,15 @@ export const pressKey = defineTool({
288288
},
289289
handler: async (request, response, context) => {
290290
const page = context.getSelectedPage();
291-
const tokens = parseKey(request.params.key) as KeyInput[];
291+
const tokens = parseKey(request.params.key);
292292
const [key, ...modifiers] = tokens;
293293

294294
await context.waitForEventsAfterAction(async () => {
295-
for (let i = modifiers.length - 1; i >= 0; i--) {
296-
await page.keyboard.down(modifiers[i]);
295+
for (const modifier of modifiers) {
296+
await page.keyboard.down(modifier);
297297
}
298298
await page.keyboard.press(key);
299-
for (const modifier of modifiers) {
299+
for (const modifier of modifiers.toReversed()) {
300300
await page.keyboard.up(modifier);
301301
}
302302
});

src/utils/keyboard.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import type {KeyInput} from '../third_party';
77

8+
// See the KeyInput type for the list of supported keys.
89
const validKeys = new Set([
910
'0',
1011
'1',
@@ -273,7 +274,7 @@ function throwIfInvalidKey(key: string): KeyInput {
273274
}
274275

275276
/**
276-
* Returns the key, followed by modifiers.
277+
* Returns the primary key, followed by modifiers in original order.
277278
*/
278279
export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
279280
let key = '';
@@ -287,7 +288,9 @@ export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
287288
key += ch;
288289
}
289290
}
290-
if (key) result.push(throwIfInvalidKey(key));
291+
if (key) {
292+
result.push(throwIfInvalidKey(key));
293+
}
291294

292295
if (result.length === 0) {
293296
throw new Error(`Key ${keyInput} could not be parsed.`);
@@ -297,5 +300,5 @@ export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
297300
throw new Error(`Key ${keyInput} contains duplicate keys.`);
298301
}
299302

300-
return result.reverse() as [KeyInput, ...KeyInput[]];
303+
return [result.at(-1), ...result.slice(0, -1)] as [KeyInput, ...KeyInput[]];
301304
}

tests/tools/input.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ describe('input', () => {
438438
it('parses keys', () => {
439439
assert.deepStrictEqual(parseKey('Shift+A'), ['A', 'Shift']);
440440
assert.deepStrictEqual(parseKey('Shift++'), ['+', 'Shift']);
441+
assert.deepStrictEqual(parseKey('Control+Shift++'), [
442+
'+',
443+
'Control',
444+
'Shift',
445+
]);
441446
assert.deepStrictEqual(parseKey('Shift'), ['Shift']);
442447
assert.deepStrictEqual(parseKey('KeyA'), ['KeyA']);
443448
});

0 commit comments

Comments
 (0)