How to send keystrokes (F5) from terminal to a GUI program?
GUI programs don't read from their standard input, they get their input from the X server. There are tools to inject a keystroke to a window. xdotool is fairly common and convenient.
You'll need to find the window ID that you want to send the keystroke to. You can do that with xdotool. xdotool search --class Chrome
returns the list of window IDs of all the Chrome windows. If this returns more than one, you need to pick the one you want. You can use xdotool search --name
to match on the title instead of the class. You can also parse the output of wmctrl and extract the desired window ID.
Once you've found the right window ID, you can call xdotool
to inject a keystroke. Unfortunately, many applications reject synthetic events, i.e. keystrokes and mouse events sent by another application. This is the case with current versions of Chrome. It's possible to inject a keystroke from another application by a different mechanism, but that requires the window to be focused. You can do all of that with xdotool, but it'll cause the focus to quickly flicker to the Chrome window and back. The following snippet sends F5 to the first Chrome window (in a somewhat arbitrary order).
xdotool search --class Chrome windowactivate --sync %1 key F5 windowactivate $(xdotool getactivewindow)
Or with older versions of xdotool:
xdotool windowactivate $(xdotool search --class Chrome) &&
xdotool key F5 &&
xdotool windowactivate $(xdotool getactivewindow)
Remember that this sends F5 to that window and it's up to the program to decide what to do with it. In Chrome, this reloads the current tab.
The solution suggested above used xdotool like this
xdotool key --windowid <window> <keystroke>
which didn't work for me. After some experimenting, I arrived at
xdotool windowactivate --sync <window> key <keystroke>
Once, that seemed to be working, I defined some scripts and updated my .lircrc file as show here:
http://pcfellow.com/ClementineRemote.html
I had a similar use case on a Raspberry Pi running Raspbian Wheezy. I needed to rotate tabs on Chrome using kiosk mode (sadly, installing an extension to do this on old Chrome is no longer reasonable). The accepted answer's example has some typos or simply doesn't work with my software versions, here is what works for me:
xdotool key --window "$(xdotool search --class Chromium | head -1)" Ctrl+Tab