Forum: Ruby Setting the mouse position

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Abb249055208c7af4d35568e422dfd63?d=identicon&s=25 Gues (Guest)
on 2005-12-27 14:40

How can I set the mouse position? So change the mouse to a specified
(x,y) on the screen?


How can I get the location of a window on the screen of which I know the

4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2005-12-27 18:57
(Received via mailing list)
I assume you are asking about Microsoft Windows. This is a very
specific question which is more related to Windows programming and the
Windows API than Ruby programming. But nonetheless:

1. To set the mouse position you need the SetCursorPos function, which
you can access from Ruby like so:

require 'Win32API'

setCursorPos ="user32", "SetCursorPos", ['I','I'], 'V')

2. To get the location of a Window whose title you know, do the

require 'Win32API'

findWindow ="user32", "FindWindow", ['P','P'], 'L')
getClientRect ="user32", "GetClientRect", ['P','P'], 'V')
hwnd = findWindow.Call(nil, 'name of window here')
lpRect = " " * 16 # Four LONGs
getClientRect.Call(hwnd, lpRect)
left, top, right, bottom = lpRect.unpack('LLLL')
puts "Left: #{left}, Top: #{top}, Right: #{right}, Bottom: #{bottom}"

This topic is locked and can not be replied to.