zoukankan      html  css  js  c++  java
  • 鼠标持续点击

    最近玩一款老游戏, 里面开箱子太麻烦了, 一直手动点, 朋友让我试着写一个脚本。 就搜了下, 挺好实现的, 就试着写出来了。

    修改下 也可以做些其他重复的事情。  

    其实就是一个x键精灵的事情大笑


    我的系统是 win7 64, python34

    用到了pyhooked,pypiwin32


    pyhook我一直安不上,就用的pyhooked,win32,可以直接用pip install pypiwin32安装。


    from pyhooked import Hook, KeyboardEvent, MouseEvent
    import win32gui, win32con, win32api
    import time
    from ctypes import *
    
    import threading
    
    
    def mouse_click(x=None, y=None):
        if not x is None and not y is None:
            mouse_move(x, y)
            time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    
    
    def mouse_dclick(x=None, y=None):
        if not x is None and not y is None:
            mouse_move(x, y)
            time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    
    
    def mouse_move(x, y):
        windll.user32.SetCursorPos(x, y)
    
    
    class MyThread(threading.Thread):
        def __init__(self, timesleep=0.15):
            super(MyThread, self).__init__()
            self.stopped = False
            self.timesleep = timesleep
    
        def run(self):
            print('启动')
            x, y = win32gui.GetCursorPos()
            count = 0
            while not self.stopped:
                count += 1
                mouse_click(x, y)
                time.sleep(self.timesleep)
            print('本次点击了%s次' % count)
    
        def stop(self):
            self.stopped = True
            print('停止')
    
        def isStopped(self):
            return self.stopped
    
    
    class MyHook(Hook):
        def __init__(self):
            Hook.__init__(self)
            self.open = False
            self.thread = None
    
        def handle_events(self, args):
            if isinstance(args, KeyboardEvent) and not self.open and args.current_key == 'F2' and args.event_type == 'key down':
                self.thread = MyThread()
                self.thread.start()
                self.open = True
    
            elif isinstance(args,KeyboardEvent) and self.open and args.current_key == 'F2' and args.event_type == 'key down':
                self.open = False
                self.thread.stop()
                time.sleep(1)
    
    
    if __name__ == '__main__':
        print('F2启动,再次F2关闭,启动时请不要移动鼠标')
        hk = MyHook()  # make a new instance of PyHooked
        hk.handler = hk.handle_events  # add a new shortcut
        hk.hook()  # hook into the events, and listen to the presses
    


    参考 :http://www.jb51.net/article/47422.htm

  • 相关阅读:
    Reflector 已经out了,试试ILSpy
    visio studio删除空行
    SQL语句增加字段、修改字段、修改类型、修改默认值
    判断两个集合中 是否有相同的元素
    Rdlc 参数问题
    SQL Server 2008 报表服务入门【转】
    WebAPI异常捕捉处理,结合log4net日志(webapi2框架)
    HTTP Error 500.30
    前端Json 增加,删除,修改元素(包含json数组处理)
    IE浏览器F12调试模式不能使用或报错以及安装程序遇到错误0x80240037的解决办法
  • 原文地址:https://www.cnblogs.com/thewindkee/p/12873267.html
Copyright © 2011-2022 走看看