Windows——AutoHotKey
初次了解AutoHotKey,是在Win下最爱效率神器: AutoHotKey | 晚晴幽草轩这篇博客中,博主有对AutoHotKey作详细介绍,这里不在赘余。当时打字很慢,更苦于写代码时需要经常按方向键、Home/End等按键(当然这些问题有些编辑器可以解决),个人而言很是痛苦,直到遇到了AutoHotKey,解决了一大痛点,可以用它来重新布局我的键盘。看到了@冯若航大佬分享的AHK脚本AutoHotKey常用函数或小技巧有哪些分享?,稍微做了一丁点修改及删减,主要的按键映射如下图所示(其余功能见代码):
这里稍微说明一下:将CapsLock键映射为了ESC键,CapsLock键通过CapsLock + \来实现,图中的实现默认为和CapsLock组合按下时生效;另外,关于光标移动,比如CapsLock + H映射为Home键,若再同时按下Alt键,则可实现选中当前光标到Home键位置之间的文本,其余的比如CapsLock + Alt + U实现向前选中一个字词皆类似,依此类推(另外,还可按住CapsLock + Alt不松手,多次按下不同的光标移动映射按键来实现快速精确的选中文本)。
这里也把代码贴一下,如下CapsLock.ahk所示:
;—- CapsLock Initializer —-
SetCapsLockState, AlwaysOff
;—- CapsLock + \ === CapsLock —-
CapsLock & \::
GetKeyState, CapsLockState, CapsLock, T
if CapsLockState = D
SetCapsLockState, AlwaysOff
else
SetCapsLockState, AlwaysOn
KeyWait, \
return
;—- CapsLock === ESC —-
CapsLock::Send, {ESC}
;—- Editor —-
CapsLock & z:: Send, ^z ; Z = Cancel
CapsLock & x:: Send, ^x ; X = Cut
CapsLock & c:: Send, ^c ; C = Copy
CapsLock & v:: Send, ^v ; V = Paste
CapsLock & a:: Send, ^a ; A = Select All
CapsLock & g:: Send, ^y ; Y = Redo
;—- —- —- —-
CapsLock & f:: Send, ^f ; F = Find
CapsLock & w:: Send, ^w ; W = Close
CapsLock & s:: Send, ^s ; S = Save
CapsLock & e:: Send, !{F4} ; E = Exit
;—- CapsLock + D === Copy Current Line —-
CapsLock & d:: send, {Home}+{End}^c{End}
;—- CapsLock + R === AppsKey(Menu Key) —-
CapsLock & r:: Send, {AppsKey}
;—- CapsLock + T === Ctrl + Tab —-
CapsLock & t:: Send, ^{Tab}
;—- —- —- —-
CapsLock & =:: Send, ^{WheelUp}
CapsLock & -:: Send, ^{WheelDown}
CapsLock & Space:: Send, {Shift}
;—- Delete —-
CapsLock & ,:: Send, {Del} ; , = Del char after
CapsLock & .:: Send, ^{Del} ; . = Del word after
CapsLock & /:: Send, +{End}{Del} ; / = Del all after
CapsLock & m:: Send, {BS} ; m = Del char before
CapsLock & n:: Send, ^{BS} ; n = Del word before
CapsLock & b:: Send, +{Home}{Del} ; b = Del all before
;—- U === Word Before —-
CapsLock & u::
if getkeystate(“alt”) = 0
Send, ^{Left}
else
Send, ^+{Left}
return
;—- O === Word After —-
CapsLock & o::
if getkeystate(“alt”) = 0
Send, ^{Right}
else
Send, ^+{Right}
return
;—- P === PageDown —-
CapsLock & p::
if getkeystate(“alt”) = 0
Send, {PgDn}
else
Send, +{PgDn}
return
;—- Y === PageUp —-
CapsLock & y::
if getkeystate(“alt”) = 0
Send, {PgUp}
else
Send, +{PgUp}
return
;—- H === Home —-
CapsLock & h::
if getkeystate(“alt”) = 0
Send, {Home}
else
Send, +{Home}
return
;—- ; === End —-
CapsLock & `;::
if getkeystate(“alt”) = 0
Send, {End}
else
Send, +{End}
return
;—- J === Left —-
CapsLock & j::
if getkeystate(“alt”) = 0
Send, {Left}
else
Send, +{Left}
return
;—- K === Down —-
CapsLock & k::
if getkeystate(“alt”) = 0
Send, {Down}
else
Send, +{Down}
return
;—- I === Up —-
CapsLock & i::
if getkeystate(“alt”) = 0
Send, {Up}
else
Send, +{Up}
return
;—- L === Right —-
CapsLock & l::
if getkeystate(“alt”) = 0
Send, {Right}
else
Send, +{Right}
return
;—- Mouse Controller —-
CapsLock & Up:: MouseMove, 0, -10, 0, R ;Mouse Up
CapsLock & Down:: MouseMove, 0, 10, 0, R ;Mouse Down
CapsLock & Left:: MouseMove, -10, 0, 0, R ;Mouse Left
CapsLock & Right:: MouseMove, 10, 0, 0, R ;Mouse Right
;—- CapsLock + Enter(Push Release) === Mouse Left Push(Release) —-
CapsLock & Enter::
SendEvent {Blind}{LButton down}
KeyWait Enter
SendEvent {Blind}{LButton up}
return
;—- Scroll Left —-
CapsLock & WheelUp:: ; 向左滚动
ControlGetFocus, fcontrol, A
Loop 2 ;
SendMessage, 0x114, 0, 0, %fcontrol%, A ; 0x114 是 WM_HSCROLL, 它后面的 0 是 SB_LINELEFT
return
;—- Scroll Right —-
CapsLock & WheelDown:: ; 向右滚动
ControlGetFocus, fcontrol, A
Loop 2 ;
SendMessage, 0x114, 1, 0, %fcontrol%, A ; 0x114 是 WM_HSCROLL, 它后面的 1 是 SB_LINERIGHT
return
Linux——Xmodmap
但在使用Linux时,没有了AutoHotKey的按键映射,很不习惯,最后找到了Xmodmap的解决方案。参考链接:Xmodmap (简体中文),这里也把配置文件~/.Xmodmap分享出来,如下所示:
!– xmodmap ~/.Xmodmap –!
! Caps_Lock -> Mode_switch
clear lock
add mod5 = Caps_Lock
keycode 66 = Mode_switch
! Caps_Lock + space -> Escape
keycode 65 = space NoSymbol Escape
! Caps_Lock + r -> Menu
keycode 27 = r R Menu
! Caps_Lock + , -> Delete
keycode 59 = comma less Delete
! Caps_Lock + m -> BackSpace
keycode 58 = m M BackSpace
! Caps_Lock + y -> Prior
keycode 29 = y Y Prior
! Caps_Lock + p -> Next
keycode 33 = p P Next
! Caps_Lock + i -> Up
keycode 31 = i I Up
! Caps_Lock + k -> Down
keycode 45 = k K Down
! Caps_Lock + j -> Left
keycode 44 = j J Left
! Caps_Lock + l -> Right
keycode 46 = l L Right
! Caps_Lock + h -> Home
keycode 43 = h H Home
! Caps_Lock + ; -> End
keycode 47 = semicolon colon End
具体的功能实现过程可参考上述链接Xmodmap以及Xmodmap Command。不足之处:牺牲了CapsLock键,并且无法实现组合键的映射,仅仅实现了以上的部分单个按键的映射(具体仅包含以下按键:Escape、Menu、Delete、BackSpace、PgUp、PgDn、Up、Down、Left、Right、Home、End)。