# 使用 CapsLock 切换输入法


为了让 mac/win 下的体验稍微一致些, 写了一个 ahk 脚本来做这件事情.

针对 *CapsLock *键:

* **单击*** *切换输入法(本质是调用 *win + sapce*)

* **双击/长按** 切换 *CapsLock *状态

![preview](https://cdn.hashnode.com/res/hashnode/image/upload/v1605152180896/-10BYSVsa.gif)*preview*

以下是脚本内容:

```autohotkey
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Persistent
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

hideTrayTip() {
    TrayTip  ; Attempt to hide it the normal way.
    if SubStr(A_OSVersion,1,3) = "10." {
        Menu Tray, NoIcon
        Sleep 200  ; It may be necessary to adjust this sleep.
        Menu Tray, Icon
    }
}


switchCapsLockState() {
    state := GetKeyState("CapsLock", "T")
    nextState := !state
    SetCapsLockState % nextState
    
    return nextState
}

showTip(isOn) {
    title := isOn ? "CapsLock: ON" : "CapsLock: OFF"
    text := isOn ? "已打开" : "已关闭"

    TrayTip, %title%, %text%, 0, 16
    Sleep 1000
    hideTrayTip()

    return 
}

toggleAndShowTip() {
    nextState :=switchCapsLockState()
    showTip(nextState)

    return
}

CapsLock::
    KeyWait, CapsLock, T0.2

    if (ErrorLevel) {
        ; long click
        toggleAndShowTip()
    } else {
        KeyWait, CapsLock, D T0.1

        if (ErrorLevel) {
            ; single click
            Send #{Space}
        } else {
            ; double click
            toggleAndShowTip()
        }
    }

    KeyWait, CapsLock

return

```
