The examples in this article describe Python's approach to capturing and simulating mouse events. Share to everyone for your reference. The specific analysis is as follows:
This holiday played a lot of galgame, but some very old game does not automatically run mode, click the mouse and too hurt the button, so you want to scroll the mouse wheel map to click the mouse.
Search on the Internet, did not find any ready-made software, and the key wizard is too heavy, so consider simply their own python to write a forget.
Here need to put Pyhook and PyWin32 are loaded (recommended EXE version, so as not to install a variety of egg pain).
Turning over the tutorial, it is easy to realize:
#-*-Coding:utf-8-*-import pythoncom, Pyhook def onmouseevent (event): print ' MessageName: ', event. MessageName print ' Message: ', event. Message print ' time: ', event. Time print ' Window: ', event. Window print ' windowname: ', event. Windowname print ' Position: ', event. Position print ' Wheel: ', event. Wheel print ' injected: ', event. Injected print '---' # returns True to pass events to other handlers, otherwise stop propagating event return True # Create hook Management Object HM = Pyhook.hookmanager () # Listen to All Mouse event hm. Mouseall = onmouseevent # is equivalent to HM. Subscribemouseall (onmouseevent) # starts listening for mouse event hm. Hookmouse () # always listens until you manually exit the program pythoncom. Pumpmessages ()
This example program captures all mouse events, in fact I just need to capture the events of scrolling down the scroll wheel. Turn the document below, corresponding to the mousewheel, then just judge the event. Whether the wheel is 1.
The end is trigger mouse click, this need to use the win32api.mouse_event (), send a press the left mouse button event, and then send a bounce event, on completion of a click.
The final code is as follows:
#-*-coding:utf-8-*-import pythoncom import pyhook import time import win32api import Win32con def onmousewheel (event) : if event. Wheel = =-1: win32api.mouse_event (Win32con. Mouseeventf_leftdown, 0, 0) time.sleep (0.05) win32api.mouse_event (Win32con. Mouseeventf_leftup, 0, 0)
Hopefully this article will help you with Python programming.