feat(gnome): create custom keyboard shortcuts
Add script to automate creation of GNOME custom keyboard shortcuts. - Detect existing custom keybindings - Find the next available custom shortcut slot - Register new custom-keybinding entries automatically - Configure shortcut name, command, and key combination - Preserve existing GNOME keyboard shortcuts - Add usage documentation and examples - Include helper notes for discovering key names with xev - Simplify scripting and workstation setup automation
This commit is contained in:
1 parent
fbcbf008ea
commit
8d98c134d0
2 files changed
+124
-39
No files matched your search
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/bin/bash
|
||||
|
||||
###############################################################################
|
||||
# Script Name: add-gnome-shortcut.sh
|
||||
#
|
||||
# Description:
|
||||
# Creates a new custom keyboard shortcut in GNOME by automatically finding
|
||||
# the next available custom-keybinding slot and registering the shortcut.
|
||||
#
|
||||
# The script:
|
||||
# 1. Reads existing GNOME custom keyboard shortcuts.
|
||||
# 2. Finds the first unused custom<N> entry.
|
||||
# 3. Adds the new shortcut to the GNOME keybinding list.
|
||||
# 4. Sets the shortcut name.
|
||||
# 5. Sets the command to execute.
|
||||
# 6. Sets the keyboard shortcut combination.
|
||||
#
|
||||
# Requirements:
|
||||
# - GNOME Desktop Environment
|
||||
# - gsettings
|
||||
#
|
||||
# Usage:
|
||||
# ./add-gnome-shortcut.sh <name> <command> <key_combo>
|
||||
#
|
||||
# Arguments:
|
||||
# <name>
|
||||
# Display name shown in GNOME Settings.
|
||||
#
|
||||
# <command>
|
||||
# Command that will be executed when the shortcut is pressed.
|
||||
#
|
||||
# <key_combo>
|
||||
# GNOME keybinding string.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# Open Terminal:
|
||||
# ./add-gnome-shortcut.sh \
|
||||
# "Terminal" \
|
||||
# "gnome-terminal" \
|
||||
# "<Ctrl><Alt>T"
|
||||
#
|
||||
# Launch Firefox:
|
||||
# ./add-gnome-shortcut.sh \
|
||||
# "Firefox" \
|
||||
# "firefox" \
|
||||
# "<Super>F"
|
||||
#
|
||||
# Notes:
|
||||
# - Existing shortcuts are preserved.
|
||||
# - The next available custom-keybinding ID is selected automatically.
|
||||
# - Shortcut configuration can be viewed in:
|
||||
# Settings → Keyboard → Keyboard Shortcuts → Custom Shortcuts
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
if [ $# -lt 3 ]; then
|
||||
echo "Usage: $0 <new_name> <new_cmd> <key_combo>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
counter=0
|
||||
list="["
|
||||
num=0
|
||||
|
||||
# Find next available custom keybinding slot
|
||||
while read -r line; do
|
||||
list="$list$(echo "$line" | sed 's/\]//g' | sed 's/\[//g'), "
|
||||
|
||||
ret="$(printf "%s\n" "$line" | grep "custom$counter")"
|
||||
|
||||
if [ -z "$ret" ]; then
|
||||
num=$counter
|
||||
break
|
||||
else
|
||||
((counter++))
|
||||
num=$counter
|
||||
fi
|
||||
done < <(
|
||||
gsettings get \
|
||||
org.gnome.settings-daemon.plugins.media-keys \
|
||||
custom-keybindings \
|
||||
| sed 's/, /\n/g'
|
||||
)
|
||||
|
||||
key="org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"
|
||||
custom="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$num/"
|
||||
|
||||
if [[ $list = *"@as"* ]]; then
|
||||
list="['$custom']"
|
||||
else
|
||||
list="$list'$custom']"
|
||||
fi
|
||||
|
||||
echo "$list"
|
||||
|
||||
new_name=$1
|
||||
new_cmd=$2
|
||||
key_combo=$3
|
||||
|
||||
echo "$key$custom $new_name $new_cmd $key_combo"
|
||||
|
||||
# Optional helper for determining key names:
|
||||
#
|
||||
# key_str="$(xev)"
|
||||
# echo "$key_str" \
|
||||
# | sed 's/KeyRelease/\nKeyRelease/g' \
|
||||
# | sed 's/KeyPress/\nKeyPress/g' \
|
||||
# | grep KeyPress \
|
||||
# | sed 's/keysym/\nkeysym/g' \
|
||||
# | sed 's/same_screen/\nsame_screen/g' \
|
||||
# | grep keysym \
|
||||
# | grep -o -P '(?<=, ).*(?=\),)' \
|
||||
# | sed 's/_L//g' \
|
||||
# | sed 's/_R//g'
|
||||
|
||||
gsettings set \
|
||||
org.gnome.settings-daemon.plugins.media-keys \
|
||||
custom-keybindings \
|
||||
"$list"
|
||||
|
||||
gsettings set "$key$custom" name "$new_name"
|
||||
gsettings set "$key$custom" command "$new_cmd"
|
||||
gsettings set "$key$custom" binding "$key_combo"
|
||||
@@ -1,39 +0,0 @@
|
||||
#!/bin/bash
|
||||
if [ $# -lt 3 ]; then
|
||||
echo "Usage <new_name> <new_cmd> <key_combo>"
|
||||
exit 1;
|
||||
fi
|
||||
counter=0;
|
||||
list="["
|
||||
num=0
|
||||
while read -r line; do
|
||||
list="$list$(echo $line | sed 's/\]//g' | sed 's/\[//g'), "
|
||||
ret="$(printf "%s\n" "$line" | grep "custom$counter")"
|
||||
if [ "$ret" == "" ]; then
|
||||
num=$counter
|
||||
break;
|
||||
else
|
||||
(( counter++ ))
|
||||
num=$counter;
|
||||
fi
|
||||
done < <(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | sed 's/, /\n/g')
|
||||
key="org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"
|
||||
custom="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$num/"
|
||||
if [[ $list = *"@as"* ]]; then
|
||||
list="['$custom']"
|
||||
else
|
||||
list="$list'$custom']"
|
||||
fi
|
||||
echo $list
|
||||
new_name=$1
|
||||
new_cmd=$2
|
||||
key_combo=$3
|
||||
echo "$key$custom $new_name $new_cmd $key_combo"
|
||||
|
||||
#key_str="$(xev)"
|
||||
#echo $key_str | sed 's/KeyRelease/\nKeyRelease/g' | sed 's/KeyPress/\nKeyPress/g' | grep KeyPress | sed 's/keysym/\nkeysym/g'| sed 's/same_screen/\nsame_screen/g' | grep keysym | grep -o -P '(?<=, ).*(?=\),)' | sed 's/_L//g' | sed 's/_R//g'
|
||||
#exit
|
||||
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "$list"
|
||||
gsettings set $key$custom name "$new_name"
|
||||
gsettings set $key$custom command "$new_cmd"
|
||||
gsettings set $key$custom binding "$key_combo"
|
||||
Reference in new issue
Block a user