2010-06-23

Moving the pointer to the top left corner of the screen

I wanted a keystroke to send the pointer to the top left of the screen. Usually I do this by flipping to the other screen and back but on my laptop (with only one screen) this doesn't work.

The solution was to use xmousepos to get the mouse position (want to know which screen we're on) and xte to set it. These are part of the xautomation package in Ubuntu/Debian.

But how big are the screens? I'm using xrandr and assuming that the resolutions listed are in order left to right and no screens are up or down or rotated or anything crazy like that.

Here's my script.

#!/bin/sh

# get current horizontal mouse position
mousex=$(xmousepos | awk '{print $1}')

# walk through widths of screens (assuming they're in order left to right) until 
# the mouse is on the current screen, building offset from left
offset=0
for width in $(xrandr | grep '*' | awk '{print $1}' | sed -r 's/x.*//'); do
 [ $mousex -lt $(expr $offset + $width) ] && break
 offset=$(expr $offset + $width)
done

# move the pointer to the top left of the screen
xte "mousemove $offset 0"

xte can send other fake input to X too such as clicking, pressing and releasing keys and so on. Potentially very useful.

No comments:

Post a Comment