This commit is contained in:
2025-12-05 10:57:02 +08:00
parent da3958f8f0
commit db3334ec6c
9 changed files with 1129 additions and 38 deletions

62
src/test/test-h.ts Normal file
View File

@@ -0,0 +1,62 @@
import { keyboard, Key } from "@nut-tree/nut-js";
// 将快捷键字符串转换为 Key 枚举值
function parseHotkey(hotkey: string): Key[] {
const keyMap: Record<string, Key> = {
'ctrl': Key.LeftControl,
'leftctrl': Key.LeftControl,
'rightctrl': Key.RightControl,
'alt': Key.LeftAlt,
'leftalt': Key.LeftAlt,
'rightalt': Key.RightAlt,
'shift': Key.LeftShift,
'leftshift': Key.LeftShift,
'rightshift': Key.RightShift,
'meta': Key.LeftSuper,
'cmd': Key.LeftCmd,
'win': Key.LeftWin,
};
return hotkey
.toLowerCase()
.split('+')
.map(key => {
const trimmed = key.trim();
// 如果是修饰键,从映射表中获取
if (keyMap[trimmed]) {
return keyMap[trimmed];
}
// 如果是字母,转换为大写并查找对应的 Key
if (trimmed.length === 1 && /[a-z]/.test(trimmed)) {
const upperKey = trimmed.toUpperCase();
return Key[upperKey as keyof typeof Key] as Key;
}
// 其他情况直接查找
return Key[trimmed as keyof typeof Key] as Key;
})
.filter((key): key is Key => key !== undefined);
}
const hotkey = 'ctrl+h';
const keys = parseHotkey(hotkey);
console.log('准备模拟按下快捷键:', hotkey);
console.log('解析后的键:', keys);
// 同时按下所有键
keyboard.pressKey(...keys)
.then(() => {
console.log('快捷键已按下');
// 短暂延迟后释放
return new Promise(resolve => setTimeout(resolve, 100));
})
.then(() => {
// 释放所有键
return keyboard.releaseKey(...keys);
})
.then(() => {
console.log('快捷键已释放');
})
.catch((error) => {
console.error('模拟快捷键失败:', error);
});