Simulate the keyboard and mouse movement in Linux
- `
Have you ever had a dream?
- -- Your Computer
- Can I work for you automatically?
- Maybe not because
- You just saw Terminator.
- However
- Automation of scripts and tasks
- Is the dream pursued by every senior user
- If today
- There are many solutions
- This goal can be met
- So
- Sometimes
- It's hard to extract from the numerous
- Simple, intelligent, and efficient
- Me
- Cannot pretend
- I discovered it myself.
- At the same time
- But he prefers
- Clean software-xdotool
- The method is so intuitive
- As it is exposed as an X11 automation tool
- Conversion ideas
- Xdotool can read text files
- Simulate the key-hitting melody
- And manwu with the mouse
`
Allow Xdotool to settle in Linux
For Ubuntu, Debian, or Linux Mint, you can only:
- $ sudo apt-get install xdotool
For Fedora, use the yum command:
- $ sudo yum install xdotool
For CentOS users, you can find this package in EPEL repo. After enabling the EPEL repository, you only need to use the yum command above to fulfill your wish.
For Arch users, you can find the package in the Community Repository:
- $ sudo pacman -S xdotool
If you still cannot find the corresponding xdotool of your release, you can download it from its official site.
Basic Xdotool skills
Although xdotool is so intuitive, it is still a script program. Therefore, to use it correctly, you still need to understand its syntax. However, you can rest assured that the syntax is easier to learn than the functions of the program.
First, it is easy to simulate a key. You can enter the following command from the terminal:
- $ xdotool key [name of the key]
If you want to connect two keys, you can use the "+" Operator between them. It looks like this:
- $ xdotool key alt+Tab
You can switch between these two keys.
To allow xdotool to help you input the data, run the following command:
- $ xdotool type ''
These are sufficient for basic keys. However, one of the many advantages of xdotool is that it can obtain the focus of a specific window. It can get the window on the right and input it in it. All the buttons you record will not evaporate, but will appear exactly as you wish. To obtain this function, a simple command can be used:
- $ xdotool search --name [name of the window] key [keys to press]
This command searches for the window with the corresponding name in the window that opens, focuses on the window, and then simulates the shortcut key.
It is more advanced, but it is very useful. xdotool can simulate moving and clicking the mouse. See this command:
- $ xdotool mousemove x y
You can position the cursor to the x, y) pixel of the screen coordinate ). You can also use the "click" parameter for combination:
- $ xdotool mousemove x y click 1
This will move the mouse to x, y), and then click the left mouse button. "1" indicates the left mouse button, "2" indicates the scroll wheel, and "3" indicates the right mouse button.
Finally, once these commands are rooted in your mind, you may want to actually dump them to files for editing and try to play. In view of this, there will be more than one statement. You need to write a bash script:
- #!/bin/bash
-
- xdotool [command 1]
- xdotool [command 2]
- etc
Or you can use:
- $ xdotool [filename]
Here you write the command into a separate file, and then use the file name as the parameter of the xdotool command.
Unexpected gains
As an unexpected result of this article, here is a specific example of xdotool. You may have heard of or heard of Bing-Microsoft's search engine. In the following examples, you may have never heard of Bing rewards: a program that allows you to use Bing points to redeem Amazon gift cards and other gift cards: I have never heard of it ~). To earn these points, you can search for 30 times on Bing every day, and you will get 0.5 points each time you search. In other words, you must set Bing as the default search engine and use it every day.
Alternatively, you can use the xdotool script. In this script, it will automatically focus on Firefox. you can replace it with your favorite browser ), use the fortune command to generate random words for search. In about 30 seconds, your daily search task is complete.
- #!/bin/bash
-
- for i in {1..30}
- do
- WID=`xdotool search --title "Mozilla Firefox" | head -1`
- xdotool windowfocus $WID
- xdotool key ctrl+l
- xdotool key Tab
- SENTENCE="$(fortune | cut -d' ' -f1-3 | head -1)"
- xdotool type $SENTENCE
- xdotool key "Return"
- sleep 4
- done
Here is a summary: I really like xdotool, even though its full functionality is beyond the scope covered in this article. This is indeed a simple method for script and task automation. The negative problem is that it may not be the most efficient one. But I want to say it again, it is loyal to its duty, and it is not so troublesome to learn.
What do you think about xdotool? Do you prefer another automation tool instead of it? Why? Please let us know in the comment.