在python中控制滑鼠

來源:互聯網
上載者:User
在python中控制滑鼠

作者:Snake發布時間:October 2, 2010分類: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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.