A mouse emulator for SSH in BASH

October 17, 2022

There is a Linux utility called x2x that forwards all keyboard and mouse events through SSH. For some reason, since I switched from Debian to Manjaro I can’t use that utility. It always complains with an error on X forwarding over SSH.

I did try to solve the issue, to no avail, but without really insisting either. So I dropped it and went for a personal solution.

This is a BASH script that combines SSH, read and xdotool. It does not emulate the application x2x. It offers a functionality comparable to x2x while being a distinctly different kind of app.

It has to be installed in the remote machine. You have to have SSH access to said remote machine. The remote machine has to have X11 running, while the local one may not. This is a big difference vis-à-vis x2x, which requires both machines having X11. The local machine can not even have X11 installed and run through a plain TTY!

Once launched, an infinite loop tees off. It reads keyboard inputs. What is forwarding the key presses is actually SSH, not my script. While x2x forwards mouse to mouse, my script forwards keyboard to mouse. It does so locally, in the remote machine. The forwarding is taken care of by SSH, as I said earlier.

Copy-paste this chunk to a text file and save it in the remote machine:

#!/bin/bash

STEP=20
while :
do
read -s -n 1 KEY
case $KEY in
q) exit ;;
h) xdotool mousemove_relative -- -$STEP 0 ;;
l) xdotool mousemove_relative $STEP 0 ;;
j) xdotool mousemove_relative 0 $STEP ;;
k) xdotool mousemove_relative -- 0 -$STEP ;;
+) STEP=$((STEP+5)) ;;
-) STEP=$((STEP-5)) ;;
u) xdotool click 1;;
i) xdotool click 2;;
o) xdotool click 3;;
J) xdotool click 5;;
K) xdotool click 4;;
t) read -p "Type here: " TEXT; xdotool type $TEXT ;;
x) xdotool key 0xff08 ;;
"") xdotool key 0xff0d ;;
esac
done

Don’t forget to make it executable.

Once you execute it, the script will read the following key presses, without echoing anything so STDOUT:

h mouse movement, left

l mouse movement, right

j mouse movement, down

k mouse movement, up

u mouse left click

i mouse middle click

o mouse right click

J mouse scroll down

K mouse scroll up

+ increase the pixels per movent, or make the mouse movements faster

- decrease the pixels per movement, thus slowing down the mouse movements

t to insert text into the focused window. Pressing return sends the text, without the carriage return.

x sends a backspace key press

q quits the loop and returns control to the shell

anything else, even return, sends a return carriage :)

I tried to use the Vim keybindings. It is so transparent and easy to customize, that, heck, go to town, change the keybindings to suit your liking.

Can it be used locally? Well, sure, it can. It is a pain in the posterior using the mouse clicks, as it focuses the windows you click on, so you have to manually give control back to the X11 terminal emulator where you run the script.

A possible use case to run it locally might be, say, if your physical mouse broke. This thing is certainly better than no mouse.

Combine this with Android and Termux, and you are using your mobile phone as a mouse for your Linux PC!


Comment Box is loading comments…

Flag Counter


This web page was created using Linux, Vim, markdown and pandoc. Email me at gasconheart@sdf.org. My Skype is gasconheart@tilde.team. My Discord is gasconheart#1668. Reach me through Telegram: +34 640667425. Go back to the home page of my blog.