-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoen.py
More file actions
executable file
·40 lines (34 loc) · 1.46 KB
/
Copy pathautoen.py
File metadata and controls
executable file
·40 lines (34 loc) · 1.46 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
#!/usr/bin/env python3
import time, subprocess, ctypes
from ctypes import c_double, c_uint32, c_int64
# ===== 配置 =====
IDLE_THRESHOLD = 7 # 空闲阈值(秒)
CHECK_INTERVAL = 5 # 轮询间隔(秒)
EN_ID = "com.apple.keylayout.ABC" # 用 im-select list 确认你的英文ID
# ===== 空闲检测(CoreGraphics)=====
CG = ctypes.CDLL("/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics")
CG.CGEventSourceSecondsSinceLastEventType.restype = c_double
CG.CGEventSourceSecondsSinceLastEventType.argtypes = [c_uint32, c_int64]
def idle_seconds() -> float:
# 1 = kCGEventSourceStateHIDSystemState, -1 = kCGAnyInputEventType
return CG.CGEventSourceSecondsSinceLastEventType(1, -1)
# ===== 输入法操作(仅用 im-select)=====
def current_ime() -> str:
return subprocess.check_output(["/opt/homebrew/bin/im-select"]).decode().strip()
def set_english():
subprocess.run(["/opt/homebrew/bin/im-select", EN_ID], check=True)
if __name__ == "__main__":
# 启动即校正一次
try:
if current_ime() != EN_ID:
set_english()
except Exception as e:
raise SystemExit(f"im-select 异常:{e}\n请先确保可运行 `im-select list`")
# 空闲切英文循环
while True:
try:
if idle_seconds() >= IDLE_THRESHOLD and current_ime() != EN_ID:
set_english()
except Exception:
pass
time.sleep(CHECK_INTERVAL)