DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Simulating a mouseclick? (http://www.daniweb.com/forums/thread123475.html)

tondeuse34 May 10th, 2008 12:58 am
Simulating a mouseclick?
 
Hey guys, is it possible to simulate a mouse click using x and y co-ordinates. And also could you have user input of a certain co-ordinate and it would click that specified co-ordinate? thanks.

Shadow14l May 12th, 2008 11:53 pm
Re: Simulating a mouseclick?
 
I found a very neat sample of doing this:

from ctypes import *
import time, curses, win32con, win32gui

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


click(150, 150)

It may be long, but it is one of the few examples I have seen that work with little hassle in Python.

For your second question, I'd probably bind with gui. Follow the example here but it doesn't have to be right click:

http://www.daniweb.com/forums/post598858.html

tondeuse34 May 13th, 2008 5:40 pm
Re: Simulating a mouseclick?
 
also, how would you use Tkinter with two buttons for example to save a .txt document all you have to do is click that button and it will make a prompt for the filename and save it. Thanks

jrcagle May 13th, 2008 9:30 pm
Re: Simulating a mouseclick?
 
If you're talking about mouse clicks in Tkinter, then you will want to check out the manual:

http://infohost.nmt.edu/tcc/help/pub...er/tkinter.pdf

OR in HTML format

http://infohost.nmt.edu/tcc/help/pubs/tkinter/
http://www.pythonware.com/library/index.htm

To get mouse clicks, check out the Bind method at the end of the manual. To handle file dialogs, check out the tkFileDialog module.

Hope that helps,
Jeff

tondeuse34 May 13th, 2008 10:34 pm
Re: Simulating a mouseclick?
 
thanks answered my question


All times are GMT -4. The time now is 9:24 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC