Create loop with xdotool [HELP]

RMCA

New Member
Credits
67
Hello Linux community.

I recently changed my operating system and am using a Linux distribution and I am extremely satisfied, my computer is very efficient and working like I never imagined, very good indeed.

I'm trying to automate a certain function but I'm having difficulties, I would like to know if someone here in the community could help me.

It is as follows:

- using xdotool or another method;
- repeatedly send the "F12" key to a specific window in a random interval between 2s and 5s.
- If the "Scroll Lock" key is disabled, the pause function is enabled, if the "F12" key's spam function is enabled.

It's possible?
 


RMCA

New Member
Credits
67
After some reading and effort, I managed to come up with the following script;

#!/bin/bash
while true;
do sleep 3.00;
xdotool key --window 0x2800001 F12;
done

It's working in parts.
It sends the "F12" key but only when the window is active, I would like to send the "F12" key even if the window is minimized, I read something about EVENT NOTES, and it seems to me that the program I'm using doesn't support , is there a way I can enable this?

Another feature that I couldn't add was the "Scroll Lock" key, when it is disabled the loop pauses too (sending the F12 key)
 

RMCA

New Member
Credits
67
Update:

I managed to make the key repeat work, in an interval between 2s and 5s using the script:

#!/bin/bash
while true;
do sleep $(shuf -i2-5 -n1);
xdotool key --window $(xdotool search --class 'programfile') F12;
done

However, key repeat only works if the window is focused, I think the program does not accept the "EVENTS NOTES".

Does anyone have any idea how to make the program accept the "EVENTS NOTES" or simply accept the key push, even if it is not focused?
 

f33dm3bits

Gold Member
Gold Supporter
Credits
25,840
You could install wmctrl.
Code:
sudo apt install wmctrl
Then list your windows like this.
Code:
wmctrl -l
From my understanding you are trying to send the F12 key to a random window whether focused or unfocused, you could use the previous command in your scripte like this.
Bash:
#!/bin/bash

while true
do
     sleep $(shuf -i2-5 -n1)
        for window in $(wmctrl -l | cut -d" " -f1)
             do
                   xdotool key --window $window F12;
             done
done
So with wmctrl -l you list all the windows(focused or unfocused) and then grab the names of all the windows which are in the first column. Then you loop over those and use xdotool to send F12 to each of those windows, I'm still not quite sure what you are doing but I hope this helps ;)

Edit: I just reread your original post and you don't want to send F12 to all the windows but only a specific one but in a random interval. How are you determining which window you will be wanting to send the F12 key to?
 
Last edited:

RMCA

New Member
Credits
67
You could install wmctrl.
Code:
sudo apt install wmctrl
Then list your windows like this.
Code:
wmctrl -l
From my understanding you are trying to send the F12 key to a random window whether focused or unfocused, you could use the previous command in your scripte like this.
Bash:
#!/bin/bash

while true
do
     sleep $(shuf -i2-5 -n1)
        for window in $(wmctrl -l | cut -d" " -f1)
             do
                   xdotool key --window $window F12;
             done
done
So with wmctrl -l you list all the windows(focused or unfocused) and then grab the names of all the windows which are in the first column. Then you loop over those and use xdotool to send F12 to each of those windows, I'm still not quite sure what you are doing but I hope this helps ;)

Edit: I just reread your original post and you don't want to send F12 to all the windows but only a specific one but in a random interval. How are you determining which window you will be wanting to send the F12 key to?
Thank you for your help.
What I'm doing is exactly that, sending the "F12" key to the window named class "programfile".
With the script below the function is working;

#!/bin/bash
while true;
do sleep $(shuf -i2-5 -n1);
xdotool key --window $(xdotool search --class 'programfile') F12;
done


Using the command line (xdotool search --class 'programfile') and I can identify the window that "F12" should be sent.

However, I am having 2 problems;

1- The "F12" key is sent to the "programfile" window only if it is focused, if the window is in the background the "F12" key is not sent, and I would like it to be.

2- I would like the "F12" key to be sent only if no other keyboard key or mouse click is being performed, by me, for example:

I'm typing any text, the moment I press any keyboard button the "F12" key is not sent, unless no keyboard key is being executed.
 

f33dm3bits

Gold Member
Gold Supporter
Credits
25,840
You can get the window class with wmctrl as well, man wmctrl
Code:
-x     Include WM_CLASS in the window list or interpret <WIN> as the WM_CLASS name.
The third field would be the window class and the fourth field would be the window name.
Code:
0x01000003 -1 xfce4-panel.Xfce4-panel  debian xfce4-panel
0x01400028 -1 xfdesktop.Xfdesktop   debian Desktop
0x02a00003  0 xfce4-terminal.Xfce4-terminal  debian Terminal - [email protected]: ~
0x00e00007  0 Thunar.Thunar         debian tux
0x02a00da6  0 xfce4-terminal.Xfce4-terminal  debian Terminal - [email protected]: ~
0x00e02d30  0 Thunar.Thunar         debian tux
So then we could take your already existing script and adjust it a bit.
Bash:
#!/bin/bash

