• 鼠标持续点击


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

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

    其实就是一个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

  • 相关阅读:
    fcc的高级算法题
    jq on方法绑定多个事件
    高效的jQuery代码编写技巧
    HTML 提高页面加载速度的方法
    link 和 import 导入外部样式的区别
    js将多个方法添加到window对象上的多种方法
    js 数组删去重复的加上没有的元素
    JS中定义对象原型的两种使用方法
    CSS 清除默认样式
    JavaScript中定义对象的四种方式 2012-5-10 15:19 阅读(0)
  • 原文地址:https://www.cnblogs.com/thewindkee/p/12873267.html
Copyright © 2020-2023  润新知