Switch audio output by keyboard in KDE Plasma
I would like to have a way of switching the audio output device (internal speakers/headphones to bluetooth speakers etc) by keyboard actions (which I usually can do without searching, finding and putting my glasses on my nose).
Plasma tray elements can be activated with shortcuts, and thus I can connect, disconnect, switch between bluetooth devices by keyboard, because when the drop-down menu is visible, selection with arrow-keys and Enter to activate does the trick.
But that doesn’t work when trying to switch between audio devices themselves. I can bring forth the audio volume tray element with a shortcut:
but further interaction by keyboard is limited to volume level.
There is a widget called audio device switcher which brings the options more to the front, but its shortcut does nothing. Overall, it lacks keyboard interaction.
The closest thing to what I want is to simply disable bluetooth by the first method IF that device is the one that is currently playing…
As a variant – you can go by way of scripts/launchers and set the hotkeys to run this scripts.
First you need to get a list of "sinks", which is how system knows audo devices:
pactl list sinks
One would have state "running" other in "suspended". Take a note of Name fields in this printout.
The script you will start from hotkey will execute a switch:
pactl set-default-sink SINK-NAME
Using the "Name" field from the list of sinks. This will switch all audio output to specified device.
Assuming you have just two sinks, you can do a script like this
#!/bin/bash
sink1=alsa_output.pci-0000_00_01.1.hdmi-stereo-extra1
sink2=alsa_output.usb-GeneralPlus_USB_Audio_Device-00.iec958-stereo
sink_current=`pactl get-default-sink`
case $sink_current in
$sink1) pactl set-default-sink $sink2 ;;
$sink2) pactl set-default-sink $sink1 ;;
*) pactl set-default-sink $sink1 ;;
esac
Put it into you personal bin
, and add a hotkey launcher.