Emulation of mouse movements and keystrokes
September 16, 2012 • 3 minutes read

How can i emulate keyboard and mouse activity on the system? How to automate it?

This can be done with xte that included in the xautomation package.

Install the xautomation package

sudo apt-get install xautomation    

Now we have xte, which can simulate the movement of the mouse and keyboard keypress

Examples of xte usage

Move the cursor to the point x = 10 and y = 20 on the screen grid

xte 'mousemove 10 20' 

Shift relative to the current cursor location on x = 10 and y = 10

xte 'mousermove 10 10' 

Left mouse click

xte 'mouseclick 1'

Right mouse click

xte 'mouseclick 3' 

Press key l

xte 'key l' 

Press left Shift

xte 'key Shift_L' 

Below is a list of possible values ​​for the key

Home
Left
Up
Right
Down
Page_Up
Page_Down
End
Return
BackSpace
Tab
Escape
Delete
Shift_L
Shift_R
Control_L
Control_R
Meta_L
Meta_R
Alt_L
Alt_R  

Automation activity for keyboard and mouse

It is possible to write a small bash script

#!/bin/bash
randomKeys=(Shift_L Shift_R Control_L Control_R)
keysCount=${#randomKeys[*]}
while [  true ]; do  
  #random sign
  sign='+'
  if [ $((RANDOM%2+0)) -eq 0 ]
  then sign='-'
  fi
  #random mouse coordinates
  x=$((RANDOM%50+1))
  y=$((RANDOM%50+1))
  #mouse move
  xte "mousermove $sign$x $sign$y"
  #random key press
  randomKey=${randomKeys[$((RANDOM%$keysCount+0))]}
  xte "key $randomKey"
  sleep 1
done

This script moves mouse relative location on a random value for x and y every second. And also emulates pressing the Shift or Control

Last update May 9, 2021
Development linux bash automation
Translation of this article: Русский
Do you have any questions?

Contact Me