Setting the mouse position

Discussion in 'Ruby' started by Gues, Dec 27, 2005.

  1. Gues

    Gues Guest

    Hi,

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

    Also,

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

    Thanks.

    --
    Posted via http://www.ruby-forum.com/.
    Gues, Dec 27, 2005
    #1
    1. Advertising

  2. 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 =3D Win32API.new("user32", "SetCursorPos", ['I','I'], 'V')
    setCursorPos.Call(100,100)

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

    require 'Win32API'

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

    Ryan

    On 12/27/05, Gues <> wrote:
    > Hi,
    >
    > How can I set the mouse position? So change the mouse to a specified
    > (x,y) on the screen?
    >
    > Also,
    >
    > How can I get the location of a window on the screen of which I know the
    > title?
    >
    > Thanks.
    Ryan Leavengood, Dec 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    2
    Views:
    1,286
    Jim Corey
    Apr 23, 2004
  2. Max
    Replies:
    7
    Views:
    9,076
  3. tom arnall
    Replies:
    6
    Views:
    1,256
    Ian Shef
    Jan 18, 2007
  4. James Black
    Replies:
    0
    Views:
    389
    James Black
    May 28, 2006
  5. Akhenaten

    Setting default mouse position

    Akhenaten, Jun 13, 2007, in forum: Javascript
    Replies:
    3
    Views:
    93
    Tim Slattery
    Jun 14, 2007
Loading...

Share This Page