Turning capslock into controlock.

Many people recommend binding your capslock to control. While that is better than pressing the control key as it is placed on most keyboards (lonely and playing in the corner), having to hold it instead of just pressing places strain on your pinky. Since I'm lazy, I thought: why not make capslock into controlock - one press to activate control and hold it, another to disable it. Now there are ways to do this in Emacs only - like god mode, or controlock mode. But I wanted uniformity, and to never have to hold down the ctrl key again. Thus, I created a script that uses xdotool to keep control pressed, and release it when needed. The script can be found at my gitlab, but I will also include it here to not create a single point of failure

#!/bin/bash
# Make control get pressed down and held, or
# released depending on state. Uses xdotool.
FILE=/tmp/controlock
if ! [ -e "$FILE" ]
then
    touch "$FILE"
    echo "off" > "$FILE" 2>&1
fi

if grep -q -we "^on$" < "$FILE"
then
    echo "turning off"
    xdotool keyup "Control"
    echo "off" > "$FILE" 2>&1
else
    echo "turning on"
    xdotool keydown "Control"
    echo "on" > "$FILE" 2>&1
fi

Save this script somewhere, and bind it to capslock (for example with .Xbindkeys). Please keep in mind that you will need to bind Caps​_Lock and Control + Caps​_Lock, for turning it on and off respectively. For me, this works:

# switch controlock
"~/scripts/controlock.sh"
Caps_Lock
# ditto, switching off because it turns into ctrl stuff
"~/scripts/controlock.sh"
Control + Caps_Lock

Big thanks to thblt at #emacs on freenode, for giving me a pointer on how to do this.

Author: wanko

Created: 2020-09-06 Sun 11:08

Home