Simulate the keyboard and mouse movement in Linux

Source: Internet
Author: User
Tags epel repo linux mint

Simulate the keyboard and mouse movement in Linux

 
 
  1. `
    Have you ever had a dream?
  2. -- Your Computer
  3. Can I work for you automatically?
  4. Maybe not because
  5. You just saw Terminator.
  6. However
  7. Automation of scripts and tasks
  8. Is the dream pursued by every senior user
  9. If today
  10. There are many solutions
  11. This goal can be met
  12. So
  13. Sometimes
  14. It's hard to extract from the numerous
  15. Simple, intelligent, and efficient
  16. Me
  17. Cannot pretend
  18. I discovered it myself.
  19. At the same time
  20. But he prefers
  21. Clean software-xdotool
  22. The method is so intuitive
  23. As it is exposed as an X11 automation tool
  24. Conversion ideas
  25. Xdotool can read text files
  26. Simulate the key-hitting melody
  27. And manwu with the mouse
    `

Allow Xdotool to settle in Linux

For Ubuntu, Debian, or Linux Mint, you can only:

 
 
  1. $ sudo apt-get install xdotool

For Fedora, use the yum command:

 
 
  1. $ 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:

 
 
  1. $ 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:

 
 
  1. $ xdotool key [name of the key]

If you want to connect two keys, you can use the "+" Operator between them. It looks like this:

 
 
  1. $ xdotool key alt+Tab

You can switch between these two keys.

To allow xdotool to help you input the data, run the following command:

 
 
  1. $ 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:

 
 
  1. $ 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:

 
 
  1. $ 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:

 
 
  1. $ 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:

 
 
  1. #!/bin/bash
  2. xdotool [command 1]
  3. xdotool [command 2]
  4. etc

Or you can use:

 
 
  1. $ 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.

 
 
  1. #!/bin/bash
  2. for i in {1..30}
  3. do
  4. WID=`xdotool search --title "Mozilla Firefox" | head -1`
  5. xdotool windowfocus $WID
  6. xdotool key ctrl+l
  7. xdotool key Tab
  8. SENTENCE="$(fortune | cut -d' ' -f1-3 | head -1)"
  9. xdotool type $SENTENCE
  10. xdotool key "Return"
  11. sleep 4
  12. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.