在python中控制鼠标

一.在获得焦点的窗口中移动鼠标

#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)


二.移动鼠标

from ctypes import *

windll.user32.SetCursorPos(100, 100)


三.获得鼠标当前坐标

import win32gui

win32gui.GetCursorPos()


四.模拟鼠标点击

#coding=gbk

import win32api

import win32con

from ctypes import windll


#鼠标左键

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y)

time.sleep(0.05)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y)


#鼠标右键

win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, x, y)

time.sleep(0.05)

win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, x, y)


五.获取鼠标事件

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, 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 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)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值