Xdotool - Mouse

Discussion in 'X org / Desktop' started by Jarret W. Buse, Aug 11, 2014.

  1. Jarret W. Buse

    Jarret W. Buse Well-Known Member Staff Writer

    Messages:
    192
    Likes Received:
    276
    Trophy Points:
    63
    Xdotool - Mouse

    The xdotool is a utility used from the terminal or in a script to manually perform keyboard and mouse input (see the Xdotool – Keyboard article). The commands can also be used to make a script of many xdotool commands to create large tasks. Later articles will cover the xdotool ability to control window and desktop manipulation.

    The syntax for xdotool depends on the command being used. Let's start with sending mouse movements to a window by using the 'mousemove' command. The syntax is as follows:

    mousemove [options] x y

    There are five options available for mousemove which are:

    · --window window_id – for mouse movement. The window you specify will be the one used as a relative point for movement
    · --screen screen_number – specifies the screen number, screen 0 is your first monitor
    · --polar – uses polar coordinates by specifying 'x' between 0-360 degrees and 'y' as the distance from the center. Based off the specified window by its id
    · --clearmodifiers – clears all modifiers such as Shift, CTRL and such keyboard keys as well as a mouse button being pressed manually
    · --sync – causes the mousemove command to be sent, but the cursor does not move until the mouse is physically moved. Once moved, the cursor will go to the position specified

    NOTE: To find the window_id see the previous article: Xdotool – Keyboard.

    If I had a text editor open which had the window id of 46137499 and I wished to move the mouse cursor to the upper left corner of the window I would perform the following command:

    xdotool mousemove --window 46137499 0 0

    If I wanted to move the cursor to the top left corner of the whole screen, I would do the following:

    xdotool mousemove 0 0

    NOTE: When no window is specified, the desktop is assumed as the window.

    If I wished to use polar coordinates up is 0 or 360 degrees, right is 90, down is 180 and left is 270 degrees. The 'y' coordinate is the number of pixels from the center of the window. In the following example, the cursor will be placed 45 degrees and 300 pixels from the center of the desktop:


    xdotool mousemove –polar 45 300

    NOTE: The cursor is placed in the upper right part of the screen. Any pixel value greater than what would cause the cursor to be off the screen will only place the cursor at the edge of the screen.

    There exists another mousemove command which restores the cursor back to where it was before you performed the previous mousemove:

    mousemove [options] restore

    The options are the same as the mousemove, except –window and –polar do not work. The restore command needs to be used in the same xdotool command as follows:

    xdotool mousemove 1000 1000 mousemove restore

    NOTE: Multiple commands can be chained together in xdotool command as shown. All the commands can be mixed such as keyboard, mouse, window and desktop commands.

    The third mousemove command is:

    mousemove_relative [options] x y


    This command moves the cursor relative to its current position. The options are as follows:

    · --polar – uses polar coordinates by specifying 'x' between 0-360 degrees and 'y' as the distance from the center, based off the specified window by its id
    · --clearmodifiers – clears all modifiers such as Shift, CTRL and such keyboard keys as well as a mouse button being pressed manually
    · --sync – causes the mousemove command to be sent, but the cursor does not move until the mouse is physically moved. Once moved, the cursor will go to the position specified

    Here, we can move the cursor to the center of the screen and then use the relative command to move it down (180 degrees) and a distance of 50 pixels:

    xdotool mousemove --polar 0 0 mousemove_relative --polar 180 50

    Another mouse command is the 'click' command. Here, we can cause the cursor to perform a mouse click as if the user did physically click a button on the mouse. The syntax is:

    click [options] button

    The options are as follows:

    · --window window_id – for mouse movement, the window you specify will be the one used as a relative point for movement
    · --clearmodifiers – clears all modifiers such as Shift, CTRL and such keyboard keys as well as a mouse button being pressed manually
    · --repeat – specifies the number of times to click. The default is one which is a single click, specify 2 for a double click
    · --delay milliseconds – the number of milliseconds to delay between clicks. The option is only used when the repeat option is used with a value greater than 1

    The buttons are numeric from left to right on the mouse or the opposite way if it is as left-handed mouse. For a right-handed mouse, button 1 is the far leftmost, the center button is button 2 if there is one, and the right button would either be 2 or 3 depending if a middle button existed.

    NOTE: With some systems, button 4 can be for the middle button up and 5 for the middle button down.

    For example, let us use a text editor with a window id of 46137499. We will move the cursor to the top left corner of the window and left click to select it (focus). The cursor is then moved down 10 pixels and right 100 pixels relative to the current position. At this point we will cause the wheel to scroll down 10 times:

    xdotool mousemove --window 46137499 0 0 click 1 mousemove_relative 10 110 click --repeat 10 5

    The following two commands are similar and work together. The commands are:

    mousedown [options] button
    mouseup [options] button


    Here it is possible to perform drag-and-drop procedures on systems which support it. Place the cursor in a position. Use the mousedown command with the proper button, move the mouse again and use the mouseup command to drop what has been dragged.

    For example, let us click on the text editor window and select it with the left mouse button. The mousedown command is used to be able to drag it. The mouse is moved to the desktop coordinates of 10 10. Now the mouseup command is given to drop the window.

    xdotool mousemove --window 46137499 0 0 click 1; xdotool mousedown 1; xdotool mousemove 10 10; xdotool mouseup 1

    NOTE: In this case, chaining the commands does not work and each command must be seprarate. Chained commands must be experimented with before using in a script.

    The next command is the 'getmouselocation' which is shown in Figure 1. Here you can see the Terminal executing the 'getmouselocation' command while the mouse is on the text editor window. The output for get mouse location is: x:1170 y:19 screen:0 window:46137499.

    NOTE: This is another way to get the window id.

    xDoTool - Mouse Figure 1.jpg
    FIGURE 1

    The getmouselocation has no input, but can accept the --shell option. The --shell option places the four location items in a column rather than a row. The items can be used in a shell script to provide information for variables to use in the scripts.

    The next command is the 'behave_screen_edge'. The syntax is as follows:

    behave_screen_edge [options] WHERE COMMAND

    This command allows you to specify an edge of a screen which will cause an action to be performed when the mouse hits the specified edge. The command seems to work best when the current window is maximized or touches the edge of the screen that is the “hot spot”.

    The options are as follows:

    · delay milliseconds – specifies the number of milliseconds which the cursor must be at the WHERE to trigger event
    · quiesce milliseconds – delay before next trigger can occur (default 0)

    The WHERE options are as follows:

    · left
    · top-left
    · top
    · top-right
    · right
    · bottom-left
    · bottom
    · bottom-right

    A simple command is to execute the gedit text editor command when the cursor touches the left side of the screen:

    xdotool behave_screen_edge --quiesce 750 left exec gedit

    Try a few of these and my next article will cover the Window commands.

    Attached Files:

    • slide.jpg
      slide.jpg
      File size:
      115.8 KB
      Views:
      228,941
    Rishabh Verma and DevynCJohnson like this.
  2. bashcommando

    bashcommando Active Member

    Messages:
    217
    Likes Received:
    43
    Trophy Points:
    28
    I can make my own RDP using this
  3. Jarret W. Buse

    Jarret W. Buse Well-Known Member Staff Writer

    Messages:
    192
    Likes Received:
    276
    Trophy Points:
    63
    Wait for my future articles on xdotool (keyboard, window and desktop). I may do 2 others, one to make things simpler to use (window stack) and another to show examples to put it all together. Believe me, it gets a lot more interesting.

Share This Page