-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutool.py
More file actions
112 lines (100 loc) · 3.19 KB
/
utool.py
File metadata and controls
112 lines (100 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
该文件是分离保存主程序的的工具
"""
import win32con
from pynput.keyboard import Key
# 映射特殊键名字符串到pynput的Key对象
SPECIAL_KEYS = {
'<ctrl>': Key.ctrl,
'<shift>': Key.shift,
'<alt>': Key.alt,
'<cmd>': Key.cmd,
'<win>': Key.cmd,
'<enter>': Key.enter,
'<tab>': Key.tab,
'<esc>': Key.esc,
'<space>': Key.space,
'<backspace>': Key.backspace,
'<delete>': Key.delete,
'<insert>': Key.insert,
'<home>': Key.home,
'<end>': Key.end,
'<page_up>': Key.page_up,
'<page_down>': Key.page_down,
'<up>': Key.up,
'<down>': Key.down,
'<left>': Key.left,
'<right>': Key.right,
'<f1>': Key.f1,
'<f2>': Key.f2,
'<f3>': Key.f3,
'<f4>': Key.f4,
'<f5>': Key.f5,
'<f6>': Key.f6,
'<f7>': Key.f7,
'<f8>': Key.f8,
'<f9>': Key.f9,
'<f10>': Key.f10,
'<f11>': Key.f11,
'<f12>': Key.f12,
}
def parse_keys_string(keys_str: str):
"""
解析组合键字符串,返回pynput可识别的按键列表。
例如: "<ctrl>+<shift>+s" -> [Key.ctrl, Key.shift, 's']
"""
keys = []
parts = keys_str.lower().split('+')
for part in parts:
part = part.strip()
if part in SPECIAL_KEYS:
keys.append(SPECIAL_KEYS[part])
elif len(part) == 1:
keys.append(part)
return keys
# 映射人类可读的键名到Windows虚拟键码(vkCode)
# 映射人类可读的键名到Windows虚拟键码(vkCode)
KEY_TO_VK = {
# 字母
'a': 0x41, 'b': 0x42, 'c': 0x43, 'd': 0x44, 'e': 0x45, 'f': 0x46,
'g': 0x47, 'h': 0x48, 'i': 0x49, 'j': 0x4A, 'k': 0x4B, 'l': 0x4C,
'm': 0x4D, 'n': 0x4E, 'o': 0x4F, 'p': 0x50, 'q': 0x51, 'r': 0x52,
's': 0x53, 't': 0x54, 'u': 0x55, 'v': 0x56, 'w': 0x57, 'x': 0x58,
'y': 0x59, 'z': 0x5A,
# 数字
'0': 0x30, '1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34,
'5': 0x35, '6': 0x36, '7': 0x37, '8': 0x38, '9': 0x39,
# 特殊字符
'semicolon': 0xBA, # ; (VK_OEM_1)
'apostrophe': 0xDE, # ' (VK_OEM_7)
'comma': 0xBC, # , (VK_OEM_COMMA)
'period': 0xBE, # . (VK_OEM_PERIOD)
'slash': 0xBF, # / (VK_OEM_2)
'backspace': 0x08, # (VK_BACK)
'space': 0x20, # (VK_SPACE)
'enter': 0x0D, # (VK_RETURN)
# 功能键
'esc': win32con.VK_ESCAPE,
'f1': win32con.VK_F1, 'f2': win32con.VK_F2, 'f3': win32con.VK_F3,
'f4': win32con.VK_F4, 'f5': win32con.VK_F5, 'f6': win32con.VK_F6,
'f7': win32con.VK_F7, 'f8': win32con.VK_F8, 'f9': win32con.VK_F9,
'f10': win32con.VK_F10, 'f11': win32con.VK_F11, 'f12': win32con.VK_F12,
# 控制键
'shift': win32con.VK_SHIFT,
'lshift': win32con.VK_LSHIFT,
'rshift': win32con.VK_RSHIFT,
'shift_r': win32con.VK_RSHIFT,
'ctrl': win32con.VK_CONTROL,
'lctrl': win32con.VK_LCONTROL,
'rctrl': win32con.VK_RCONTROL,
'alt': win32con.VK_MENU, # Alt key
'lalt': win32con.VK_LMENU,
'ralt': win32con.VK_RMENU,
}
# 映射键名到pynput的Key对象,用于on_press/on_release监听器
NAME_TO_PYNPUT_KEY = {
'esc': Key.esc,
'shift_l': Key.shift_l,
'caps_lock': Key.caps_lock,
'alt': Key.alt,
}