Skip to content

Instantly share code, notes, and snippets.

@monstermunchkin
Created April 27, 2012 09:03
Show Gist options
  • Save monstermunchkin/2507621 to your computer and use it in GitHub Desktop.
Save monstermunchkin/2507621 to your computer and use it in GitHub Desktop.
Sending keycodes to unfocused window (e.g. Spotify)
#!/bin/bash
KEY_NEXT=XF86AudioNext # keycode: 171
KEY_PLAY=XF86AudioPlay # keycode: 172
KEY_PREV=XF86AudioPrev # keycode: 173
# the window id of Spotify
SPOTIFY_WID=$(xdotool search --name "Spotify - Linux Preview")
if [[ -n ${SPOTIFY_WID} ]]; then
# if Spotify instance is running send keycode to Spotify ...
ACTIVE_WID=${SPOTIFY_WID}
else
# ... otherwise send to active (focused) window
ACTIVE_WID=$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/ {print $NF}')
fi
case "${1}" in
prev)
xdotool key --window ${ACTIVE_WID} ${KEY_PREV};;
next)
xdotool key --window ${ACTIVE_WID} ${KEY_NEXT};;
play)
xdotool key --window ${ACTIVE_WID} ${KEY_PLAY};;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment