Automatic Keyboard Presser on Linux

Automatic keyboard pressers are useful tools. However, it seems that there is no simple and easy to use automatic keyboard presser for Linux if you search for “automatic keyboard presser linux”. After some digging, I find using the xvkbd with some options is a good method for automatic keyboard pressing though it is not designed for this purpose.

One example

Let’s use one example here to introduce how to use xvkbd as a automatic keyboard presser: we want to refresh the browser every 5 seconds to check a webpage for changes. If you do it manually, you press Ctrl + r every 5 seconds. Let’s make the computer do it automatically for us while what we need to do is just to watch the screen while drinking a cup of tea.

xvkbd sends a string

xvkbd has an option -text which can send the string to the focused window:

-text string
Send the string to the focused window (see also `-window' option). 
If this option is specified, xvkbd will not open its window and terminate soon after sending the string.
The string can contain:

\r - Return
\t - Tab
\b - Backspace
\e - Escape
\d - Delete
\S - Shift (modify the next character; please note that modify with ``\S'' will be ignored in many cases. For example, ``a\Cb\ScD\CE'' will be interpreted as a, Control-b, c, Shift-D, and Control-Shift-E.)
\C - Control (modify the next character)
\A - Alt (modify the next character)
\M - Meta (modify the next character)
\[keysym] - the keysym keysym (e.g., \[Left]), which will be processed in the similar matter with other general characters
\{keysym} - the keysym keysym (e.g., \{Left}), which will be processed in more primitive matter and can also be used for modofier keys such as Control_L, Meta_L, etc.; also, \{+keysym} and \{+keysym} will simulate press and release of the key, respectively 
\Ddigit - delay digit * 100 ms
\xvalue - move mouse pointer (use "+" or "-" for relative motion)
\yvalue - move mouse pointer (use "+" or "-" for relative motion)
\mdigit - simulate click of the specified mouse button

With this option, we can send out Ctrl + r by:

xvkbd -text "\Cr"

The script as the automatic key presser

With the option of xvkbd, we can write our own automatic key presser with a bit bash script to repeatedly invoke xvkbd.

Here comes the script that automatically refresh the browser (the active window).

while true; do xvkbd -text "\Cr"; sleep 5; done;

Execute it in a shell, make the browser focused and then you can watch the page “automatically” refreshed.

When you want to stop the “automatic keyboard presser”, just press Ctrl+c in the shell window.

More interesting and complex key pressers can be made using the functions provided by xvkbd (especially the \D to delay a little while).

Eric Zhiqiang Ma

Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

3 comments:

Leave a Reply

Your email address will not be published. Required fields are marked *