Advertisement
fant0men

xdotool notes (simulate key presses)

Aug 28th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. while true; do sleep 2 && xdotool key --window 0x3a00001 Page_Down && echo "scrolling..."; done
  2.  
  3. xwininfo = skaffa window ID
  4.  
  5. ***
  6.  
  7. 1:34:00
  8.  
  9. for n in {0..660}; do
  10. sleep 0.5;
  11. win='0x3800007';
  12. xdotool windowfocus ${win};
  13. xdotool key ctrl+t;
  14. sleep 0.5;
  15. xdotool key Return;
  16. echo $n;
  17. done
  18.  
  19. ***
  20.  
  21. http://xmodulo.com/simulate-key-press-mouse-movement-linux.html
  22.  
  23. *
  24.  
  25. How to simulate key press and mouse movement in Linux
  26. Last updated on July 10, 2014 Authored by Adrien Brochard 7 Comments
  27.  
  28. Have you ever dreamed of your computer doing stuff automatically for you? Probably not if you just watched Terminator. But except for that, scripting and task automation are every power user's dreams. If a lot of solutions exist today to fit such goal, it is sometimes hard to pick the simple, smart, and efficient one out of the lot. I cannot pretend to have found it myself, but in the mean time, my preference goes to neat software called xdotool. Its approach is intuitive as it stands as an X11 automation tool. In other words, xdotool can simulate key presses and even mouse events from reading a text file.
  29. Installation of Xdotool on Linux
  30.  
  31. For Ubuntu, Debian or Linux Mint, you can just do:
  32. $ sudo apt-get install xdotool
  33.  
  34. For Fedora, use yum command:
  35. $ sudo yum install xdotool
  36.  
  37. For CentOS user, the package is available in EPEL repo. After enabling EPEL repo, simply use yum command as above.
  38.  
  39. For Arch user, the package is available in the Community repo:
  40. $ sudo pacman -S xdotool
  41.  
  42. If you cannot find xdotool for your distribution, you can always download it from the official website.
  43. Basic Usage of Xdotool
  44.  
  45. As intuitive as it is, xdotool remains a scripting application. Hence you have to know the syntax in order to use it properly. Rest assured though, the syntax is very simple and quick to pick up, relative to the extent of the program's features.
  46.  
  47. First, it is very easy to simulate key press. From the terminal, you can type the command:
  48. $ xdotool key [name of the key]
  49.  
  50. If you want to chain two keys, use the "+" operator between them. So:
  51. $ xdotool key alt+Tab
  52.  
  53. will switch window for you.
  54.  
  55. To have xdotool type for you, use the type command:
  56. $ xdotool type ''
  57.  
  58. That's already enough for basic key pressing. But one of the many strengths of xdotool is its ability to put the focus on a particular window. It can fetch the right window, and then type in it, preventing all your recorded keystrokes to just vaporize in thin air. For this, the simplest command is:
  59. $ xdotool search --name [name of the window] key [keys to press]
  60.  
  61. This will search through the opened window for one with the name matching the search, give it the focus, and then simulate the key pressing.
  62.  
  63. A bit more advanced, but very useful, xdotool can simulate mouse movement and click. With:
  64. $ xdotool mousemove x y
  65.  
  66. you can place the cursor at coordinates (x,y) of your screen (in pixels). You can also combine it with the "click" argument:
  67. $ xdotool mousemove x y click 1
  68.  
  69. This will move the mouse to (x,y), and click with the left button. The "1" represents the left button of the mouse, "2" would be the scroll wheel, "3" the right button, etc.
  70.  
  71. Finally, once you have your commands in mind, you might want to actually dump it in a file to edit and play. For that, there is more than one syntax. You can write is a bash script:
  72. 1
  73. 2
  74. 3
  75. 4
  76. 5
  77.  
  78. #!/bin/bash
  79.  
  80. xdotool [command 1]
  81. xdotool [command 2]
  82. etc
  83.  
  84. Or you can use:
  85. $ xdotool [filename]
  86.  
  87. where you write your commands in a separate file and plug its name as the argument.
  88. Bonus
  89.  
  90. As a bonus to this post, here is a concrete example of xdotool in action. You may or may not have heard of Bing, the Microsoft's search engine. In the latter case, you have then never heard of Bing Rewards: a program that allows you to trade Bing points for Amazon's and other gift cards. To earn those points, you can do up to 30 searches a day on Bing, each search giving you 0.5 point. In other words, you have to make Bing your default search engine, and use it every day.
  91.  
  92. Or, you can use this xdotool script, which will automatically give focus to Firefox (replace it with your favorite navigator), and perform a search using the fortune command to generate some random words. In about 30 seconds, all your daily searches will be done.
  93. 1
  94. 2
  95. 3
  96. 4
  97. 5
  98. 6
  99. 7
  100. 8
  101. 9
  102. 10
  103. 11
  104. 12
  105. 13
  106.  
  107. #!/bin/bash
  108.  
  109. for i in {1..30}
  110. do
  111. WID=`xdotool search --title "Mozilla Firefox" | head -1`
  112. xdotool windowfocus $WID
  113. xdotool key ctrl+l
  114. xdotool key Tab
  115. SENTENCE="$(fortune | cut -d' ' -f1-3 | head -1)"
  116. xdotool type $SENTENCE
  117. xdotool key "Return"
  118. sleep 4
  119. done
  120.  
  121. To conclude, I really like xdotool even if its full capabilities extend way beyond the scope of this post. It is a really approachable way to scripting and task automation. The downside is that it probably is not the most efficient one. But again, it does the job, and isn't too much of a bother to learn.
  122.  
  123. What are your thoughts on xdotool? Do you prefer another automation tool to it? And why? Let us know in the comments.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement