How to use the command xdotool (with examples)
- Linux
- December 25, 2023
The xdotool command is a command-line automation tool for X11, which allows users to simulate keyboard input, mouse activity, and manipulate windows in the X Window System. It can be useful for automating repetitive tasks, creating custom shortcuts, or scripting user interactions.
Use case 1: Retrieve the X-Windows window ID of the running Firefox window(s)
Code:
xdotool search --onlyvisible --name firefox
Motivation: This use case allows users to retrieve the X-Windows window ID of the running Firefox window(s). This information can be used for various automation tasks, such as focusing on a specific Firefox window or manipulating its properties.
Explanation:
search
is the xdotool command to search for windows based on certain criteria.--onlyvisible
specifies that only visible windows should be considered in the search.--name
is used to match the window name with the provided string “firefox”.
Example output: If there are two Firefox windows currently running, the command output might be:
12345
67890
Use case 2: Click the right mouse button
Code:
xdotool click 3
Motivation: This use case allows users to simulate a right-click of the mouse. It can be used to interact with context menus or perform operations that require a right-click.
Explanation:
click
is the xdotool command to simulate mouse clicks.3
specifies that the right mouse button should be clicked.
Example output: N/A
Use case 3: Get the ID of the currently active window
Code:
xdotool getactivewindow
Motivation: This use case allows users to retrieve the X-Windows window ID of the currently active window. This information can be useful for various automation tasks, such as focusing on a specific window or manipulating its properties.
Explanation: N/A
Example output: If the currently active window has an ID of 54321, the command output would be:
54321
Use case 4: Focus on the window with ID of 12345
Code:
xdotool windowfocus --sync 12345
Motivation: This use case allows users to focus on a specific window by providing its X-Windows window ID. Focusing on a window means bringing it to the front and receiving input events.
Explanation:
windowfocus
is the xdotool command to make a window the currently focused window.--sync
ensures that the command waits for the window focus change to complete before returning.12345
represents the X-Windows window ID of the window to focus on.
Example output: N/A
Use case 5: Type a message, with a 500ms delay for each letter
Code:
xdotool type --delay 500 "Hello world"
Motivation: This use case allows users to simulate keyboard typing with a specified delay between each letter. It can be used for automating text input or simulating human-like typing behavior.
Explanation:
type
is the xdotool command to simulate keyboard input.--delay 500
specifies a delay of 500 milliseconds between each character."Hello world"
is the message that will be typed.
Example output: N/A
Use case 6: Press the enter key
Code:
xdotool key KP_Enter
Motivation: This use case allows users to simulate pressing the enter key on the keyboard. It can be used to submit forms, confirm actions, or navigate through command-line interfaces.
Explanation:
key
is the xdotool command to simulate keystrokes.KP_Enter
represents the enter key on the keypad.
Example output: N/A
Conclusion:
The xdotool command provides powerful automation capabilities for X11, allowing users to simulate keyboard input, mouse activity, and manipulate windows. By understanding and utilizing its various use cases, users can automate repetitive tasks, create custom shortcuts, or script user interactions efficiently.