From 9fca21425ca2a8a04a87adb2015e19614701f7df Mon Sep 17 00:00:00 2001 From: rcmdnk Date: Sat, 28 Sep 2024 23:45:08 +0900 Subject: [PATCH] refactor: replace Send with SendInput for improved reliability in key input operations. refactor: remove `SendMode("Input")` setting as all Send was replaced by SendInput --- lib/bind/vim_command.ahk | 12 ++--- lib/bind/vim_enter_insert.ahk | 10 ++-- lib/bind/vim_normal.ahk | 28 +++++------ lib/bind/vim_replace.ahk | 8 +-- lib/bind/vim_search.ahk | 12 ++--- lib/bind/vim_visual.ahk | 22 ++++---- lib/bind/vim_ydcxp.ahk | 44 ++++++++-------- lib/util/vim_ahk_setting.ahk | 1 - lib/vim_move.ahk | 94 +++++++++++++++++------------------ lib/vim_state.ahk | 14 +++--- 10 files changed, 122 insertions(+), 123 deletions(-) diff --git a/lib/bind/vim_command.ahk b/lib/bind/vim_command.ahk index 47fc487..f1ce261 100644 --- a/lib/bind/vim_command.ahk +++ b/lib/bind/vim_command.ahk @@ -6,34 +6,34 @@ w::Vim.State.SetMode("Command_w") q::Vim.State.SetMode("Command_q") h:: { - Send("{F1}") + SendInput("{F1}") Vim.State.SetMode("Vim_Normal") } #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Command_w")) Enter:: { - Send("^s") + SendInput("^s") Vim.State.SetMode("Vim_Normal") } q:: { - Send("^s") - Send("!{F4}") + SendInput("^s") + SendInput("!{F4}") Vim.State.SetMode("Insert") } Space:: { - Send("!fa") + SendInput("!fa") Vim.State.SetMode("Insert") } #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Command_q")) Enter:: { - Send("!{F4}") + SendInput("!{F4}") Vim.State.SetMode("Insert") } diff --git a/lib/bind/vim_enter_insert.ahk b/lib/bind/vim_enter_insert.ahk index e136b89..d67a6ca 100644 --- a/lib/bind/vim_enter_insert.ahk +++ b/lib/bind/vim_enter_insert.ahk @@ -3,33 +3,33 @@ i::Vim.State.SetMode("Insert") +i:: { - Send("{Home}") + SendInput("{Home}") Vim.State.SetMode("Insert") } a:: { if(! Vim.CheckChr("`n")){ - Send("{Right}") + SendInput("{Right}") } Vim.State.SetMode("Insert") } +a:: { - Send("{End}") + SendInput("{End}") Vim.State.SetMode("Insert") } o:: { - Send("{End}{Enter}") + SendInput("{End}{Enter}") Vim.State.SetMode("Insert") } +o:: { - Send("{Home}{Enter}{Left}") + SendInput("{Home}{Enter}{Left}") Vim.State.SetMode("Insert") } diff --git a/lib/bind/vim_normal.ahk b/lib/bind/vim_normal.ahk index e07dd23..c44f9c2 100644 --- a/lib/bind/vim_normal.ahk +++ b/lib/bind/vim_normal.ahk @@ -1,24 +1,24 @@ #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Vim_Normal")) ; Undo/Redo -u::Send("^z") -^r::Send("^y") +u::SendInput("^z") +^r::SendInput("^y") ; Combine lines -+j:: Send("{End}{Space}{Delete}") ++j:: SendInput("{End}{Space}{Delete}") ; Change case ~:: { ClipSaved := ClipboardAll() A_Clipboard := "" - Send("+{Right}^x") + SendInput("+{Right}^x") ClipWait(1) if(isLower(A_Clipboard)){ A_Clipboard := StrUpper(A_Clipboard) }else if(isUpper(A_Clipboard)){ A_Clipboard := StrLower(A_Clipboard) } - Send("^v") + SendInput("^v") A_Clipboard := ClipSaved } @@ -26,31 +26,31 @@ u::Send("^z") #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Z")) +z:: { - Send("^s") - Send("!{F4}") + SendInput("^s") + SendInput("!{F4}") Vim.State.SetMode("Vim_Normal") } +q:: { - Send("!{F4}") + SendInput("!{F4}") Vim.State.SetMode("Vim_Normal") } ; period -.::Send("+^{Right}{BS}^v") +.::SendInput("+^{Right}{BS}^v") ; Q-dir #HotIf Vim.IsVimGroup() and WinActive("ahk_group VimQdir") and (Vim.State.Mode == "Vim_Normal") ; For Q-dir, ^X mapping does not work, use !X instead. ; ^X does not work to be sent, too, use Down/Up ; switch to left top (1), right top (2), left bottom (3), right bottom (4) -!u::Send("{LControl Down}{1 Down}{1 Up}{LControl Up}") -!i::Send("{LControl Down}{2 Down}{2 Up}{LControl Up}") -!j::Send("{LControl Down}{3 Down}{3 Up}{LControl Up}") -!k::Send("{LControl Down}{4 Down}{4 Up}{LControl Up}") +!u::SendInput("{LControl Down}{1 Down}{1 Up}{LControl Up}") +!i::SendInput("{LControl Down}{2 Down}{2 Up}{LControl Up}") +!j::SendInput("{LControl Down}{3 Down}{3 Up}{LControl Up}") +!k::SendInput("{LControl Down}{4 Down}{4 Up}{LControl Up}") ; Ctrl+q, menu Quick-links -'::Send("{LControl Down}{q Down}{q Up}{LControl Up}") +'::SendInput("{LControl Down}{q Down}{q Up}{LControl Up}") ; Keep the e key in Normal mode, use the right button and then press the refresh (e) function, do nothing, return to the e key directly ~e:: {} diff --git a/lib/bind/vim_replace.ahk b/lib/bind/vim_replace.ahk index b8c68ee..d8365c9 100644 --- a/lib/bind/vim_replace.ahk +++ b/lib/bind/vim_replace.ahk @@ -96,13 +96,13 @@ r::Vim.State.SetMode("r_once") ~>:: ~Space:: { - Send("{Del}") + SendInput("{Del}") Vim.State.SetMode("Vim_Normal") } ::: ; ":" can't be used with "~"? { - Send("{:}{Del}") + SendInput("{:}{Del}") Vim.State.SetMode("Vim_Normal") } @@ -200,12 +200,12 @@ r::Vim.State.SetMode("r_once") ~>:: ~Space:: { - Send("{Del}") + SendInput("{Del}") } ::: { - Send("{:}{Del}") + SendInput("{:}{Del}") } #HotIf diff --git a/lib/bind/vim_search.ahk b/lib/bind/vim_search.ahk index e9ff475..bd1346d 100644 --- a/lib/bind/vim_search.ahk +++ b/lib/bind/vim_search.ahk @@ -1,7 +1,7 @@ #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Vim_Normal")) /:: { - Send("^f") + SendInput("^f") Vim.State.SetMode("Insert") } @@ -9,15 +9,15 @@ { ClipSaved := ClipboardAll() A_Clipboard := "" - Send("^{Left}+^{Right}^c") + SendInput("^{Left}+^{Right}^c") ClipWait(1) - Send("^f") - Send("^v!f") + SendInput("^f") + SendInput("^v!f") A_Clipboard := ClipSaved Vim.State.SetMode("Insert") } -n::Send("{F3}") -+n::Send("+{F3}") +n::SendInput("{F3}") ++n::SendInput("+{F3}") #HotIf diff --git a/lib/bind/vim_visual.ahk b/lib/bind/vim_visual.ahk index 98a3e95..02a4f66 100644 --- a/lib/bind/vim_visual.ahk +++ b/lib/bind/vim_visual.ahk @@ -3,14 +3,14 @@ v::Vim.State.SetMode("Vim_VisualChar") ^v:: { - Send("^b") + SendInput("^b") Vim.State.SetMode("Vim_VisualChar") } +v:: { Vim.State.SetMode("Vim_VisualLineFirst") - Send("{Home}+{Down}") + SendInput("{Home}+{Down}") } #HotIf Vim.IsVimGroup() and (Vim.State.StrIsInCurrentVimMode("Visual")) @@ -20,10 +20,10 @@ v::Vim.State.SetMode("Vim_Normal") y:: { A_Clipboard := "" - Send("^c") - Send("{Right}") + SendInput("^c") + SendInput("{Right}") if WinActive("ahk_group VimCursorSameAfterSelect"){ - Send("{Left}") + SendInput("{Left}") } ClipWait(1) if(Vim.State.StrIsInCurrentVimMode("Line")){ @@ -36,7 +36,7 @@ y:: d:: { A_Clipboard := "" - Send("^x") + SendInput("^x") ClipWait(1) if(Vim.State.StrIsInCurrentVimMode("Line")){ Vim.State.SetMode("Vim_Normal", 0, 0, 1) @@ -48,7 +48,7 @@ d:: x:: { A_Clipboard := "" - Send("^x") + SendInput("^x") ClipWait(1) if(Vim.State.StrIsInCurrentVimMode("Line")){ Vim.State.SetMode("Vim_Normal", 0, 0, 1) @@ -60,7 +60,7 @@ x:: c:: { A_Clipboard := "" - Send("^x") + SendInput("^x") ClipWait(1) if(Vim.State.StrIsInCurrentVimMode("Line")){ Vim.State.SetMode("Insert", 0, 0, 1) @@ -73,10 +73,10 @@ c:: { ClipSaved := ClipboardAll() A_Clipboard := "" - Send("^c") + SendInput("^c") ClipWait(1) - Send("^f") - Send("^v!f") + SendInput("^f") + SendInput("^v!f") A_Clipboard := ClipSaved Vim.State.SetMode("Vim_Normal") } diff --git a/lib/bind/vim_ydcxp.ahk b/lib/bind/vim_ydcxp.ahk index c8799f5..0ad016e 100644 --- a/lib/bind/vim_ydcxp.ahk +++ b/lib/bind/vim_ydcxp.ahk @@ -7,15 +7,15 @@ c::Vim.State.SetMode("Vim_ydc_c", 0, -1, 0) Vim.State.SetMode("Vim_ydc_y", 0, 0, 1) Sleep(150) ; Need to wait (For variable change?) if WinActive("ahk_group VimDoubleHomeGroup"){ - Send("{Home}") + SendInput("{Home}") } - Send("{Home}+{End}") + SendInput("{Home}+{End}") if not WinActive("ahk_group VimLBSelectGroup"){ Vim.Move.Move("l") }else{ Vim.Move.Move("") } - Send("{Left}{Home}") + SendInput("{Left}{Home}") } +d:: @@ -24,7 +24,7 @@ c::Vim.State.SetMode("Vim_ydc_c", 0, -1, 0) if not WinActive("ahk_group VimLBSelectGroup"){ Vim.Move.Move("$") }else{ - Send("{Shift Down}{End}{Left}") + SendInput("{Shift Down}{End}{Left}") Vim.Move.Move("") } } @@ -35,7 +35,7 @@ c::Vim.State.SetMode("Vim_ydc_c", 0, -1, 0) if not WinActive("ahk_group VimLBSelectGroup"){ Vim.Move.Move("$") }else{ - Send("{Shift Down}{End}{Left}") + SendInput("{Shift Down}{End}{Left}") Vim.Move.Move("") } } @@ -44,7 +44,7 @@ c::Vim.State.SetMode("Vim_ydc_c", 0, -1, 0) y:: { Vim.Move.YDCMove() - Send("{Left}{Home}") + SendInput("{Left}{Home}") } #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Vim_ydc_d")) @@ -54,26 +54,26 @@ d::Vim.Move.YDCMove() c::Vim.Move.YDCMove() #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Vim_Normal")) -x::Send("{Delete}") -+x::Send("{BS}") +x::SendInput("{Delete}") ++x::SendInput("{BS}") ; Paste #HotIf Vim.IsVimGroup() and (Vim.State.IsCurrentVimMode("Vim_Normal")) p:: { ;i:=0 - ;;Send("{p Up}") + ;;SendInput("{p Up}") ;Loop { ; if !GetKeyState("p", "P"){ ; break ; } ; if(Vim.State.LineCopy == 1){ - ; Send("{End}{Enter}^v{BS}{Home}") + ; SendInput("{End}{Enter}^v{BS}{Home}") ; }else{ - ; Send("{Right}") - ; Send("^v") + ; SendInput("{Right}") + ; SendInput("^v") ; ;Sleep(1000) - ; Send("^{Left}") + ; SendInput("^{Left}") ; } ; ;TrayTip,i,%i%, ; if(i == 0){ @@ -89,16 +89,16 @@ p:: ;} if(Vim.State.LineCopy == 1){ if WinActive("ahk_group VimNoLBCopyGroup"){ - Send("{End}{Enter}^v{Home}") + SendInput("{End}{Enter}^v{Home}") }else{ - Send("{End}{Enter}^v{BS}{Home}") + SendInput("{End}{Enter}^v{BS}{Home}") } }else{ - Send("{Right}") - Send("^v") + SendInput("{Right}") + SendInput("^v") ;Sleep(1000) - Send("{Left}") - ;;Send("^{Left}") + SendInput("{Left}") + ;;SendInput("^{Left}") } KeyWait("p") ; To avoid repeat, somehow it calls , print... } @@ -106,10 +106,10 @@ p:: +p:: { if(Vim.State.LineCopy == 1){ - Send("{Up}{End}{Enter}^v{BS}{Home}") + SendInput("{Up}{End}{Enter}^v{BS}{Home}") }else{ - Send("^v") - ;Send("^{Left}") + SendInput("^v") + ;SendInput("^{Left}") } KeyWait("p") } diff --git a/lib/util/vim_ahk_setting.ahk b/lib/util/vim_ahk_setting.ahk index bd32bbb..c07bd3a 100644 --- a/lib/util/vim_ahk_setting.ahk +++ b/lib/util/vim_ahk_setting.ahk @@ -4,4 +4,3 @@ InstallKeybdHook(true) ; For checking key history ; Use ~500kB memory? A_HotkeyInterval := 2000 ; Hotkey interval (default 2000 milliseconds). A_MaxHotkeysPerInterval := 70 ; Max hotkeys per interval (default 50). -SendMode("Input") ; Recommended for new scripts due to its superior speed and reliability. diff --git a/lib/vim_move.ahk b/lib/vim_move.ahk index 827ace5..59d2f71 100644 --- a/lib/vim_move.ahk +++ b/lib/vim_move.ahk @@ -8,13 +8,13 @@ this.shift := 0 if(this.Vim.State.StrIsInCurrentVimMode("Visual") or this.Vim.State.StrIsInCurrentVimMode("ydc")){ this.shift := 1 - Send("{Shift Down}") + SendInput("{Shift Down}") } if(this.Vim.State.IsCurrentVimMode("Vim_VisualLineFirst")) and (Key == "k" or Key == "^u" or Key == "^b" or Key == "g"){ - Send("{Shift Up}{End}") + SendInput("{Shift Up}{End}") this.Zero() - Send("{Shift Down}") + SendInput("{Shift Down}") this.Up() this.vim.state.setmode("Vim_VisualLine") } @@ -25,63 +25,63 @@ if(this.Vim.State.StrIsInCurrentVimMode("Vim_ydc")) and (Key == "k" or Key == "^u" or Key == "^b" or Key == "g"){ this.Vim.State.LineCopy := 1 - Send("{Shift Up}") + SendInput("{Shift Up}") this.Zero() this.Down() - Send("{Shift Down}") + SendInput("{Shift Down}") this.Up() } if(this.Vim.State.StrIsInCurrentVimMode("Vim_ydc")) and (Key == "j" or Key == "^d" or Key == "^f" or Key == "+g"){ this.Vim.State.LineCopy := 1 - Send("{Shift Up}") + SendInput("{Shift Up}") this.Zero() - Send("{Shift Down}") + SendInput("{Shift Down}") this.Down() } } MoveFinalize(){ - Send("{Shift Up}") + SendInput("{Shift Up}") ydc_y := false if(this.Vim.State.StrIsInCurrentVimMode("ydc_y")){ A_Clipboard := "" - Send("^c") + SendInput("^c") ClipWait(1) this.Vim.State.SetMode("Vim_Normal") ydc_y := true }else if(this.Vim.State.StrIsInCurrentVimMode("ydc_d")){ A_Clipboard := "" - Send("^x") + SendInput("^x") ClipWait(1) this.Vim.State.SetMode("Vim_Normal") }else if(this.Vim.State.StrIsInCurrentVimMode("ydc_c")){ A_Clipboard := "" - Send("^x") + SendInput("^x") ClipWait(1) this.Vim.State.SetMode("Insert") } this.Vim.State.SetMode("", 0, 0) if(ydc_y){ - Send("{Left}{Right}") + SendInput("{Left}{Right}") } ; Sometimes, when using `c`, the control key would be stuck down afterwards. ; This forces it to be up again afterwards. - send("{Ctrl Up}") + SendInput("{Ctrl Up}") } Zero(){ if WinActive("ahk_group VimDoubleHomeGroup"){ - Send("{Home}") + SendInput("{Home}") } - Send("{Home}") + SendInput("{Home}") } Up(n:=1){ Loop n { if WinActive("ahk_group VimCtrlUpDownGroup"){ - Send("^{Up}") + SendInput("^{Up}") } else { - Send("{Up}") + SendInput("{Up}") } } } @@ -89,9 +89,9 @@ Down(n:=1){ Loop n { if WinActive("ahk_group VimCtrlUpDownGroup"){ - Send("^{Down}") + SendInput("^{Down}") } else { - Send("{Down}") + SendInput("{Down}") } } } @@ -110,69 +110,69 @@ ; 1 character if(Key == "h"){ if WinActive("ahk_group VimQdir"){ - Send("{BackSpace down}{BackSpace up}") + SendInput("{BackSpace down}{BackSpace up}") } else { - Send("{Left}") + SendInput("{Left}") } }else if(Key == "l"){ if WinActive("ahk_group VimQdir"){ - Send("{Enter}") + SendInput("{Enter}") } else { - Send("{Right}") + SendInput("{Right}") } ; Home/End }else if(Key == "0"){ this.Zero() }else if(Key == "$"){ if(this.shift == 1){ - Send("+{End}") + SendInput("+{End}") }else{ - Send("{End}") + SendInput("{End}") } }else if(Key == "^"){ if(this.shift == 1){ if WinActive("ahk_group VimCaretMove"){ - Send("+{Home}") - Send("+^{Right}") - Send("+^{Left}") + SendInput("+{Home}") + SendInput("+^{Right}") + SendInput("+^{Left}") }else{ - Send("+{Home}") + SendInput("+{Home}") } }else{ if WinActive("ahk_group VimCaretMove"){ - Send("{Home}") - Send("^{Right}") - Send("^{Left}") + SendInput("{Home}") + SendInput("^{Right}") + SendInput("^{Left}") }else{ - Send("{Home}") + SendInput("{Home}") } } ; Words }else if(Key == "w"){ if(this.shift == 1){ - Send("+^{Right}") + SendInput("+^{Right}") }else{ - Send("^{Right}") + SendInput("^{Right}") } }else if(Key == "e"){ if(this.shift == 1){ if(this.Vim.CheckChr(" ")){ - Send("+^{Right}") + SendInput("+^{Right}") } - Send("+^{Right}+{Left}") + SendInput("+^{Right}+{Left}") }else{ if(this.Vim.CheckChr(" ")){ - Send("^{Right}") + SendInput("^{Right}") } - Send("^{Right}{Left}") + SendInput("^{Right}{Left}") } }else if(Key == "b"){ if(this.shift == 1){ - Send("+^{Left}") + SendInput("+^{Left}") }else{ - Send("^{Left}") + SendInput("^{Left}") } } } @@ -188,13 +188,13 @@ }else if(Key == "^d"){ this.Down(10) }else if(Key == "^b"){ - Send("{PgUp}") + SendInput("{PgUp}") }else if(Key == "^f"){ - Send("{PgDn}") + SendInput("{PgDn}") }else if(Key == "g"){ - Send("^{Home}") + SendInput("^{Home}") }else if(Key == "+g"){ - Send("^{End}{Home}") + SendInput("^{End}{Home}") } if(!Repeat){ @@ -216,12 +216,12 @@ YDCMove(){ this.Vim.State.LineCopy := 1 this.Zero() - Send("{Shift Down}") + SendInput("{Shift Down}") if(this.Vim.State.n == 0){ this.Vim.State.n := 1 } this.Down(this.Vim.State.n - 1) - Send("{End}") + SendInput("{End}") if not WinActive("ahk_group VimLBSelectGroup"){ this.Move("l") }else{ diff --git a/lib/vim_state.ahk b/lib/vim_state.ahk index 90da68b..cc0cbc1 100644 --- a/lib/vim_state.ahk +++ b/lib/vim_state.ahk @@ -78,16 +78,16 @@ this.LastIME := VIM_IME_Get() if(this.LastIME){ if(VIM_IME_GetConverting("A")){ - Send("{Esc}") + SendInput("{Esc}") Return }else{ VIM_IME_SET() } } if(this.StrIsInCurrentVimMode("Visual") or this.StrIsInCurrentVimMode("ydc")){ - Send("{Right}") + SendInput("{Right}") if WinActive("ahk_group VimCursorSameAfterSelect"){ - Send("{Left}") + SendInput("{Left}") } } this.SetMode("Vim_Normal") @@ -99,7 +99,7 @@ HandleEsc(){ if (!this.Vim.Conf["VimEscNormal"]["val"]) { - Send("{Esc}") + SendInput("{Esc}") Return } ; The keywait waits for esc to be released. If it doesn't detect a release @@ -107,7 +107,7 @@ ShortPress := KeyWait("Esc", "T0.5") SetNormal := this.Vim.Conf["VimLongEscNormal"]["val"] != ShortPress if (!SetNormal or (this.Vim.Conf["VimSendEscNormal"]["val"] && this.IsCurrentVimMode("Vim_Normal"))) { - Send("{Esc}") + SendInput("{Esc}") } if (SetNormal) { this.SetNormal() @@ -121,13 +121,13 @@ HandleCtrlBracket(){ if (!this.Vim.Conf["VimCtrlBracketNormal"]["val"]) { - Send("^[") + SendInput("^[") Return } ShortPress := KeyWait("[", "T0.5") SetNormal := this.Vim.Conf["VimLongCtrlBracketNormal"]["val"] != ShortPress if (!SetNormal or (this.Vim.Conf["VimSendCtrlBracketNormal"]["val"] && this.IsCurrentVimMode("Vim_Normal"))) { - Send("^[") + SendInput("^[") } if (SetNormal) { this.SetNormal()