Python监听鼠标左键被点击,gogo,急停,
最后的核心目的是:为了对cscsgogo的中的进行急停
安装基本的软件包
import pyHook # 注意事项,1、不要安装pyHook2 (会报错TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags',) # 在pycharm中创建python3.7的虚拟环境 # conda install swig 安装swig # 去https://visualstudio.microsoft.com/visual-cpp-build-tools/中安装Microsoft Visual C++ 14.0 花费1个小时 # pip install PyHook3 import pythoncom # pip install pywin32
以下是监听鼠标点击事件的code
# 监听到鼠标事件调用 def onMouseEvent(event): if(event.MessageName!="mouse move"):# 因为鼠标一动就会有很多mouse move,所以把这个过滤下 print(event.MessageName) return True # 为True才会正常调用,如果为False的话,此次事件被拦截 def main(): # 创建管理器 hm = pyHook.HookManager() # 监听鼠标 hm.MouseAll = onMouseEvent hm.HookMouse() # 循环监听 pythoncom.PumpMessages() if __name__ == "__main__": print("thi is main") main()
急停的基本逻辑是:
每次点击鼠标左键时,进入中断。在中断中进行急停判断。
逻辑是:如果此时A正在按下,点击n次D;如果此时D正在被按下,点击n次A;
n为几合适呢?
同时需需要求证的问题是,是否存在被VAC的可能?
--------------------
测试示例一:
import PyHook3 # 注意事项,1、不要安装pyHook2 (会报错TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags',) # 在pycharm中创建python3.7的虚拟环境 # conda install swig 安装swig # 去https://visualstudio.microsoft.com/visual-cpp-build-tools/中安装Microsoft Visual C++ 14.0 花费1个小时 # pip install PyHook3 import pythoncom # pip install pywin32 import keyboard # 执行单元 # pip install keyboard class glv(): keyPressed = "" def __init__(self): keyPressed = "" # 监听到鼠标事件调用 def onMouseEvent(event): # 因为鼠标一动就会有很多mouse move,所以把这个过滤下 if(event.MessageName=="mouse left down"): pass return True # 为True才会正常调用,如果为False的话,此次事件被拦截 def onKeyboardEvent(event): # print(event.MessageName) if event.MessageName=="key down": #当按键被按下去之后,主要开始记录 gv.keyPressed = chr(event.Ascii) # print("moving:{}".format(gv.keyPressed)) if event.MessageName=="key up": if gv.keyPressed == 'a': print("stop with d") keyboard.write("d", delay=0.02) if gv.keyPressed == 'd': print("stop with a") keyboard.write("a", delay=0.02) if gv.keyPressed == 'w': print("stop with s") keyboard.write("s", delay=0.02) return True def main(): # 创建管理器 hm = PyHook3.HookManager() # 监听鼠标 hm.MouseAll = onMouseEvent hm.HookMouse() # 监听键盘 hm.KeyDown = onKeyboardEvent hm.KeyUp = onKeyboardEvent hm.HookKeyboard() # 循环监听 pythoncom.PumpMessages() if __name__ == "__main__": print("thi is main") gv = glv() gv.keyPressed = "" main()
---------------
import time import PyHook3 # 注意事项,1、不要安装pyHook2 (会报错TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags',) # 在pycharm中创建python3.7的虚拟环境 # conda install swig 安装swig # 去https://visualstudio.microsoft.com/visual-cpp-build-tools/中安装Microsoft Visual C++ 14.0 花费1个小时 # pip install PyHook3 import pythoncom # pip install pywin32 # import keyboard # # 执行单元 # # pip install keyboard import win32api import win32con from time import * class glv(): keyPressed = "" def __init__(self): keyPressed = "" # 监听到鼠标事件调用 def onMouseEvent(event): # 因为鼠标一动就会有很多mouse move,所以把这个过滤下 if(event.MessageName=="mouse left down"): gv.count = 0 print("count is {}".format(gv.count)) return True # 为True才会正常调用,如果为False的话,此次事件被拦截 def onKeyboardEvent(event): gv.count = gv.count + 1 t = time() print("{:.2f}\t{}\t{}\t{}\tdelt={:.2f}".format(t,gv.count,event.MessageName, chr(event.Ascii),t-gv.lt)) gv.lt = t # print(event.MessageName) if event.MessageName=="key down": #当按键被按下去之后,主要开始记录方向 gv.keyPressed = chr(event.Ascii) return True if event.MessageName=="key up": sleep(0.005) if gv.keyPressed == 'a' or gv.keyPressed == 'A': # keyboard.press_and_release("d") #进行急停; # keyboard.press("d") # sleep(0.06) # keyboard.release("d") # for i in range(50): # keyboard.press_and_release("d") win32api.keybd_event(68,0,0,0) sleep(0.06) win32api.keybd_event(68,0,win32con.KEYEVENTF_KEYUP,0) return True return True def main(): # 创建管理器 hm = PyHook3.HookManager() # 监听鼠标 hm.MouseAll = onMouseEvent hm.HookMouse() # 监听键盘 hm.KeyDown = onKeyboardEvent hm.KeyUp = onKeyboardEvent hm.HookKeyboard() # 循环监听 pythoncom.PumpMessages() if __name__ == "__main__": print("thi is main") gv = glv() gv.keyPressed = "" gv.count = 0 gv.lt = time() # gv.firsttime = 1 main()
放弃了,虽然在按键的时间延迟上已经完美的模拟了手动急停的时间延迟,但是从实际的效果上看,还是达不到急停的效果;
--
测试三;结论放弃了,没有太大意义:
import time import PyHook3 # 注意事项,1、不要安装pyHook2 (会报错TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'flags',) # 在pycharm中创建python3.7的虚拟环境 # conda install swig 安装swig # 去https://visualstudio.microsoft.com/visual-cpp-build-tools/中安装Microsoft Visual C++ 14.0 花费1个小时 # pip install PyHook3 import pythoncom # pip install pywin32 import win32api # pip install pywin32 import win32con # pip install pywin32 # http://blog.itpub.net/26736162/viewspace-2644877/ 按键的键码 import keyboard # 执行单元 # pip install keyboard from time import * from threading import Timer import threading import sys class glv(): keyPressed = "" def __init__(self): keyPressed = "" # 监听到鼠标事件调用 def onMouseEvent(event): # 因为鼠标一动就会有很多mouse move,所以把这个过滤下 if(event.MessageName=="mouse left down"): gv.count = 0 gv.lt = time() print("count is {}".format(gv.count)) return True # 为True才会正常调用,如果为False的话,此次事件被拦截 def onKeyboardEvent(event): gv.count = gv.count + 1 t = time() print("{:.2f}\t{}\t{}\t{}\tdelt={:.2f}".format(t,gv.count,event.MessageName, chr(event.Ascii),t-gv.lt)) gv.lt = t if event.MessageName=="key down": #当按键被按下去之后,主要开始记录方向 gv.keyPressed = chr(event.Ascii) return True elif event.MessageName=="key up": if gv.keyPressed == 'a' or gv.keyPressed == 'A': # #3次代码急停1 # keyboard.press("d") # sleep(0.098) # keyboard.release("d") #3次代码急停2 # keyboard.press_and_release("d") #3次代码急停3 # for i in range(20): # keyboard.press_and_release("d") # #3次代码急停4 # keyboard.press("a") # keyboard.press("w") # keyboard.press("s") # keyboard.press("d") # sleep(0.01) # keyboard.release("a") # keyboard.release("w") # keyboard.release("s") # keyboard.release("d") #3次代码急停5 # keyboard.press("a") # sleep(0.005) # keyboard.release("a") # keyboard.press("d") # sleep(0.001) # keyboard.release("d") # 如果打开keycastow,则有急停的效果; # keyboard.press_and_release("a") # keyboard.press_and_release("d") # # 开不开keycastow都没有急停的效果 # # keyboard.press_and_release("a") # keyboard.press_and_release("d") # keyboard.press_and_release("a") #按A的目的是为了夺取键盘的控制权 # keyboard.press_and_release("d") # keyboard.write('adddddddddddddddddd') #在这里边按键,存在递归的现象,出不去 keyboard.press_and_release("a") keyboard.press_and_release("a") keyboard.press_and_release("d") return True elif gv.keyPressed == 'w' or gv.keyPressed == 'W': return True if chr(event.Ascii) == 'm':#OVER sys.exit() elif chr(event.Ascii) == 'p': #BEGIN gv.start = 1 return True elif chr(event.Ascii) == 'l' and gv.start == 1: #DO keyboard.press("a") sleep(1) keyboard.release("a") keyboard.press("d") sleep(0.07) keyboard.release("d") # for i in range(20): # keyboard.press_and_release("a") # keyboard.press("d") # sleep(0.07) # keyboard.release("a") return True #永远不能删除!!! return True def main(): # 创建管理器 hm = PyHook3.HookManager() # 监听鼠标 hm.MouseAll = onMouseEvent hm.HookMouse() # 监听键盘 hm.KeyDown = onKeyboardEvent hm.KeyUp = onKeyboardEvent hm.HookKeyboard() # 循环监听 pythoncom.PumpMessages() if __name__ == "__main__": print("thi is main") gv = glv() gv.keyPressed = "" gv.count = 0 gv.lt = time() gv.start = 0 sleep(1) main()