Automation: Simulate the supports of keys on the keyboard

this article is intended to describe how to automate support of keys in an application.

indeed, I am often asked to do repetitive actions on sites (every hour).

as it happens I have a RaspberryPI that runs all the time (like many) it could very well take up this task.


click to have the definition of the levels.

the goal of the project is to carry out the following steps every 45 minutes:

  1. throwing the focus on the Mozilla application already launched

  2. open a new browser window in a new tab (to avoid disrupting the already active windows)

  3. launch a web site in the address bar

  4. launch a second website in the same address bar

  5. close tab

we will use as application xdotool which is quite comprehensive as it allows not only to simulate the keys but also to activate windows or simulate the mouse. Personally I avoid to use the mouse, because it is too linked to the positioning of the windows. Emphasis so the keyboard shortcuts that have much more chance of success if the window is active.

it is on the classic facility.

 # installation package sudo apt - get update and sudo apt - get upgrade sudo apt - get install xdotool configuration 

the basic commands that will be of interest to us are the following:

 # to enter a function (ctrl + t) xdotool key key ctrl + t # to enter text xdotool type 'My text to enter' 

here is the batch file that will simulate the input keyboard corresponding to the different stages of the project [itwaswrittentoafilewithnano/home/pi/SimuleClaviersh:

 nano home/pi/

enter the following lines:

 # = # = to cron / * 45 (every 45 minutes) == # = #! / bin/bash sleep 5s # Activation of Mozilla # the browser must already be run WID ='xdotool search "Mozilla Firefox" | head - 1' xdotool windowactivate - sync sleep 2s $WID # new tab in mozilla xdotool key ctrl + t sleep 2s # address site to launch key alt + d xdotool xdotool type '' xdotool key Return sleep 10s # other site to launch key alt + d xdotool xdotool type '' xdotool key Return sleep 20s # closing tab xdotool key ctrl + w 

exit nano using CTRL + X and Y


alt + d key allows to select the address bar of the browser. Indeed, after each launch site the address bar loses focus.

For sites that’s what you want, but it is sometimes interesting to see how they work. In my case, the passage of the nickname in parameter to avoid to enter in the login window.


then make your script executable by using the following statement:

 chmod 777 home/pi/

we will use cron via the following installation:

 sudo apt - get install gnome-schedule 

then create a scheduled task. You can do the GUI or command line.

command-line method

 crontab e 

choose nano. That will launch the configuration file with nano. You need to add the last line to start the script every 45 seconds

 # Output of the crontab jobs (including errors) is feeling through # email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts # at 5 a.m. every week with: # 0 5 * 1 tar - answer /var/backups/home.tgz /home/ # For more information see the manual pages of crontab (5) and cron (8) # m h dom my dow command * / 45 * /home/pi/

exit nano using CTRL + X and Y

graphical method

after installing a new icon “Scheduled tasks” is present in Start menu:

clicking on a Setup window opens

create a new task and type like /home/pi/ command then Xapplication. Then choose ‘Advanced’ mode-> * / 45 which is 45 minutes.

that’s your task is scheduled to run every 45 minutes.

it can be interesting to work in the background the launch of web sites without graphical interactions. This can use a python script which would look like this in Python version 2 to version 3 it’s different:

 #! / usr/bin/env python import urllib f = s urllib.urlopen("") = () print (s) 

No comments:

Powered by Blogger.