#!/bin/sh # Press Logo+Alt+Ctrl+$K to turn $K into a toggle key. # Pressing $K then alternates key down and up events. # Press Logo+Alt+Ctrl+$K again to return $K to a normal key. # Setup (in sway config); # dotool needs to be running for the virtual keyboard input to work # exec dotoold # usage: call this script from your sway config, with the key as the only argument: # exec ~/.config/sway/dotool_key_toggle w S="$0" K="$1" A="$2" I="$3" # necessary due to https://github.com/swaywm/sway/issues/6961 and our use of --input-device swaymsg seat '*' keyboard_grouping none keyup() { echo "keyup $K" | dotoolc; } keydn() { echo "keydown $K" | dotoolc; } thenup() { swaymsg -- bindsym --input-device="$I" "$K" exec "'$S' '$K' up '$I'"; } thendn() { swaymsg -- bindsym --input-device="$I" "$K" exec "'$S' '$K' dn '$I'"; } setup() { # unbind the key swaymsg -- unbindsym "$K" "$(dirname "$S")/dotool_other_keyboards" "$S" "$K" un # release the key to avoid holding it down forever keyup # bind the combination to `seton` swaymsg -- bindsym "Mod1+Mod4+Ctrl+$K" exec "'$S' '$K' on" } seton() { # bind the key "$(dirname "$S")/dotool_other_keyboards" "$S" "$K" bd # bind the combination to `setup` swaymsg -- bindsym "Mod1+Mod4+Ctrl+$K" exec "'$S' '$K' off" } case "$A" in bd) thendn;; up) keyup; thendn;; dn) keydn; thenup;; un) swaymsg -- unbindsym --input-device="$I" "$K";; on) seton;; *) setup;; esac