Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

xdotool Install 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.

share|improve this answer
    
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 '10 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 '10 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 '10 at 3:45
    
@fread.bear: Thanks, corrected. –  Riccardo Murri Dec 11 '10 at 12:32
add comment

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.

share|improve this answer
    
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 '10 at 10:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.