AutoHotKey

AutoHotkey(简称 AHK)是一款 Windows 平台上的免费开源自动化脚本工具,用简单脚本就能操控键盘鼠标、窗口和各类程序,常用于提升日常操作效率。

核心能力

  • 热键与热字串:把任意按键组合绑定到动作,或输入缩写自动展开成整段文本。
  • 键鼠模拟:自动发送按键、点击、移动鼠标,实现重复操作自动化。
  • 窗口管理:激活、移动、调整、隐藏窗口,按标题/进程/类名精确匹配。
  • GUI 界面:用脚本快速创建窗口、按钮、输入框等简易工具界面。
  • 与系统交互:读写注册表、调用 COM、执行命令行、操作剪贴板等。
#Requires AutoHotkey v2.0

GroupAdd("TCMDGroup", "ahk_exe totalcmd.exe")
GroupAdd("TCMDGroup", "ahk_exe totalcmd64.exe")

global IsListaryLaunching := false
global KeyBuffer := ""

HotIf((*) => (WinActive("ahk_group TCMDGroup") and not IsInputFocused()) or IsListaryLaunching)

Loop Parse "abcdefghijklmnopqrstuvwxyz0123456789", "" {
    Hotkey(A_LoopField, OnTcmdTypeToSearch)
}

Loop 10 {
    Hotkey("Numpad" . (A_Index - 1), OnTcmdTypeToSearch)
}

for k in ["NumpadDot", "NumpadAdd", "NumpadSub", "NumpadMult", "NumpadDiv"] {
    Hotkey(k, OnTcmdTypeToSearch)
}

HotIf()

NormalizeKey(key) {
    if (RegExMatch(key, "^Numpad(\d)$", &m))
        return m[1]

    static numpadMap := Map(
        "NumpadDot",  ".",
        "NumpadAdd",  "+",
        "NumpadSub",  "-",
        "NumpadMult", "*",
        "NumpadDiv",  "/"
    )
    if (numpadMap.Has(key))
        return numpadMap[key]

    return key
}

OnTcmdTypeToSearch(ThisHotkey) {
    global IsListaryLaunching, KeyBuffer

    KeyBuffer .= NormalizeKey(ThisHotkey)

    if (IsListaryLaunching)
        return

    IsListaryLaunching := true
    try {
        SetKeyDelay(25, 15)
        SendEvent("{Ctrl}{Ctrl}")

        if (hWnd := WinWaitActive("ahk_exe Listary.exe", , 0.5)) {
            WaitListaryWindowStable(hWnd)

            SetKeyDelay(5, 5)
            while (KeyBuffer != "") {
                nextChar := SubStr(KeyBuffer, 1, 1)
                KeyBuffer := SubStr(KeyBuffer, 2)
                SendEvent("{Raw}" . nextChar)
            }
        }
    } finally {
        KeyBuffer := ""
        IsListaryLaunching := false
    }
}

WaitListaryWindowStable(hWnd) {
    startTime := A_TickCount
    lastW := 0, lastH := 0
    stableCount := 0

    while (A_TickCount - startTime < 300) {
        try {
            WinGetPos(, , &w, &h, hWnd)
            if (w > 200 && h > 30 && w == lastW && h == lastH) {
                stableCount++
                if (stableCount >= 2) {
                    Sleep(30)
                    return true
                }
            } else {
                stableCount := 0
            }
            lastW := w, lastH := h
        }
        Sleep(15)
    }
    return false
}

IsInputFocused() {
    try {
        focusedCtrl := ControlGetClassNN(ControlGetFocus("A"))
        return InStr(focusedCtrl, "Edit") > 0
    }
    return false
}

 

posted @ 2026-12-06 20:16  乐观的知觉  阅读(0)  评论(0)    收藏  举报