Caps Lock and Insert keys mapped to Home and End (for laptops)

Post your working scripts, libraries and tools for AHK v1.1 and older
loopernow
Posts: 27
Joined: 17 Mar 2019, 14:18

Caps Lock and Insert keys mapped to Home and End (for laptops)

Post by loopernow » 03 Jun 2023, 11:10

My laptop keyboard does not have Home or End keys. This script maps the Caps Lock key to Home, and the Insert key to End. You can use the usual modifier keys (Ctrl, Shift, Ctrl+Shift) with both remapped keys to navigate and select text in a text editor as usual.

(And you can send Win+Home, the built-in Windows 10 keyboard shortcut to hide other windows)

To toggle Caps Lock, you can press either Caps Lock + ` (mnemonically easy to remember) or Caps Lock + ; (easy to press, neither hand leaves the home row keys).

Credit to the many scripts I reviewed to cobble this one together.

Code: Select all

; CapsLock + ` toggles CapsLock
SetCapsLockState, AlwaysOff
CapsLock & `::
GetKeyState, CapsLockState, CapsLock, T
if CapsLockState = D
    SetCapsLockState, AlwaysOff
else
    SetCapsLockState, AlwaysOn
KeyWait, ``
return

; CapsLock + ; toggles CapsLock
SetCapsLockState, AlwaysOff
CapsLock & SC027::
GetKeyState, CapsLockState, CapsLock, T
if CapsLockState = D
    SetCapsLockState, AlwaysOff
else
    SetCapsLockState, AlwaysOn
KeyWait, ``
return

; map Caps Lock key to Home key
$CapsLock::Send {Home}
$^CapsLock::Send ^{Home}			; ctrl+Caps Lock
$+CapsLock::Send +{Home}			; Shift+Caps Lock
$^+CapsLock::Send ^+{Home}			; ctrl+Shift+Caps Lock
$#CapsLock::Send #{Home}			; Win+Caps Lock

; map Insert key to End key
$Insert::Send {End}
$^Insert::Send ^{End}				; ctrl+End
$+Insert::Send +{End}				; Shift+End
$^+Insert::Send ^+{End}				; Ctrl+Shift+End
$#Insert::Send {Insert}				; Win+Insert sends Insert

Return to “Scripts and Functions (v1)”