[效率] HHKB鍵盤 + Autohotkey 配置祕籍

weixin_33766168發表於2018-04-08

由於已經厭倦了機械鍵盤,又實在無法抵擋 HHKB 的顏值,入手了一枚 hhkb pro2。

圖片描述

入手之後立刻傻眼,方向鍵不僅需要按 Fn 鍵才能觸發,那憋屈的鍵位讓我這用方向鍵與 Ctrl+C 一樣多的程式猿情何以堪!好在我是程式猿,天生不怕折騰,在經過設定 DIP 開關、使用 Autohotkey 改鍵、設定 Win10 系統許可權等一系列的折騰之後,終於可以舒服的使用這款 HHKB 寫程式碼了,效率更超之前的機械鍵盤。

設定 DIP 開關

SW1 on、SW2 off = Lite ext 模式,既 ◇ 鍵為 Win 鍵。
SW3 on = Delete 鍵改為退格鍵。
SW4 on = 左側 ◇ 鍵為 Fn 鍵。
SW5 off = 不交換 ◇ 與 Alt 鍵。
SW6 on = 啟用喚醒功能。

使用 Autohotkey 改鍵

首先要解決方向鍵問題。我並不是 Emacs/Vim 黨,想來想去還是把“上下左右”的快捷鍵設定成 Ctrl + I、K、J、L 比較直觀。另外,編程式碼時跳到行首、行尾的操作也很多,所以可以再加上 Ctrl + H 跳到行首,Ctrl + ' 跳到行尾的快捷鍵。指令碼也很簡單:

^j::Send,{Left}
^l::Send,{Right}
^i::Send,{Up}
^k::Send,{Down}
^h::Send,{Home}
^'::Send,{End}

但是,僅僅這樣並沒有比原來方便,既然進入了雙手不離開主鍵盤區的領域,就要儘可能減少使用滑鼠才能提高效率,畢竟現在要實現按方向鍵已經必須使用兩隻手了。編程式碼選中變數名的操作很多,我們已經實現了 Ctrl + I、K、J、L 上下左右移動游標,如果能實現 Ctrl + ◇ + I、K、J、L 上下左右選中文字就非常方便而且直觀了!觀察一下 HHKB 的鍵盤,正好 ◇ + I、K、J、L 等價於 PrintScreen、Home、小鍵盤的除號、PageUp 鍵,所以就再增加如下指令碼程式碼:

^PrintScreen up::Send,{RShift down}{Up}{RShift up}
^Home up::Send,{RShift down}{Down}{RShift up}
^NumpadDiv up::Send,+{Left}
^PgUp up::Send,{RShift down}{Right}{RShift up}

注意在每個快捷鍵後面都加上了 “up”。這是因為在測試時發現,如果 Ctrl + ◇ + J 按住的話,也就是希望游標以最快速度往左側選中文字的時候,每選中5、6個字母,選中的字母就會被一個 “/” 字母替換掉!也就是本來應該連續輸出 Shift + Left,卻偶爾直接輸出了 “/”。而快捷鍵後面加上 “up” 的意思是不允許按住,只允許一下一下按快捷鍵。這樣雖然不會出錯了,但是這一下一下按效率實在太低了。經過反覆嘗試,我找到一個祕技:先使用 “NumpadDiv::CtrlBreak” 和 “PgUp::CtrlBreak” 把要輸出的快捷鍵改為不會實際輸出字元的“CtrlBreak”鍵,就不怕鍵衝突了。實際指令碼這個樣子:

NumpadDiv::CtrlBreak
PgUp::CtrlBreak

^NumpadMult up::Send,{RShift down}{Home}{RShift up}
^NumpadDiv::Send,+{Left}
^Home up::Send,{RShift down}{Down}{RShift up}
^PgUp::Send,{RShift down}{Right}{RShift up}
^PrintScreen up::Send,{RShift down}{Up}{RShift up}
^Right up::Send,{RShift down}{End}{RShift up}
^NumpadSub up::Send,{RControl down}{Left}{RControl up}{RControl down}{RShift down}{Right}{RControl up}{RShift up} ;select whole word left to right
^End up::Send,{RControl down}{RShift down}{Left}{RControl up}{RShift up} ;select whole word left
^PgDn up::Send,{RControl down}{RShift down}{Right}{RControl up}{RShift up} ;select whole word right
^NumpadAdd up::Send,{Home}+{End}

