• 鼠标持续点击


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

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

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

  • 相关阅读:
    UOJ425【集训队作业2018】strings【分块,常数优化】
    UOJ444【集训队作业2018】二分图【构造,结论】
    各种类型转换为字符串类型(ToString())
    ADO.net数据绑定
    关于扫雷游戏学习笔记(二)
    关于扫雷游戏学习笔记(一)
    Luogu P2864 [USACO06JAN]树林The Grove(bfs)
    Luogu P1712 [NOI2016]区间(线段树)
    Luogu P2051 [AHOI2009]中国象棋(dp)
    Luogu P2577 [ZJOI2005]午餐(dp)
  • 原文地址:https://www.cnblogs.com/thewindkee/p/12873267.html
Copyright © 2020-2023  润新知