Skip to content

Instantly share code, notes, and snippets.

@karenepitaya
Forked from sedm0784/CapsLockCtrlEscape.ahk
Last active June 26, 2026 13:59
Show Gist options
  • Select an option

  • Save karenepitaya/46aeb717b3202c8d371d50b34eefb568 to your computer and use it in GitHub Desktop.

Select an option

Save karenepitaya/46aeb717b3202c8d371d50b34eefb568 to your computer and use it in GitHub Desktop.
AutoHotkey2 script to map Caps Lock to Escape when it's pressed on its own and Ctrl when used in combination with another key, à la Steve Losh. Adapted from one that does something similar with the Ctrl Key on the Vim Tips Wiki (http://vim.wikia.com/wiki/Map_caps_lock_to_escape_in_Windows?oldid=32281). (Plus contribs from @randy909 & @mmikeww.)
#Requires AutoHotkey v2.0
#SingleInstance Force
SetCapsLockState "AlwaysOff"
g_LastCtrlKeyDownTime := 0
g_AbortSendEsc := false
g_ControlRepeatDetected := false
*CapsLock:: {
global
if (g_ControlRepeatDetected)
return
Send("{Ctrl down}")
g_LastCtrlKeyDownTime := A_TickCount
g_AbortSendEsc := false
g_ControlRepeatDetected := true
}
*CapsLock Up:: {
global
Send("{Ctrl up}")
g_ControlRepeatDetected := false
if (g_AbortSendEsc)
return
time_elapsed := A_TickCount - g_LastCtrlKeyDownTime
if (time_elapsed <= 250)
SendInput("{Esc}")
}
AbortEscHandler(*) {
global g_AbortSendEsc
g_AbortSendEsc := true
}
; 字母 a-z
Loop 26 {
k := Chr(96 + A_Index)
Hotkey("~*^" k, AbortEscHandler)
}
; 数字 0-9
Loop 10 {
Hotkey("~*^" (A_Index - 1), AbortEscHandler)
}
; F1-F12
Loop 12 {
Hotkey("~*^F" A_Index, AbortEscHandler)
}
; 其他按键(把 Return 改成 Enter)
otherKeys := ["Space", "Backspace", "Delete", "Insert", "Home", "End"
, "PgUp", "PgDn", "Tab", "Enter"
, ",", ".", "/", ";", "'", "[", "]", "\", "-", "=", "``"]
for k in otherKeys
Hotkey("~*^" k, AbortEscHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment