9

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.

AdminBee
  • 22,803
Tim
  • 101,790
  • I noticed that none of the answers address actually sending mouse events. i don't even see an option to specify a window id for mousemove_relative, and mousemove will take a window id but it seems to be only for moving the mouse to coordinates relative to that window. did you ever find a way to send mouse movement events to a window not in front? – Michael Jul 26 '22 at 16:36

4 Answers4

9

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.

piggy
  • 3
  • 2
kirill-a
  • 2,923
  • Thanks. How about other browsers, such as google chrome? – Tim May 05 '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 05 '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. – Gilles 'SO- stop being evil' May 05 '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
4

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
hackerb9
  • 1,569
2

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

activewindow=$(xdotool getactivewindow)
xdotool windowactivate --sync $(xdotool search --name "Firefox") key space
xdotool windowactivate "$activewindow"
AdminBee
  • 22,803
bob dylan
  • 1,862
  • 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

    – Paul Hedderly Sep 08 '20 at 10:18
  • will this interfere with, e.g. trying to type or interact with a different window than the events are being sent to? – Michael Jul 26 '22 at 16:34
1

This worked for me with a slight change. I could not get it to work using the inline execution.

    activewindow=$(xdotool getactivewindow)
    wantedwindow=$(xdotool search --name "Firefox")
    xdotool windowactivate $wantedwindow
    (xdotool commands here, starting with xdotool e.g. xdotool mousemove 0 0)
    xdotool windowactivate $activewindow