上面的指令碼還同時實現了 Ctrl + M 選中單詞,Ctrl + N 選中行,Ctrl + < 向左按單詞擴選,Ctrl + > 向右按單詞擴選。最棒的還是可以允許按住 Ctrl + ◇ + J 和 Ctrl + ◇ + L 來快速擴選了,代價是犧牲了小鍵盤除號和PageUp鍵,不過可以接受。

另外像把變數首字母由大寫改為小寫這樣的功能,雖然不是很常用,但也能有效提升效率,我把快捷鍵設定為 Ctrl + 反引號,實現起來稍稍有點複雜但也不難。使用一段時間,做了些優化和微調,最終的Autohotkey指令碼:

NumpadDiv::CtrlBreak
PgUp::CtrlBreak

^j::Send,{Left}
^l::Send,{Right}
^i::Send,{Up}
^k::Send,{Down}
^h::Send,{Home}
^'::Send,{End}

^NumpadMult up::Send,{RShift down}{Home}{RShift up}
^NumpadDiv::Send,+{Left}
^Home up::Send,{RShift down}{Down}{RShift up}
^PgUp::Send,{RShift down}{Right}{RShift up}
^PrintScreen up::Send,{RShift down}{Up}{RShift up}
^Right up::Send,{RShift down}{End}{RShift up}
^NumpadSub up::Send,{RControl down}{Left}{RControl up}{RControl down}{RShift down}{Right}{RControl up}{RShift up} ;select whole word left to right
^End up::Send,{RControl down}{RShift down}{Left}{RControl up}{RShift up} ;select whole word left
^PgDn up::Send,{RControl down}{RShift down}{Right}{RControl up}{RShift up} ;select whole word right
^NumpadAdd up::Send,{Home}+{End}

RWin & '::Send,{RWin down}{RControl down}{Right}{RWin up}{RControl up}
RWin & `;::Send,{RWin down}{RControl down}{Left}{RWin up}{RControl up}

+Esc::Send,{RAlt down}{Left}{RAlt up}
+Tab::Send,{RAlt down}{Right}{RAlt up}

; Ctrl + ` set firt char to lower
^`::
    clipBak := ClipboardAll ; bak clipboard
    Clipboard := "" ;clear clipboard
    Send,{RControl down}{Left}{RControl up}{RShift down}{Right}{RShift up}{RControl down}c{RControl up} ;copy first char to clipboard
    ClipWait, 1 ;wait clip complete
    ; convert firt char in clipboard to lower
    selText := Clipboard
    ;MsgBox % selText
    StringLower, selText, selText
    ; set lower char to clipboard and paste it to replace in place
    Clipboard := selText
    Send, ^v
    Sleep, 100 ;prevent restore clipBak too early
    Clipboard := clipBak ; restore clipboard
    Send, {RControl down}{Right}{RControl up}
return

;Ignore these shortkey
^1::return
^2::return
^3::return
^4::return
^5::return
^6::return
^7::return
^8::return
^9::return
^0::return
^-::return
^=::return
^\::return
^Left::return

設定 Win10 許可權

到目前為止似乎一切都很完美,但是開啟 Visual Studio,突然發現在 VS 裡面剛剛設定的所有快捷鍵全!失!效!!一開始還以為是 VS 把全域性快捷鍵給遮蔽了,想找找能不能通過 VS 裡面的設定不遮蔽全域性快捷鍵,結果無功而返,感覺怕是解決不了了。後來還是在靠譜的 Stackoverflow 裡面找到了答案。原來是因為 VS 執行於管理員許可權,而 Autohotkey 執行於普通使用者許可權。解決方法就是在 AutoHotkeyU64.exe(如果是64位作業系統的話)右擊,選“屬性”,在“相容性”選項卡里,勾選“以管理員身份執行此程式”。

終於解決了 VS 快捷鍵失效的問題,但是馬上又發現以管理員身份執行 Autohotkey 會造成它不能開機自動啟動。解決方法是禁用 UAC。禁用 UAC 的方法是:Win+R,輸入gpedit.msc,執行開啟“本地組策略編輯器”,計算機配置->Windows設定->安全設定->本地策略->安全選項->以管理員批准模式執行所有管理員,改為"已禁用"即可。

相關文章