鼠标持续点击
最近玩一款老游戏, 里面开箱子太麻烦了, 一直手动点, 朋友让我试着写一个脚本。 就搜了下, 挺好实现的, 就试着写出来了。
修改下 也可以做些其他重复的事情。
其实就是一个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