0

I'm trying to make an autoclicker (for a farm) in a video-game (Minecraft if you want to know) that works even if the window is in the back. I disabled the game auto-pause upon losing window focus successfuly and then tried to use

xdotool click --repeat 100000000000 --delay 1000 1 --window  $(xdotool search --name --onlyvisible Minecraft)

or simply

xdotool click --repeat 100000000000 --delay 1000 1 --window  WINDOW_ID

where WINDOW_ID is the ID of the game window that I manually fetched. But both commands completely ignore the --window option and act as

xdotool click --repeat 100000000000 --delay 1000 1

i.e. it clicks where the mouse is currently pointing.

Am I doing something wrong or is there some technical limitation that I'm not aware of?

AdminBee
  • 22,803
  • Are you sure the problem comes from xdotool ? I mean : did you try the same thing with softwares other than Minecraft ? – ewen-goisot Mar 18 '21 at 07:00

1 Answers1

1

Does the problem really comes from xdotool ?

There is nothing wrong in the command you typed :
The option --window exists for xdotool click.
Maybe the problem partially comes from Minecraft. Did you try with other softwares ?

Here is what I did. It worked for me :

  • Open 2 terminals. Let's call them terminal_1 and terminal_2.
    (I did it with St and Guake, but you can try with other terminal emulators).

  • On terminal_1, run xev | grep button.
    It will open a blank window and check if events are recognized on it.

  • On terminal_2, run sleep 5 && xdotool getwindowfocus,
    and quickly (in less than 5 seconds) click on the window created by xev.
    You will get a number, let's call it WINDOW_ID.

  • Focus the terminal_2 again and make sure the mouse cursor is outside the xev window.
    After that, run the command xdotool click --window WINDOW_ID 1.

  • See the output on terminal_1. You are supposed to get something like :

      state 0x0, button 1, same_screen YES
      state 0x100, button 1, same_screen YES
    

And it means the xev window recieved the click event sent with xdotool even if it wasn't focused.

I also noticed that if I send click from St (or probably any terminal) to unfocused Guake, with mouse cursor out of Guake window, nothings visible happens except Guake is now focused.
But it doesn't work on the other direction (from Guake to St, Guake stay focused).

What you still can do

For example, keyboard events don't really care about the mouse position, but some mouse events, including clicks, does : you are supposed to click "somewhere".

On Xdotool manual, for xdotool click --window, they say
Uses the current mouse position when generating the event.,
so, maybe, if your mouse cursor is outside the window (let's say it's "Minecraft") you want to click in, the Minecraft window will ignore the event.

The simplest solution I see is:

  • store current mouse position :
    prev_pos=$(xdotool getmouselocation | awk -F "[: ]" '{print $2 " " $4}'
  • go on the window where you want to click (for example, go to the top left corner) :
    xdotool mousemove $(xdotool getwindowgeometry $(xdotool search --name --onlyvisible Minecraft | tail -1) | awk -F "[, ]" 'NR==2{print $4 " " $5}'))
  • click (will work because the mouse is on the right place) :
    xdotool click 1
  • go back to previous mouse position :
    xdotool mousemove $(echo $prev_pos)

By the way, if you want to repeat click many times, you should either :

  • stay in the "Minecraft" window.
  • switch window every second, then you need to use a for loop instead of the
    --repeat 100000000 --delay 1000 option.