在python中控制鼠标
来源:互联网 发布:js根据方法名调用方法 编辑:程序博客网 时间:2024/06/05 22:58
在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)
- 在python中控制鼠标
- 在python中控制鼠标
- python键盘、鼠标控制
- python pymouse 控制鼠标
- python控制鼠标 pyautogui
- DATAWINDOW 中控制鼠标
- 用VBS控制鼠标,在Excel2010、2013,64位中
- selenium+python控制鼠标移动
- python:控制鼠标和键盘
- python脚本控制鼠标和键盘-实例
- delphi控制鼠标在程序界面范围内
- 控制控件出现在鼠标位置
- 控制控件出现在鼠标位置
- 在Python中使用ORM(MongoEngine)控制MongoDB的方法
- D3D中使用鼠标控制摄像机旋转
- css控制鼠标15中状态
- 网页中鼠标按键的控制
- Python中控制语句
- sql server 数据库 function用法
- Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月28日-2月3日)
- login 与 rlogin
- Python 模拟鼠标操作
- Yii+ajax+json练习
- 在python中控制鼠标
- 用Python模拟键盘输入
- 用Python监听鼠标和键盘事件(转载)
- 用python在linux下模拟鼠标
- python实现模拟鼠标代码
- Python之Windows控件操作系列一:模拟鼠标点击
- linux-0.11调试教程,安装minix-1.5.10到硬盘(3)安装shoelace后修改/etc/rc文件和更改root密码
- 用Python监听鼠标和键盘事件
- 模拟鼠标键盘