Skip to content

Instantly share code, notes, and snippets.

@h2m730131
Last active May 6, 2026 03:39
Show Gist options
  • Select an option

  • Save h2m730131/da324c5e625cdf999c4bc26760dff399 to your computer and use it in GitHub Desktop.

Select an option

Save h2m730131/da324c5e625cdf999c4bc26760dff399 to your computer and use it in GitHub Desktop.
Remap the Keyboard in Windows: AutoHotkey
if not A_IsAdmin {
Run('*RunAs "' A_ScriptFullPath '"')
ExitApp()
}
; Log GUI
; global g_LogGui := Gui("+Resize +AlwaysOnTop", "AHK Log")
; global g_LogEdit := g_LogGui.Add("Edit", "ReadOnly -Wrap w500 h300")
; g_LogGui.Show("NoActivate")
;
; Log(msg) {
; global g_LogEdit
; g_LogEdit.Value .= "[" FormatTime(, "HH:mm:ss") "] " msg "`n"
; SendMessage(0xB1, -1, -1, g_LogEdit) ; EM_SETSEL: move caret to end
; SendMessage(0xB7, 0, 0, g_LogEdit) ; EM_SCROLLCARET: scroll to caret
; }
; GroupAdd("Vim_Only", "ahk_exe devenv.exe") ; Visual Studio 2019
; GroupAdd("Vim_Only", "ahk_exe Code.exe") ; Visual Studio Code
; GroupAdd("Vim_Only", "ahk_exe msedge.exe") ; Edge
; Left Alt + h / j / k / l 為方向鍵
; #HotIf WinActive("ahk_group Vim_Only")
; <!h::Send("{Left}")
; <!j::Send("{Down}")
; <!k::Send("{Up}")
; <!l::Send("{Right}")
; #HotIf
<!h:: Send("{Left}")
<!j:: Send("{Down}")
<!k:: Send("{Up}")
<!l:: Send("{Right}")
; Left Ctrl + J 切換輸入語言 (Left Alt + Shift)
; <^J:: {
; KeyWait("Ctrl")
; KeyWait("J")
; ; Send("{LAlt down}{Shift}{LAlt up}") ; {LAlt down}{Shift down}{Shift up}{LAlt up} 中文輸入法的輸入模式會交替互換,這次為中文、下次換英數字元
; ; {Shift down}{LAlt down}{LAlt up}{Shift up} 切換輸入語言後,會停留在工具列上 (e.g., File)
; Send("{LAlt down}{Shift down}{LAlt up}{Shift up}") ; 避免中文輸入法的輸入模式會交替互換
; }
; [使用AutoHotkey快速切換語言鍵盤,和超難用的Ctrl/Alt+Shift說再見!(AHK #9) – 簡睿隨筆](https://jdev.tw/blog/5738)
<^J:: {
; 改用 SPI_GETDEFAULTINPUTLANG(SystemParametersInfo)讀取系統預設輸入法
buf := Buffer(4, 0)
DllCall("SystemParametersInfo", "UInt", 0x0059, "UInt", 0, "UPtr", buf.Ptr, "UInt", 0)
langId := NumGet(buf, 0, "UInt") & 0xFFFF
if (langId = 0x0409) { ; 目前為英文,切換為中文
SetDefaultKeyboard(0x0404)
; Log("切換輸入法: 英文 → 中文")
} else { ; 目前為中文,切換為英文
SetDefaultKeyboard(0x0409)
; Log("切換輸入法: 中文 → 英文")
}
}
SetDefaultKeyboard(LocaleID) {
SPI_SETDEFAULTINPUTLANG := 0x005A
SPIF_SENDWININICHANGE := 2
Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
buf := Buffer(4, 0)
NumPut("UInt", LocaleID, buf, 0)
;Lan := 0xE0090404
DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", buf.Ptr, "UInt",
SPIF_SENDWININICHANGE)
windows := WinGetList()
for hwnd in windows {
PostMessage(0x50, 0, Lan, , "ahk_id " hwnd)
}
}
^h:: Send("{Backspace}")
#HotIf IsExcludedApp()
^w:: Send("^{Backspace}")
; ^d::Send("{Delete}")
#HotIf
IsExcludedApp() {
exeName := WinGetProcessName("A")
if (exeName = "Code.exe" || exeName = "devenv.exe" || exeName = "bash.exe" || exeName = "WindowsTerminal.exe" ||
exeName = "warp.exe")
return true
return false
}
; CapsLock 鍵改成 Esc 鍵
*CapsLock:: {
KeyWait("CapsLock")
if (A_ThisHotkey = "*CapsLock")
Send("{Blind}{Esc}")
}
; Ctrl+Shift+O to open containing folder in Explorer.
; Ctrl+Shift+E to open folder with current file selected.
; Supports SciTE and Notepad++.
; ^+o::
; ^+e:: {
; path := WinGetTitle("A")
; if (RegExMatch(path, "\*?\K(.*)\\[^\\]+(?= [-*] )", &match)) {
; path := match[1]
; if (FileExist(path) && A_ThisHotkey = "^+e")
; Run('explorer.exe /select,"' path '"')
; else
; Run('explorer.exe "' path '"')
; }
; }
; Moving the Mouse Cursor via the Keyboard
; *#up::MouseMove(0, -10, 0, "R") ; Win+UpArrow hotkey => Move cursor upward
; *#Down::MouseMove(0, 10, 0, "R") ; Win+DownArrow => Move cursor downward
; *#Left::MouseMove(-10, 0, 0, "R") ; Win+LeftArrow => Move cursor to the left
; *#Right::MouseMove(10, 0, 0, "R") ; Win+RightArrow => Move cursor to the right
;
; *<#RCtrl:: { ; LeftWin + RightControl => Left-click (hold down Control/Shift to Control-Click or Shift-Click).
; SendEvent("{Blind}{LButton down}")
; KeyWait("RCtrl") ; Prevents keyboard auto-repeat from repeating the mouse click.
; SendEvent("{Blind}{LButton up}")
; }
;
; *<#AppsKey:: { ; LeftWin + AppsKey => Right-click
; SendEvent("{Blind}{RButton down}")
; KeyWait("AppsKey") ; Prevents keyboard auto-repeat from repeating the mouse click.
; SendEvent("{Blind}{RButton up}")
; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment