核心思路 Compton 本身不提供快捷键功能,需要在窗口管理器(如 i3、sway、awesome)里绑定键盘,通过执行 compton 命令或切换配置文件来改变透明、模糊等效果。也就是说,快捷键由窗口管理器处理,Compton 负责执行渲染效果。
i3 与 Sway 的配置示例
# 将当前会话的窗口不透明度设为 50%
bindsym $mod+Shift+t exec --no-startup-id compton --config /path/to/compton.conf --backend glx --alpha-threshold 1 --opacity 0.5
i3-msg reloadexec 执行命令):# 切换为“透明”预设
bindsym $mod+t exec --no-startup-id compton --config ~/.config/compton/transparent.conf
# 切换为“不透明”预设
bindsym $mod+Shift+t exec --no-startup-id compton --config ~/.config/compton/opaque.conf
swaymsg reload切换配置的正确姿势
#!/usr/bin/env bash
PIDFILE="$XDG_RUNTIME_DIR/compton.pid"
CONF_DIR="$HOME/.config/compton"
start() {
if [[ -f "$PIDFILE" ]] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
echo "Compton 已在运行 (PID $(cat "$PIDFILE"))"
return 1
fi
compton --config "$CONF_DIR/$(cat "$CONF_DIR/current 2>/dev/null || echo default.conf)" &
echo $! > "$PIDFILE"
}
stop() {
if [[ -f "$PIDFILE" ]]; then
kill "$(cat "$PIDFILE")" && rm -f "$PIDFILE"
fi
}
case "$1" in
start) start ;;
stop) stop ;;
toggle)
if [[ -f "$PIDFILE" ]] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
stop && echo "已停止"
else
start && echo "已启动"
fi
;;
*) echo "用法: $0 {start|stop|toggle}"; exit 1 ;;
esac
chmod +x /usr/local/bin/toggle-comptonbindsym $mod+Shift+t exec --no-startup-id toggle-compton toggle
# 设为透明预设
ln -sf ~/.config/compton/transparent.conf ~/.config/compton/current
toggle-compton toggle
# 设为不透明预设
ln -sf ~/.config/compton/opaque.conf ~/.config/compton/current
toggle-compton toggle
bindsym $mod+Shift+t exec --no-startup-id killall compton; compton --config ~/.config/compton/transparent.conf
常见问题与排查
killall compton 再启动,或使用上面的“切换脚本”管理 PID 与单实例。systemctl restart compton,并注意会话环境差异。