6

I want to position the mouse in relation to a window.

I've tried several command-line apps which send keyboard and mouse messages to X, but they all seem to only place the mouse in relation to the screen.

Is there some command-line/script way of moving the mouse in relation to a window?

2 Answers 2

6

xdotool can do this:

xdotool mousemove --window $WINDOWID $x $y

The window id can be an X11 window id or a reference to the result of a previous xdotool's window selection command; see the man page for details.

3
  • I've run into a (hopefully)minor problem... I am using Lucid, and my apt installs an earlier version than the one with the syntax you have shown... My version does not allow that option at all... The above syntax is for the Maverick version... I suppose it will be okay to install it into Lucid... so this is still "work in progress"... (I need a coffee.. )
    – Peter.O
    Oct 24, 2010 at 10:47
  • Whoo-hoo! You beauty.. Thanks... You and that Maverick Meerkat deserve a medal.. nice and simple.. a one-liner: xdotool mousemove -window $(xdotool getwindowfocus), 300, 300 (it is "-window" not "--" by the way).. just tried it again; bothe work "-window" and "--windows"
    – Peter.O
    Oct 24, 2010 at 11:49
  • A late update: I've just noticed that the "$WINDOW_ID" should be $WINDOWID ...that's why I had used $(xdotool getwindowfocus)
    – Peter.O
    Dec 11, 2010 at 3:45
1

Have you tried XWarpPointer? The manual page says:

       If dest_w is None, XWarpPointer moves the pointer by the offsets
       (dest_x, dest_y) relative to the current position of the pointer.  If
       dest_w is a window, XWarpPointer moves the pointer to the offsets
       (dest_x, dest_y) relative to the origin of dest_w.  However, if src_w
       is a window, the move only takes place if the window src_w contains the
       pointer and if the specified rectangle of src_w contains the pointer.

It's not a standalone utility, but an an Xlib function; you should be able to use it via the Python Xlib bindings.

1
  • Muarat, I'm new to both bash and python, and I'm having a few problems... I feel it's tantalizingly close, but i'm stuck on how to identify dst_w in a python script... I'll keep pecking away at it, but if you can give me an example that would be great... My "dst_w" is KDE's konsole window.
    – Peter.O
    Oct 24, 2010 at 10:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .