Control the mouse in Python
Author: Snake release date: October 2, 2010 classification: Python
1. Move the mouse in the focus window
# Coding = GBK
From ctypes import *
Import time
USER32 = Windll. USER32
Kernel32 = Windll. Kernel32
Class rect (structure ):
_ Fields _ = [
("Left", c_ulong ),
("TOP", c_ulong ),
("Right", c_ulong ),
("Bottom", c_ulong)
]
Class guithreadinfo (structure ):
_ Fields _ = [
("Cbsize", c_ulong ),
("Flags", c_ulong ),
("Hwndactive", c_ulong ),
("Hwndfocus", c_ulong ),
("Hwndcapture", c_ulong ),
("Hwndmenuowner", c_ulong ),
("Hwndmovesize", c_ulong ),
("Hwndcaret", c_ulong ),
("Rccaret", rect)
]
Def movecursorincurrentwindow (x, y ):
# Find the focussed window.
Guithreadinfo = guithreadinfo (cbsize = sizeof (guithreadinfo ))
User32.getguithreadinfo (0, byref (guithreadinfo ))
Focussedwindow = guithreadinfo. hwndfocus
# Find the screen position of the window.
Windowrect = rect ()
User32.getwindowrect (focussedwindow, byref (windowrect ))
# Finally, move the cursor relative to the window.
User32.setcursorpos (windowrect. Left + X, windowrect. Top + Y)
For I in range (0, 10 ):
Time. Sleep (1)
Movecursorincurrentwindow (I * 15, I * 10)
2. Move the mouse
From ctypes import *
Windll. user32.setcursorpos (100,100)
3. Get the current mouse Coordinate
Import win32gui
Win32gui. getcursorpos ()
4. Simulate mouse clicks
# Coding = GBK
Import WIN32API
Import win32con
From ctypes import Windll
# Left mouse button
WIN32API. mouse_event (win32con. mouseeventf_leftdown, x, y)
Time. Sleep (0.05)
WIN32API. mouse_event (win32con. mouseeventf_leftup, x, y)
# Right-click
WIN32API. mouse_event (win32con. mouseeventf_rightdown, x, y)
Time. Sleep (0.05)
WIN32API. mouse_event (win32con. mouseeventf_rightup, x, y)
5. Get mouse events
Import win32con
Import win32gui
Import ctypes
From ctypes import wintypes
# Container class for global hook
# This will store the hhook ID and mouse information
Class HOOK:
Def _ init _ (Self ):
Self. Hook = 0
Self. m_struct = none
Class msllhookstruct (ctypes. structure ):
_ Fields _ = [("PT", wintypes. Point ),
("Mousedata", ctypes. c_long ),
("Flags", ctypes. c_long ),
("Time", ctypes. c_long ),
("Dwextrainfo", ctypes. pointer (ctypes. c_ulong)]
Def copymemory (destination, source ):
Source = ctypes. c_void_p (source)
Ctypes. Windll. kernel32.rtlmovememory (ctypes. addressof (destination), source, ctypes. sizeof (destination ))
Def postquitmessage (nmsg ):
Return ctypes. Windll. user32.postquitmessage (nmsg)
Def getmodulehandle (lpmodulename ):
Return ctypes. Windll. kernel32.getmodulehandlea (lpmodulename)
Def callnexthookex (HHK, ncode, wparam, lparam ):
Return ctypes. Windll. user32.callnexthookex (HHK, ncode, wparam, lparam)
Def setwindowshookex (idhook, lpfunc, hmod, dwthreadid ):
Winfunc = ctypes. winfunctype (ctypes. c_long, ctypes. c_long)
Return ctypes. Windll. user32.setwindowshookexa (idhook, winfunc (lpfunc), hmod, dwthreadid)
Def unhookwindowshookex (HHK ):
Return ctypes. Windll. user32.unhookwindowshookex (HHK)
# Create instance of global mouse hook class
Mll_hook = hook ()
Mll_hook.m_struct = msllhookstruct ()
# Mouse hook callback. Intercept mouse events
Def lowlevelmouseproc (ncode, wparam, lparam ):
If ncode = win32con. hc_action:
# Lparam holds the starting address of the mouse hook Structure
# Call copymemory so that m_struct class points to the mouse structure pool
Copymemory (mll_hook.m_struct, lparam)
# Print out the cursors X and Y screen position
If wparam = win32con. wm_mbuttonup:
Postquitmessage (0)
If wparam = win32con. wm_lbuttonup: # wm_rbuttonup
Print "x = [% d] \ ty = [% d]" % (mll_hook.m_struct.pt.x, mll_hook.m_struct.pt.y)
Return callnexthookex (mll_hook.hook, ncode, wparam, lparam)
If _ name _ = '_ main __':
Print "press the middle mouse button to exit"
Try:
Mll_hook.hook = setwindowshookex (win32con. wh_mouse_ll,
Lowlevelmouseproc,
Getmodulehandle (0 ),
0)
Except t exception, err:
Print err
# Set up a message queue, you can use any valid message loop tkinter, pygtk and wxpythons message loops all work
Win32gui. pumpmessages ()
# Unhook the mouse hook
Unhookwindowshookex (mll_hook.hook)