Capturing and simulating mouse events with Python __python

Source: Internet
Author: User
This holiday has played a lot of galgame, but some very old game does not automatically run mode, click the mouse and too hurt the button, so want to roll the mouse wheel map to click the mouse.
Online search, did not find any off-the-shelf software, and the key wizard is too heavy, so consider simply using Python to write a forget.

Very lucky to find the "with Python monitor mouse and keyboard events" This article, so the Pyhook and PyWin32 are installed (recommended under EXE version, lest the installation of all kinds of egg pain).

Turned over the tutorial, found very simple:

#-*-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 an event to another handler, otherwise stop propagate event return
    true

# Create Hook management object
HM = Pyhook.hookmanager ()
# listens for all mouse events
hm. Mouseall = onmouseevent # is equivalent to HM. Subscribemouseall (Onmouseevent)
# begins to monitor mouse events
hm. Hookmouse ()
# listens until manually exiting the program
pythoncom. Pumpmessages ()

This example program captures all the mouse events, and in fact I just need to catch the events that scroll down the wheel. Turn over the document, corresponding to the MouseWheel, and then just judge the event. Whether the wheel is-1 can be.

The last is to trigger the mouse click, which will need to use the win32api.mouse_event (), send a press the left mouse button event, and then send the bounce event, the completion of a click.

The final code is as follows:

#-*-Coding:utf-8-*-

import pythoncom import pyhook import time
import win32api
import win32con< C5/>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) return
    True

HM = Pyhook.hookmanager ()
hm. MouseWheel = OnMouseWheel
hm. Hookmouse ()

pythoncom. Pumpmessages ()



Related Article

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.