没有急停,需要花费0.5秒
有急停,需要花费0.25秒;
是否存在模拟急停的可能性?
结论是,存在 这种可能性。但是现在的代码框架行不通,进不到中断里边,
中断里,应该只干标志位就好,别做执行单元!
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() ------------------- z这个版本的急停code好像可行
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": #当按键被按下去之后,主要开始记录方向 tmpKey = chr(event.Ascii) if tmpKey in ["a","d","w","s"] and tmpKey not in gv.keyDic.keys(): downTime = time() gv.keyDic[tmpKey] = {} gv.keyDic[tmpKey]["down"] = downTime print("{} down at {:.2f}".format(tmpKey, downTime)) gv.keyPressed = tmpKey return True elif event.MessageName=="key up": tmpKey = chr(event.Ascii) if tmpKey in ["a","d","w","s"] and tmpKey in gv.keyDic.keys(): upTime = time() gv.keyDic[tmpKey]["up"] = upTime delt = gv.keyDic[tmpKey]['up'] - gv.keyDic[tmpKey]['down'] gv.keyDic[tmpKey]['delt'] = delt print("{} up at {:.2f}".format(tmpKey, upTime)) print("duration of {} is {:.2f}".format(tmpKey, delt)) if tmpKey == 'a' and delt > 0.4: #取消监听: gv.hm.UnhookKeyboard() t11 = time() print("{:.2f}开始执行急停操作".format(t11)) keyboard.press("w") sleep(0.04) keyboard.release("w") keyboard.press("a") sleep(0.04) keyboard.release("a") keyboard.press("s") sleep(0.04) keyboard.release("s") keyboard.press("d") sleep(0.04) keyboard.release("d") t22 = time() print("{:.2f}结束急停操作,delt = {:.2f}".format(t22,t22-t11)) gv.hm.HookKeyboard() return True elif tmpKey == 'w': return True gv.keyDic.pop(tmpKey) 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") return True #永远不能删除!!! return True def main(): # 创建管理器 gv.hm = PyHook3.HookManager() # 监听鼠标 gv.hm.MouseAll = onMouseEvent gv.hm.HookMouse() # 监听键盘 gv.hm.KeyDown = onKeyboardEvent gv.hm.KeyUp = onKeyboardEvent gv.hm.HookKeyboard() # 循环监听 pythoncom.PumpMessages() if __name__ == "__main__": print("thi is main") gv = glv() gv.keyPressed = "" gv.count = 0 gv.keyDic = {} gv.lt = time() gv.start = 0 sleep(1) main() 测试结果👇:结论,没有实现急停
=====================
急停成功了,通过code
keyboard.press("a") keyboard.press("d") # sleep(0.01) keyboard.release("a") sleep(0.075) keyboard.release("d")
====================
急停成功,发布代码
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): if event.MessageName=="key down": #当按键被按下去之后,主要开始记录方向 tmpKey = chr(event.Ascii) if tmpKey in ["a","d",] and tmpKey not in gv.keyDic.keys(): downTime = time() gv.keyDic[tmpKey] = {} gv.keyDic[tmpKey]["down"] = downTime print("{} down at {:.2f}".format(tmpKey, downTime)) elif event.MessageName=="key up": tmpKey = chr(event.Ascii) if tmpKey in ["a","d"] and tmpKey in gv.keyDic.keys(): upTime = time() gv.keyDic[tmpKey]["up"] = upTime delt = gv.keyDic[tmpKey]['up'] - gv.keyDic[tmpKey]['down'] gv.keyDic[tmpKey]['delt'] = delt print("{} up at {:.2f}".format(tmpKey, upTime)) print("duration of {} is {:.2f}".format(tmpKey, delt)) #delt决定是否进行急停状态 if tmpKey == 'a' and delt > 0.5: #取消监听: gv.hm.UnhookKeyboard() #急停的按键序列(急停序列) keyboard.press("a") keyboard.press("d") keyboard.release("a") sleep(0.075) keyboard.release("d") #启动监听: gv.hm.HookKeyboard() elif tmpKey == 'd' and delt > 0.5: #取消监听: gv.hm.UnhookKeyboard() #急停的按键序列(急停序列) keyboard.press("d") keyboard.press("a") keyboard.release("d") sleep(0.075) keyboard.release("a") gv.hm.HookKeyboard() #只要按键弹起来之后,就需要恢复初始状态; gv.keyDic.pop(tmpKey) #永远不能删除!!! return True def main(): # 创建管理器 gv.hm = PyHook3.HookManager() # 监听鼠标 gv.hm.MouseAll = onMouseEvent gv.hm.HookMouse() # 监听键盘 gv.hm.KeyDown = onKeyboardEvent gv.hm.KeyUp = onKeyboardEvent gv.hm.HookKeyboard() # 循环监听 pythoncom.PumpMessages() if __name__ == "__main__": print("thi is main") gv = glv() gv.count = 0 gv.keyDic = {} gv.lt = time() gv.start = 0 sleep(1) main()