33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""后端共用小工具。"""
|
|
import ctypes
|
|
|
|
KEY_ALIASES = {'return': 'enter', 'escape': 'esc', 'del': 'delete', 'ins': 'insert',
|
|
'pgup': 'pageup', 'pgdn': 'pagedown', 'control': 'ctrl', 'win': 'lwin',
|
|
'arrowup': 'up', 'arrowdown': 'down', 'arrowleft': 'left', 'arrowright': 'right'}
|
|
|
|
|
|
def parse_combo(key):
|
|
"""'Alt+F4' -> ['alt', 'f4']; 单个 '+' 视为加号键。"""
|
|
k = str(key).strip().lower()
|
|
if k == '+' or '+' not in k:
|
|
parts = [k]
|
|
else:
|
|
parts = [p.strip() for p in k.split('+')]
|
|
if any(not p for p in parts):
|
|
raise ValueError('bad key combo %r' % key)
|
|
return [KEY_ALIASES.get(p, p) for p in parts]
|
|
|
|
|
|
def win_dpi_aware(user32):
|
|
"""声明 per-monitor DPI 感知, 让 SetCursorPos/GetSystemMetrics 用物理像素, 和截图一致。"""
|
|
try:
|
|
ctypes.windll.shcore.SetProcessDpiAwareness(2)
|
|
return
|
|
except Exception:
|
|
pass
|
|
try:
|
|
user32.SetProcessDPIAware()
|
|
except Exception:
|
|
pass
|