Skip to content

Instantly share code, notes, and snippets.

@XueshiQiao
Last active April 12, 2025 05:41
Show Gist options
  • Save XueshiQiao/d29736bd47c6b67230c5002ff5c00638 to your computer and use it in GitHub Desktop.
Save XueshiQiao/d29736bd47c6b67230c5002ff5c00638 to your computer and use it in GitHub Desktop.
在 Windows 上模拟 macOS 的快捷键体验 + Vim 体验
#Requires AutoHotkey v2.0
; recommended for performance and compatibility with future autohotkey releases.
#UseHook
#SingleInstance force
InstallKeybdHook()
SendMode "Input"
/**
主要功能:
在 Windows 上模拟 macOS 的体验 + vim 体验
1. 屏蔽掉原 CapsLock 键盘(使用右Shift代替)
2. CapsLock + hjkl 模拟 vim 方向键,等等。
3. Ctrl + Tab [+ Shift] => Alt + Tab + [+ Shift]
4. 屏蔽掉默认的 Ctrl + Mouse wheel up/down = Zoom in / Zoom out
这里没有交换 LWin 和 LCtrl,原因如下:
1. 使用 AutoHotkey2 交换的话,会触发一些奇怪的系统按键,比如在 input 输入框中双击鼠标,会触发 Win 键按下,且不会 Key Up。
2. 使用 PowerToys 来映射,会引入虚拟按键,导致其他的组合键有问题,比如 Ctrl + Tab => Alt + Tab
3. 所以这里我使用 Mistel MD770 的 Macro 来自定义一个 layer 来做这个映射。从硬件层面解决这个问题。
*/
; DO NOT switch LCtrl and LWin key which will trigger strange actions
; AND DO NOT SWITCH them in PowerToys, can't support mapping ctrl+tab => alt+tab perfectly
; SWITCH them via Keyboard's Macro instead.
;LWin::LCtrl
;LCtrl::LWin
;;
; ;; deactivate capslock completely
SetCapslockState("AlwaysOff")
;; remap right Shift to Capslock
RShift::CapsLock
; Disable Ctrl + Mouse Wheel Up (Zoom In)
^WheelUp::return
; Disable Ctrl + Mouse Wheel Down (Zoom Out)
^WheelDown::return
;; map capslock to ctrl
; CapsLock::Ctrl
; ^j:: Send "{Down}"
; ^h:: Send "{Left}"
; ^k:: Send "{Up}"
; ^l::Send "{Right}"
;; vim navigation with hyper
Hotkey "~Capslock & h", SendLeft
Hotkey "~Capslock & l", SendRight
Hotkey "~Capslock & k", SendUp
Hotkey "~Capslock & j", SendDown
;; map capslock + i to delete
Hotkey "~Capslock & i", SendBackspace
Hotkey "~Capslock & u", SendUp10
Hotkey "~Capslock & d", SendDown10
Hotkey "~Capslock & a", SendHome
Hotkey "~Capslock & s", SendEnd
Hotkey "~Capslock & e", SendDeleteCurrentWord
Hotkey "~Capslock & f", SendDeleteCurrentLine
Hotkey "~Capslock & y", SendBackWord
Hotkey "~Capslock & p", SendForwardWord
HotKey "~Capslock & ,", SendHome
Hotkey "~Capslock & .", SendEnd
Hotkey "~Capslock & o", SendNextLine
CapsLock & c::^c
SendForwardWord(ThisHotKey) {
Send("{Ctrl Down}")
Send("{Right}")
Send("{Ctrl Up}")
}
SendBackWord(ThisHotKey) {
Send("{Ctrl Down}")
Send("{Left}")
Send("{Ctrl Up}")
}
;;not used
SendSwitchChinese(ThisHotKey) {
Send("{Alt Down}")
Send("{Shift Down}")
Send("{Shift Up}")
Send("{Alt Up}")
}
SendHome(ThisHotkey) {
Send("{Home}")
}
SendEnd(ThisHotkey) {
Send("{End}")
}
SendDeleteCurrentWord(ThisHotkey) {
;; delete current word
Send("{Ctrl Down}")
Send("{Left}")
Send("{Shift Down}")
Send("{Right}")
Send("{Ctrl Up}")
Send("{Shift Up}")
Send("{Delete}")
}
SendDeleteCurrentLine(ThisHotkey) {
;; delete current line
Send("{Home}")
Send("{Shift Down}")
Send("{End}")
Send("{Shift Up}")
Send("{Delete}")
Send("{Delete}")
}
SendNextLine(ThisHotKey) {
Send("{End}")
Send("{Enter}")
}
#z:: Run "https://www.autohotkey.com" ; Win+Z
SendLeft(ThisHotkey) {
Send("{Blind}{Left}")
}
SendRight(ThisHotkey) {
Send("{Blind}{Right}")
}
SendUp(ThisHotkey) {
Send("{Blind}{Up}")
}
SendUp10(ThisHotkey) {
Loop 10 {
SendUp(ThisHotkey)
}
}
SendDown(ThisHotkey) {
Send("{Blind}{Down}")
}
SendDown10(ThisHotkey) {
Loop 10 {
SendDown(ThisHotkey)
}
}
SendDelete(ThisHotkey) {
Send("{Delete}")
}
SendBackspace(ThisHotkey) {
Send("{Backspace}")
}
;;;;;;;;;;;;;;;;;;;;; Start of FUNCTION2 ;;;;;;;;;;;;;;;;;;;;
;;;;; 实现 ctrl + tab [+ shift] 映射到 alt + tab [+ shift] ;;;;
; Global variable to track if we're in the middle of a switching session
switchingApps := false
; Ctrl+Tab
^Tab::{
global switchingApps
if (!switchingApps) {
; Start the switching session
switchingApps := true
Send "{Alt Down}{Tab}"
} else {
; Continue cycling through apps
Send "{Tab}"
}
}
; Ctrl+Shift+Tab (for reverse cycling)
^+Tab::{
global switchingApps
if (!switchingApps) {
; Start the switching session (in reverse)
switchingApps := true
Send "{Alt Down}{Shift Down}{Tab}"
Send "{Shift Up}" ; Release Shift, but keep Alt down
} else {
; Continue cycling through apps in reverse
Send "+{Tab}"
}
}
; When Ctrl is released, end the switching session
~Ctrl Up::{
global switchingApps
if (switchingApps) {
switchingApps := false
Send "{Alt Up}"
}
}
; If Esc is pressed during a switching session, cancel it
~Esc::{
global switchingApps
if (switchingApps) {
switchingApps := false
Send "{Alt Up}"
}
}
;;;;;;;;;;;;;;;;;;;;; End of FUNCTION2 ;;;;;;;;;;;;;;;;;;;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment