Skip to content

Instantly share code, notes, and snippets.

@shen774411223d
Created January 17, 2025 03:10
Show Gist options
  • Select an option

  • Save shen774411223d/4bf01747609f8a8586bfc0bd850888f1 to your computer and use it in GitHub Desktop.

Select an option

Save shen774411223d/4bf01747609f8a8586bfc0bd850888f1 to your computer and use it in GitHub Desktop.
Simulate the case-switching interaction of the macOS on Windows.
#Requires AutoHotkey v2.0
/*
- 在 CapsLock 已开启的情况下:
短按 CapsLock 键时,直接关闭大写锁定,且不切换中英文输入法。
- 在 CapsLock 已关闭的情况下:
短按 CapsLock 键时,切换中英文输入法。
长按 CapsLock 键时,切换大写锁定状态。
*/
CapsLock::
{
; 记录按键按下的时间
startTime := A_TickCount
; 标志变量,用于确保只切换一次
hasSwitched := false
; 循环检测按键状态
while (GetKeyState("CapsLock", "P")) { ; 如果 CapsLock 仍然被按住
; 计算按下的持续时间
holdTime := A_TickCount - startTime
; 如果按住时间超过 1 秒且未切换过,切换大小写状态
if (holdTime >= 1000 && !hasSwitched) {
if GetKeyState("CapsLock", "T") {
SetCapsLockState("Off") ; 如果大写锁定开启,则关闭
ToolTip("大写锁定已关闭")
} else {
SetCapsLockState("On") ; 如果大写锁定关闭,则开启
ToolTip("大写锁定已开启")
}
Sleep(500) ; 显示提示信息 0.5 秒
ToolTip() ; 关闭提示信息
hasSwitched := true ; 标记为已切换
}
; 短暂休眠,避免 CPU 占用过高
Sleep(10)
}
; 如果按住时间小于 1 秒
if (!hasSwitched) {
if GetKeyState("CapsLock", "T") {
; 如果大写锁定已开启,直接关闭大写锁定
SetCapsLockState("Off")
ToolTip("大写锁定已关闭")
} else {
; 如果大写锁定已关闭,切换中英文输入法
Send("^ ") ; 发送 Ctrl+Space 切换输入法
}
Sleep(500) ; 显示提示信息 0.5 秒
ToolTip() ; 关闭提示信息
}
}
return
@shen774411223d
Copy link
Copy Markdown
Author

  1. First, you need to install AutoHotKey.

  2. The purpose of this script is to change the Caps Lock behavior on Windows to match the interaction of macOS's Caps Lock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment