Skip to content

Instantly share code, notes, and snippets.

@martinkozle
Created May 23, 2024 23:03
Show Gist options
  • Save martinkozle/6f622e36e5686751ef6ef90db33f4c36 to your computer and use it in GitHub Desktop.
Save martinkozle/6f622e36e5686751ef6ef90db33f4c36 to your computer and use it in GitHub Desktop.
Hyprland bind for switching all keyboard layouts, handling inconsistencies
# Switch keyboard layout (inline)
bind = $mainMod, SPACE, exec, switch=$(hyprctl devices -j | jq -r '.keyboards[] | .active_keymap' | uniq -c | [ $(wc -l) -eq 1 ] && echo "next" || echo "0"); for device in $(hyprctl devices -j | jq -r '.keyboards[] | .name'); do hyprctl switchxkblayout $device $switch; done; activeKeymap=$(hyprctl devices -j | jq -r '.keyboards[0] | .active_keymap'); if [ $switch == "0" ]; then resetStr="(reset)"; else resetStr=""; fi; hyprctl notify -1 1500 0 "$activeKeymap $resetStr";
# Switch keyboard layout (shell script)
bind = $mainMod, SPACE, exec, ~/somewhere/switch_layout.sh
# switch gets set to "next" if all current active keymaps are the same
# if there is more than 1 unique active keymap for any keyboard (like when plugging in a new keyboard), then it gets set to "0"
# this will reset all keyboards to the 0th index layout so they become consistent
switch=$(hyprctl devices -j | jq -r '.keyboards[] | .active_keymap' | uniq -c | [ $(wc -l) -eq 1 ] && echo "next" || echo "0")
# change the layout of every keyboard in hyprland devices
for device in $(hyprctl devices -j | jq -r '.keyboards[] | .name')
do
hyprctl switchxkblayout $device $switch
done
# get the first keyboard's active keymap
activeKeymap=$(hyprctl devices -j | jq -r '.keyboards[0] | .active_keymap')
# if switch is "0" then add (reset) to the end of the notification
if [ $switch == "0" ]
then
resetStr="(reset)"
else
resetStr=""
fi
# notify which keyboard layout has been changed (tweak the duration to liking)
hyprctl notify -1 1500 0 "$activeKeymap $resetStr"
@martinkozle
Copy link
Author

Since newer versions of Hyprland, kb_options = grp:win_space_toggle seems to be working now. I have changed my bind to only show a notification now when I change the layout.

# Notify keyboard layout
bindl = $mainMod, SPACE, exec, hyprctl notify -1 1500 0 $(hyprctl devices -j | jq -r '.keyboards[] | select(.main == true) | first(.active_keymap)')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment