Python methods for capturing and simulating mouse events

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.