Xdotool - Keyboard

J

Jarret W. Buse

Guest
Xdotool - Keyboard

The xdotool is a utility used from the terminal or in a script to manually perform keyboard input. The commands can also be used to make a script of many xdotool commands to perform large tasks. Later articles will cover the xdotool ability to perform mouse input as well as window and desktop manipulation.

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

key [options] [keys]

There are three options available for the key command:

  1. --window window_id – specified keys for the keystrokes are sent to the window_id application.
  2. --clearmodifiers – all modifiers are cleared, such as CAPS LOCK, NUM LOCK, shift held down, a mouse button held down, etc.
  3. --delay milliseconds – sets the delay between each keystroke being sent, the default is 12 ms.

The [keys] specify which keys are being sent to the specified window. The keys are based on the X Keysym strings and are:

  • space
  • R
  • exclam
  • S
  • quotedbl
  • T
  • numbersign
  • U
  • dollar
  • V
  • percent
  • W
  • ampersand
  • X
  • quoteright
  • Y
  • parenleft
  • Z
  • parenright
  • bracketleft
  • asterisk
  • backslash
  • plus
  • bracketright
  • comma
  • asciicircum
  • minus
  • underscore
  • period
  • quoteleft
  • slash
  • a
  • 0
  • b
  • 1
  • c
  • 2
  • d
  • 3
  • e
  • 4
  • f
  • 5
  • g
  • 6
  • h
  • 7
  • i
  • 8
  • j
  • 9
  • k
  • colon
  • l
  • semicolon
  • m
  • less
  • n
  • equal
  • o
  • greater
  • p
  • question
  • q
  • at
  • r
  • A
  • s
  • B
  • t
  • C
  • u
  • D
  • v
  • E
  • w
  • F
  • x
  • G
  • y
  • H
  • z
  • I
  • braceleft
  • J
  • bar
  • K
  • braceright
  • L
  • asciitilde
  • M
  • ctrl (Control key)
  • N
  • alt (Alternate key)
  • O
  • BackSpace
  • P
  • F1-F12 (Function 1 key to Function 12 just use the number of the specific Function key)
  • Q
  • Return (Return or Enter key)
Each of these characters can be used separately and some together. A space is placed between individual keystrokes and simultaneous keystrokes are connected with a plus (+) sign. For example, to perform a single keystroke of a space and then an 'x' would be: 'space x'. When simultaneous keys are pressed, such as CTRL and 'x', the keystroke would be: 'ctrl+x'. Another example would be to open a file in LibreOffice Writer. The keystrokes would be 'alt+f o', or even the shortcut key 'ctrl+o'.

Looking at the previous options for the key command, you may have noticed the –window option which requires the window id. You may wonder how you find the id of a specific window. Finding the id is a simple procedure which will be covered more in another article, but I can give the basics of it now since it is needed.

To find the window id of a specific window, use the following command:

xdotool search --name TITLE

“TITLE” is replaced with a part or all of the title of the window. When any application is opened, the title bar shows the window title. Use this name or part of it to determine the window id as shown in Figure 1.
xDoTool - Keyboard Figure 1.jpg

FIGURE 1

You can see I did a search for 'google'. I could have searched for any word of the title name of 'google mozilla firefox'. I received only one response of “52428942” since there is only one match for the title name of 'google' on the system.

Now, let us assume I wanted to do something easy, to shutdown Firefox with the xdotool command. To close Firefox, I need to send it the keystroke of alt+f to open the File menu, then a 'Q' to select Quit. The command would be:

xdotool key --window 52428942 alt+f Q

NOTE: If you enter everything correctly and your system supports xdotool, then Firefox should close. If you have multiple tabs opened there should be a message box appears to ask to close all tabs.

The next command is the 'keydown' command. The keydown produces the effect of holding down a key on the keyboard. The key will remain pressed until you press CTRL+C.

The syntax is:

keydown [options] [key]

The options are the same as the “key” command. The following command would produce an effect in the current window to have the letter 'J' held down on the keyboard.

xdotool keydown J

The letter “J” will repeat continuously until you press CTRL+C.

Another command is the 'keyup' command to stop the keydown. The options are the same as key and keydown commands. The following command will allow the letter 'J' to be held down, and then the key is released with the keyup command after a delay of 500 ms:

xdotool keydown J; xdotol keyup --delay 500 J

Multiple commands can be placed on a single line separated by a semicolon (;).

Another way to chain commands is to place multiple xdotool commands only separated by a space as follows:

xdotool keydown J keyup --delay 500 J

The last keyboard command for xdotool is 'type'. The syntax is as follows:

type [options] [string]

The options are the same as the other commands for the keyboard and work the same way.

For instance, to type a string into the Firefox application in the currently selected box, perform the following:

xdotool type --window 46137486 www.linux.org

The window id for Firefox is 46137486. If the current selected input box is the search box for Google, then 'www.linux.org' would be typed in the search box. If the current input is the address box, then 'www.linux.org' would be in the address box.

If the address box is selected as the current input box, then you could have it automatically put in 'www.linux.org' as follows:

xdotool type --window 46137486 www.linux.org

All characters after type are accepted as the string to type. No other commands can be placed after type.

Practice on the keyboard commands and I will cover the mouse commands next time.
 

Attachments




Members online


Top