Skip to content

Instantly share code, notes, and snippets.

@sedm0784
Last active July 16, 2024 11:14
Show Gist options
  • Save sedm0784/4443120 to your computer and use it in GitHub Desktop.
Save sedm0784/4443120 to your computer and use it in GitHub Desktop.
AutoHotkey 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.)
g_LastCtrlKeyDownTime := 0
g_AbortSendEsc := false
g_ControlRepeatDetected := false
*CapsLock::
if (g_ControlRepeatDetected)
{
return
}
send,{Ctrl down}
g_LastCtrlKeyDownTime := A_TickCount
g_AbortSendEsc := false
g_ControlRepeatDetected := true
return
*CapsLock Up::
send,{Ctrl up}
g_ControlRepeatDetected := false
if (g_AbortSendEsc)
{
return
}
current_time := A_TickCount
time_elapsed := current_time - g_LastCtrlKeyDownTime
if (time_elapsed <= 250)
{
SendInput {Esc}
}
return
~*^a::
~*^b::
~*^c::
~*^d::
~*^e::
~*^f::
~*^g::
~*^h::
~*^i::
~*^j::
~*^k::
~*^l::
~*^m::
~*^n::
~*^o::
~*^p::
~*^q::
~*^r::
~*^s::
~*^t::
~*^u::
~*^v::
~*^w::
~*^x::
~*^y::
~*^z::
~*^1::
~*^2::
~*^3::
~*^4::
~*^5::
~*^6::
~*^7::
~*^8::
~*^9::
~*^0::
~*^Space::
~*^Backspace::
~*^Delete::
~*^Insert::
~*^Home::
~*^End::
~*^PgUp::
~*^PgDn::
~*^Tab::
~*^Return::
~*^,::
~*^.::
~*^/::
~*^;::
~*^'::
~*^[::
~*^]::
~*^\::
~*^-::
~*^=::
~*^`::
~*^F1::
~*^F2::
~*^F3::
~*^F4::
~*^F5::
~*^F6::
~*^F7::
~*^F8::
~*^F9::
~*^F10::
~*^F11::
~*^F12::
g_AbortSendEsc := true
return
@robmunger
Copy link

robmunger commented Feb 9, 2022

Fenwar's version, posted above (May 2018) is beautiful code. I like it because it doesn't hook every key on the keyboard. Give it a look.

@Rccustevens
Copy link

https://gist.github.com/Rccustevens/e972cb21869b81ba0b5400991de5cd85

Differences:

  1. Add a rule that maps LCtrl to CapsLock.

@darianmorat
Copy link

Caps to Ctrl+Esc...
If just pressed 1 time is gonna act as ESC
If used with another key is gonna act as CTRL
Allow Shift + Ctrl combinations

#IfWinNotActive,ahk_group WorkIn
*CapsLock::
    Send {Blind}{Ctrl Down}
    cDown := A_TickCount
Return

*CapsLock up::
    If ((A_TickCount-cDown) < 150){
        Send {Blind}{Ctrl Up}{Esc}
    }
    Else {
        Send {Blind}{Ctrl Up}
    }
Return
#IfWinNotActive

@ulic-youthlic
Copy link

@AryanilPanja
Copy link

Caps to Ctrl+Esc... If just pressed 1 time is gonna act as ESC If used with another key is gonna act as CTRL Allow Shift + Ctrl combinations

#IfWinNotActive,ahk_group WorkIn
*CapsLock::
    Send {Blind}{Ctrl Down}
    cDown := A_TickCount
Return

*CapsLock up::
    If ((A_TickCount-cDown) < 150){
        Send {Blind}{Ctrl Up}{Esc}
    }
    Else {
        Send {Blind}{Ctrl Up}
    }
Return
#IfWinNotActive

where do you paste this script?

@ulic-youthlic
Copy link

Caps to Ctrl+Esc... If just pressed 1 time is gonna act as ESC If used with another key is gonna act as CTRL Allow Shift + Ctrl combinations大写为 Ctrl+Esc... 如果只按 1 次将充当 ESC 如果与另一个键一起使用将充当 CTRL 允许 Shift + Ctrl 组合

#IfWinNotActive,ahk_group WorkIn
*CapsLock::
    Send {Blind}{Ctrl Down}
    cDown := A_TickCount
Return

*CapsLock up::
    If ((A_TickCount-cDown) < 150){
        Send {Blind}{Ctrl Up}{Esc}
    }
    Else {
        Send {Blind}{Ctrl Up}
    }
Return
#IfWinNotActive

where do you paste this script?你把这个脚本粘贴到哪里?

If you want to run this script, you should look into AutoHotKey. in this example, you need to install the correct version of the runtime, save the script as an .auk file, and run it using the runtime.

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