6

On Ubuntu desktop, can I use xdotool to mouse click (e.g. click a link) and press some keys (e.g. ctrl+s) on a window (e.g. Firefox's window) that is not in the front, so that I can work on another different window which may need to lie on top of the other windows, while at the same time, let xdotool to work on a hidden window? Thanks.

7

To get a window id run:

sleep 5; xdotool getactivewindow

This will wait 5 seconds and then get the active window ID. Run this command, click on Firefox, and wait for it to finish. Use that ID in the next step.

You can send keystrokes directly to specific window, by executing:

xdotool type --window [window ID] Hello World

Use the command man xdotool to get a full list of commands and functions!

Xdotool will generate key events and send them directly to window Firefox. But sending keystrokes to a specific window uses a different API than simply typing to the active window, and many applications just ignore them.

5
  • Thanks. How about other browsers, such as google chrome?
    – Tim
    May 5 '15 at 14:44
  • I have tried on Chromium, doesn't work. But works in Xterm, for example (if SendEvents is enabled in ctrl+click menu).
    – kirill-a
    May 5 '15 at 19:33
  • 1
    It's not that Firefox ignores events when it doesn't have focus. Xdotool has two methods to generate fake events. One is synthesizing events, which can send an event to any window, but the receiver is informed that the event is synthetic and many applications drop synthetic events (out of a misguided security concern — if you let other users access your X session, that's not the only thing you need to worry about). Another method is the XTEST extension, which generates “genuine-looking” events, but that method only works to the focused window. May 5 '15 at 23:22
  • @michael Did you try focussing the window first? For example, xdotool search --class chrom windowfocus type --window %@ hello. While you don't need to focus first for Firefox, Chromium seems to require it.
    – hackerb9
    Nov 11 '19 at 11:12
  • A-ha! I see what happened. Xdotool's usage changed and this answer is no longer correct. I've posted an answer that has the current syntax.
    – hackerb9
    Nov 11 '19 at 11:22
2

Yes

Xdotool can be used to send input to a window that's not in the front. However, not all windows will accept such input. For example, xterm will not unless you check the "Allow Send Events" options. Chromium doesn't either. However, Firefox at least as of 2019, does work.

Here is an example which reloads the current tab in all Firefox windows:

xdotool search --class firefox key --window %@ Ctrl+F5
1

You can automatically activate this window and go back to the previously activated window with : (Exemple with sending Space to Firefox)

xdotool getactivewindow > /tmp/activated_window
xdotool windowactivate --sync $(xdotool search --name "Firefox") key space
xdotool windowactivate `cat /tmp/activated_window`
1
  • 2
    easier to use a var than a tmp file tmp=$(xdotool getactivewindow); xdotool windowactivate --sync $(xdotool search --name "Firefox") key space; xdotool windowactivate $tmp Sep 8 '20 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.