Mouseclick

Discussion in 'Python' started by Terje Johan Abrahamsen, May 2, 2005.

  1. Hello.

    I have been trying desperately for a while to make Python push the
    left mousebutton. I have been able to let Python push a button in a
    box:

    def click(hwnd):
    win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
    win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)

    optDialog = findTopWindow(wantedText="Options")

    def findAButtonCalledOK(hwnd, windowText, windowClass):
    return windowClass == "Button" and windowText == "OK"
    okButton = findControl(optDialog, findAButtonCalledOK)

    click(okButton)

    As described here, http://tinyurl.com/cwjls. But, that is not what I
    am looking for. I would like to specify some coordinates such as
    windll.user32.SetCursorPos(450, 370) and thereafter click the left
    mousebutton at that place.

    I know that the sollution lies somewhere with Microsoft
    (http://www.6URL.com/FED), but cannot understand how to make Python
    click the button regardless of how much I try.

    Thanks in advance.
     
    Terje Johan Abrahamsen, May 2, 2005
    #1
    1. Advertising

  2. Terje Johan Abrahamsen

    Guest

    Benji York wrote:
    > Terje Johan Abrahamsen wrote:
    > > I have been trying desperately for a while to make Python push the
    > > left mousebutton.

    >
    > Here's some code I've used to simulate a *right* click, but it should


    > be obvious how to make it do what you want:
    >
    > void sendRightClick(void)
    > {
    > PostMessage(GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON,

    0);
    > PostMessage(GetForegroundWindow(), WM_RBUTTONUP, 0, 0);
    > }


    Thanks for the input. I have 'Pythonized' the code a little:

    >>> def click():

    .... windll.user32.SetCursorPos(100, 100)
    .... win32gui.PostMessage(win32gui.GetActiveWindow(),
    win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, 0)
    .... win32gui.PostMessage(win32gui.GetActiveWindow(),
    win32con.WM_LBUTTONUP, 0, 0)

    and when I try it like this:

    n = click()

    Sure, the cursor moves, but it does not click. What am I doing wrong?

    Thanks in advance
     
    , May 2, 2005
    #2
    1. Advertising

  3. Terje Johan Abrahamsen

    Guest

    Benji York wrote:
    > Terje Johan Abrahamsen wrote:
    > > I have been trying desperately for a while to make Python push the
    > > left mousebutton.

    >
    > Here's some code I've used to simulate a *right* click, but it should


    > be obvious how to make it do what you want:
    >
    > void sendRightClick(void)
    > {
    > PostMessage(GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON,

    0);
    > PostMessage(GetForegroundWindow(), WM_RBUTTONUP, 0, 0);
    > }


    Thanks for the input. I have 'Pythonized' the code a little:

    >>> def click():

    .... windll.user32.SetCursorPos(100, 100)
    .... win32gui.PostMessage(win32gui.GetActiveWindow(),
    win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, 0)
    .... win32gui.PostMessage(win32gui.GetActiveWindow(),
    win32con.WM_LBUTTONUP, 0, 0)

    and when I try it like this:

    n = click()

    Sure, the cursor moves, but it does not click. What am I doing wrong?

    Thanks in advance
     
    , May 2, 2005
    #3
  4. Terje Johan Abrahamsen

    Benji York Guest

    Terje Johan Abrahamsen wrote:
    > I have been trying desperately for a while to make Python push the
    > left mousebutton.


    Here's some code I've used to simulate a *right* click, but it should
    be obvious how to make it do what you want:

    void sendRightClick(void)
    {
    PostMessage(GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON, 0);
    PostMessage(GetForegroundWindow(), WM_RBUTTONUP, 0, 0);
    }

    --
    Benji York
     
    Benji York, May 2, 2005
    #4
  5. Terje Johan Abrahamsen

    snoe Guest

    I did this a little while ago, there's some setup stuff in here for
    sending keyboard commands as well. Coercing the structs into python was
    the hardest part. Basically Click(x,y) moves the mouse to the specified
    spot on the screen (not window) sends a mouse down followed by mouse up
    event then returns to your original position. Sorry for lack of
    comments.


    from ctypes import *
    import time


    PUL = POINTER(c_ulong)
    class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
    ("wScan", c_ushort),
    ("dwFlags", c_ulong),
    ("time", c_ulong),
    ("dwExtraInfo", PUL)]

    class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
    ("wParamL", c_short),
    ("wParamH", c_ushort)]

    class MouseInput(Structure):
    _fields_ = [("dx", c_long),
    ("dy", c_long),
    ("mouseData", c_ulong),
    ("dwFlags", c_ulong),
    ("time",c_ulong),
    ("dwExtraInfo", PUL)]

    class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
    ("mi", MouseInput),
    ("hi", HardwareInput)]

    class Input(Structure):
    _fields_ = [("type", c_ulong),
    ("ii", Input_I)]

    class POINT(Structure):
    _fields_ = [("x", c_ulong),
    ("y", c_ulong)]

    def Click(x,y):

    orig = POINT()

    windll.user32.GetCursorPos(byref(orig))

    windll.user32.SetCursorPos(x,y)

    FInputs = Input * 2
    extra = c_ulong(0)

    ii_ = Input_I()
    ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )

    ii2_ = Input_I()
    ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )

    x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )

    windll.user32.SendInput(2, pointer(x), sizeof(x[0]))

    return orig.x, orig.y
     
    snoe, May 2, 2005
    #5
  6. Terje Johan Abrahamsen

    M.E.Farmer Guest

    This could be fun!
    # random clicks
    # play with values to maximize annoyance
    if __name__ == "__main__":
    import random
    for i in range(1000):
    x = random.randint(0,800)
    y = random.randint(0,600)
    Click(x,y)
    time.sleep(random.randint(0,20))
     
    M.E.Farmer, May 3, 2005
    #6
  7. Terje Johan Abrahamsen

    Guest

    Terje Johan Abrahamsen wrote:
    > Hello.
    >
    > I have been trying desperately for a while to make Python push the
    > left mousebutton. I have been able to let Python push a button in a
    > box:
    >
    > def click(hwnd):
    > win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
    > win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)
    >
    > optDialog = findTopWindow(wantedText="Options")
    >
    > def findAButtonCalledOK(hwnd, windowText, windowClass):
    > return windowClass == "Button" and windowText == "OK"
    > okButton = findControl(optDialog, findAButtonCalledOK)
    >
    > click(okButton)
    >
    > As described here, http://tinyurl.com/cwjls. But, that is not what I
    > am looking for. I would like to specify some coordinates such as
    > windll.user32.SetCursorPos(450, 370) and thereafter click the left
    > mousebutton at that place.
    >
    > I know that the sollution lies somewhere with Microsoft
    > (http://www.6URL.com/FED), but cannot understand how to make Python
    > click the button regardless of how much I try.
    >
    > Thanks in advance.



    Another, perhaps not so cool, way of doing this is to just invoke the
    mouse handler functions directly. e.g.:

    class Event:
    def __init__(self, x, y):
    self.x = x
    self.y = y

    class Whatever:
    .
    .
    def mouseClick(self, event):
    self.mouseDown(event) # link to ButtonPress handler
    self.mouseUp(event) # link to ButtonRelease handler

    ...
    event.x, event.y = 123, 456
    self.mouseClick(event)
     
    , May 3, 2005
    #7
    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. watzlaw wutz
    Replies:
    2
    Views:
    808
    Pasturel
    May 11, 2004
  2. jediknight

    Catching MouseClick events

    jediknight, Jul 4, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    358
    Hans Kesting
    Jul 4, 2006
  3. KNS

    winapi: mouseclick

    KNS, Jan 26, 2004, in forum: Python
    Replies:
    1
    Views:
    393
  4. Tim Golden

    RE: winapi: mouseclick

    Tim Golden, Jan 27, 2004, in forum: Python
    Replies:
    1
    Views:
    455
  5. Tim Golden

    RE: winapi: mouseclick

    Tim Golden, Jan 27, 2004, in forum: Python
    Replies:
    0
    Views:
    390
    Tim Golden
    Jan 27, 2004
Loading...

Share This Page