while true
do
    sleep $(shuf -i2-5 -n1)
for window in $(wmctrl -l -x | grep -i programfile | cut -d" " -f1)
     do
         xdotool key --sync --window $window F12
    done
done
I'm not sure though if xdotool can use the window name which you get from wmctrl. You could also try activating the windows that match, so then it would like this.
Bash:
#!/bin/bash

while true
do
    sleep $(shuf -i2-5 -n1)
for window in $(xdotool search --sync --class programfile windowactivate)
    do
        xdotool key --sync --window $window F12
    done
done
And if that doesn't work you can try using windowfocus.
Code:
xdotool search --class programfile windowfocus
 
Last edited:

RMCA

New Member
Credits
67
You can get the window class with wmctrl as well, man wmctrl
Code:
-x     Include WM_CLASS in the window list or interpret <WIN> as the WM_CLASS name.
The third field would be the window class and the fourth field would be the window name.
Code:
0x01000003 -1 xfce4-panel.Xfce4-panel  debian xfce4-panel
0x01400028 -1 xfdesktop.Xfdesktop   debian Desktop
0x02a00003  0 xfce4-terminal.Xfce4-terminal  debian Terminal - [email protected]: ~
0x00e00007  0 Thunar.Thunar         debian tux
0x02a00da6  0 xfce4-terminal.Xfce4-terminal  debian Terminal - [email protected]: ~
0x00e02d30  0 Thunar.Thunar         debian tux
So then we could take your already existing script and adjust it a bit.
Bash:
#!/bin/bash

while true
do
    sleep $(shuf -i2-5 -n1)
for window in $(wmctrl -l -x | grep -i programfile | cut -d" " -f1)
     do
         xdotool key --sync --window $window F12
    done
done
I'm not sure though if xdotool can use the window name which you get from wmctrl. You could also try activating the windows that match, so then it would like this.
Bash:
#!/bin/bash

while true
do
    sleep $(shuf -i2-5 -n1)
for window in $(xdotool search --sync --class programfile windowactivate)
    do
        xdotool key --sync --window $window F12
    done
done
And if that doesn't work you can try using windowfocus.
Code:
xdotool search --class programfile windowfocus
Thanks again for the help...
We're making progress from completing my script, little by little making adjustments and it's almost perfect, in the role I want.

xdotool (according to its manual) is able to send keystrokes (even with minimized window), the command line;

xdotool key --window $(xdotool search --class 'programfile.exe') F12

It's working, as long as the "ProgramFile" window is visible, if I minimize it and open another window from another program, the "F12" key is no longer sent.

Sending the key is working, but when it is minimized it doesn't work anymore, that's the problem I'm trying to get around.

And another feature that I'm not sure how to do is the following;

- The "F12" key should only be sent if the user is not pressing any other keyboard key or mouse click.
 

f33dm3bits

Gold Member
Gold Supporter
Credits
25,840
It's working, as long as the "ProgramFile" window is visible, if I minimize it and open another window from another program, the "F12" key is no longer sent.

- The "F12" key should only be sent if the user is not pressing any other keyboard key or mouse click.
I already told you what you could do for that and gave an example in my last reply. You can either try using windowactivate or windowfocus.
Code:
xdotool search --sync --class programfile windowactivate)
xdotool search --sync --class programfile windowfocus)
man xdotool
windowactivate [options] [window]
Activate the window. This command is different from windowfocus: if the window is on another desktop, we will switch to that desktop. It also uses a different method for bringing the window up. I recommend trying this command before using windowfocus, as it will work on more window managers.
Bash:
#!/bin/bash

while true
do
    sleep $(shuf -i2-5 -n1)
for window in $(xdotool search --sync --class programfile windowactivate)
    do
        xdotool key --sync --window $window F12
    done
done
You can use xinput to test input devices.

I still don't understand why you are trying to get this done, what do you want to achieve by this or what problem are you trying to solve by doing this?
 
Last edited:

RMCA

New Member
Credits
67
Apologies for the delay...
What I'm trying to do is get the "F12" key sent repeatedly to a game, even if it's minimized.
Also, I want the "F12" key to be sent only if no other keyboard keys are currently being pressed by me.

In short:
- I have a game, where the "F12" key must be pressed every 5 seconds
- I want the "F12" key to be automatically sent to the game, even if I'm watching Netflix for example.
- If I press any key on the keyboard, at the same time the "F12" is being sent, it waits and only after I stop using the keyboard does it send the "F12".

It's possible?
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online


Top