胖熊NET,有趣实用的生活常识!

最新更新文章排行手机版

胖熊NET!

热门搜索:
当前位置: > 教育

daemon 3.47

时间:2024-03-08 06:48:02人气:83作者:用户投稿

如果每天需要重复操作,怎样做到一键重复?

使用python2.7开发按键精灵

pyHook(安装地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook)

daemon 3.47

recode.py代码如下,记录鼠标键盘的操作

运行pythonrecode.py

#!/usr/bin/envpython\n#-*-coding:utf-8-*-\n\n\nimportpythoncom\nimportpyHook\nimporttime\nimportthreading\nimportdatetime\nimportsys\nimportwin32api\nimportwin32con\nimportwin32gui\nfromctypesimport*\nactions=[]\n_stoped=False\nclassPOINT(Structure):\n_fields_=[("x",c_ulong),("y",c_ulong)]\n\ndefget_mouse_point():\npo=POINT()\nwindll.user32.GetCursorPos(byref(po))\nreturnint(po.x),int(po.y)\ndeflogAction(e):\npo=get_mouse_point()\nseconds=getCurSec()\n\niflen(actions)>0ande.MessageName=='mousemove'andactions[-1][0]=='mousemove'andseconds-actions[-1][1]<0.05:\nactions[-1]=[e.MessageName,seconds,po[0],po[1]]\nelse:\nactions.append([e.MessageName,seconds,po[0],po[1]])\nstartLogTime=datetime.datetime.now()\ndefgetCurSec():\nreturn(datetime.datetime.now()-startLogTime).total_seconds()\ndeflog(s):\nfobj.writelines(s)\n\ndefonMouseEvent(event):\n"处理鼠标事件"\n#log("CurrentTime:%s\\n"%time.strftime("%a,%d%b%Y%H:%M:%S",time.gmtime()))\n#log("MessageName:%s\\n"%str(event.MessageName))\n#log("Position:%s\\n"%str(event.Position))\nglobal_stoped\n\nif_stoped:\nreturnTrue\n\nlogAction(event)\nreturnTrue\n\ndefonKeyboardEvent(event):\n"处理键盘事件"\n#log("CurrentTime:%s\\n"%time.strftime("%a,%d%b%Y%H:%M:%S",time.gmtime()))\n#log("MessageName:%s\\n"%str(event.MessageName))\n#log("Ascii_code:%d\\n"%event.Ascii)\n#log("Ascii_char:%s\\n"%chr(event.Ascii))\n#log("Key:%s\\n"%str(event.Key))\n#logAction(event)\nglobal_stoped\n\nifevent.KeyID==0x2C:\n_stoped=True\nif_stoped:\nreturnTrue\n\nactions.append([event.MessageName,getCurSec(),event.KeyID,event.Key])\nreturnTrue\n\ndefstart_log():\n#创建hook句柄\nhm=pyHook.HookManager()\n\n\n#监控键盘\nhm.KeyDown=onKeyboardEvent\nhm.KeyUp=onKeyboardEvent\nhm.HookKeyboard()\n\n\n#监控鼠标\nhm.MouseAll=onMouseEvent\nhm.HookMouse()\n\n#循环获取消息\npythoncom.PumpMessages()\n\nif__name__=="__main__":\nuser32=windll.user32\nhwnd=user32.GetForegroundWindow()\nwin32gui.CloseWindow(hwnd)#窗口最小化\nt4=threading.Thread(target=start_log,args=())\nt4.setDaemon(True)\nt4.start()\nprint'begintorecord'\n\nwhilenot_stoped:\ntime.sleep(1)\nprint'endtorecord'\n\n#打开日志文件\nfile_name="hook_log.txt"\niflen(sys.argv)>1:\nfile_name=sys.argv[1]\n\nfobj=open(file_name,'w')\ncontent='\\n'.join(str(','.join(str(s1)fors1ins))forsinactions)\nfobj.write(content)\n#关闭日志文件\nfobj.close()\n

运行之后记录操作,当按下PrtSc键后停止记录,得到log文件内容如下

mousemove,2.198,479,305\nmouseleftdown,2.434,480,305\nmouseleftup,2.546,480,305\nkeydown,3.238,84,T\nkeyup,3.374,84,T\nkeydown,3.382,72,H\nkeydown,3.47,73,I\nkeyup,3.542,72,H\nkeyup,3.622,73,I\nkeydown,3.686,83,S\nkeyup,3.798,83,S\nkeydown,4.19,32,Space\nkeyup,4.318,32,Space\nkeydown,4.366,73,I\nkeydown,4.454,83,S\nkeyup,4.462,73,I\nkeyup,4.582,83,S\nkeydown,4.67,32,Space\nkeyup,4.774,32,Space\nkeydown,4.982,84,T\nkeydown,5.062,69,E\nkeyup,5.15,84,T\nkeydown,5.166,83,S\nkeydown,5.27,84,T\nkeyup,5.31,69,E\nkeyup,5.318,83,S\nkeyup,5.398,84,T\nmousemove,6.086,481,306\n

记录播放

pythonplay.py

play.py代码如下

#\n#_*_coding:UTF-8_*_\n\nimportwin32api\nimportwin32con\nimportwin32gui\nfromctypesimport*\nimporttime\nimportsys,ctypes\nifsys.getdefaultencoding()!='utf-8':\nreload(sys)\nsys.setdefaultencoding('utf-8')\n\nVK_CODE={\n'backspace':0x08,\n'tab':0x09,\n'clear':0x0C,\n'enter':0x0D,\n'shift':0x10,\n'ctrl':0x11,\n'alt':0x12,\n'pause':0x13,\n'caps_lock':0x14,\n'esc':0x1B,\n'spacebar':0x20,\n'':0x20,\n'page_up':0x21,\n'page_down':0x22,\n'end':0x23,\n'home':0x24,\n'left_arrow':0x25,\n'up_arrow':0x26,\n'right_arrow':0x27,\n'down_arrow':0x28,\n'select':0x29,\n'print':0x2A,\n'execute':0x2B,\n'print_screen':0x2C,\n'ins':0x2D,\n'del':0x2E,\n'help':0x2F,\n'0':0x30,\n'1':0x31,\n'2':0x32,\n'3':0x33,\n'4':0x34,\n'5':0x35,\n'6':0x36,\n'7':0x37,\n'8':0x38,\n'9':0x39,\n'a':0x41,\n'b':0x42,\n'c':0x43,\n'd':0x44,\n'e':0x45,\n'f':0x46,\n'g':0x47,\n'h':0x48,\n'i':0x49,\n'j':0x4A,\n'k':0x4B,\n'l':0x4C,\n'm':0x4D,\n'n':0x4E,\n'o':0x4F,\n'p':0x50,\n'q':0x51,\n'r':0x52,\n's':0x53,\n't':0x54,\n'u':0x55,\n'v':0x56,\n'w':0x57,\n'x':0x58,\n'y':0x59,\n'z':0x5A,\n'numpad_0':0x60,\n'numpad_1':0x61,\n'numpad_2':0x62,\n'numpad_3':0x63,\n'numpad_4':0x64,\n'numpad_5':0x65,\n'numpad_6':0x66,\n'numpad_7':0x67,\n'numpad_8':0x68,\n'numpad_9':0x69,\n'multiply_key':0x6A,\n'add_key':0x6B,\n'separator_key':0x6C,\n'subtract_key':0x6D,\n'decimal_key':0x6E,\n'divide_key':0x6F,\n'F1':0x70,\n'F2':0x71,\n'F3':0x72,\n'F4':0x73,\n'F5':0x74,\n'F6':0x75,\n'F7':0x76,\n'F8':0x77,\n'F9':0x78,\n'F10':0x79,\n'F11':0x7A,\n'F12':0x7B,\n'F13':0x7C,\n'F14':0x7D,\n'F15':0x7E,\n'F16':0x7F,\n'F17':0x80,\n'F18':0x81,\n'F19':0x82,\n'F20':0x83,\n'F21':0x84,\n'F22':0x85,\n'F23':0x86,\n'F24':0x87,\n'num_lock':0x90,\n'scroll_lock':0x91,\n'left_shift':0xA0,\n'right_shift':0xA1,\n'left_control':0xA2,\n'right_control':0xA3,\n'left_menu':0xA4,\n'right_menu':0xA5,\n'browser_back':0xA6,\n'browser_forward':0xA7,\n'browser_refresh':0xA8,\n'browser_stop':0xA9,\n'browser_search':0xAA,\n'browser_favorites':0xAB,\n'browser_start_and_home':0xAC,\n'volume_mute':0xAD,\n'volume_Down':0xAE,\n'volume_up':0xAF,\n'next_track':0xB0,\n'previous_track':0xB1,\n'stop_media':0xB2,\n'play/pause_media':0xB3,\n'start_mail':0xB4,\n'select_media':0xB5,\n'start_application_1':0xB6,\n'start_application_2':0xB7,\n'attn_key':0xF6,\n'crsel_key':0xF7,\n'exsel_key':0xF8,\n'play_key':0xFA,\n'zoom_key':0xFB,\n'clear_key':0xFE,\n'+':0xBB,\n',':0xBC,\n'-':0xBD,\n'.':0xBE,\n'/':0xBF,\n'`':0xC0,\n';':0xBA,\n'[':0xDB,\n'\\\\':0xDC,\n']':0xDD,\n"'":0xDE,\n'`':0xC0}\n\nclassPOINT(Structure):\n_fields_=[("x",c_ulong),("y",c_ulong)]\n\ndefget_mouse_point():\npo=POINT()\nwindll.user32.GetCursorPos(byref(po))\nreturnint(po.x),int(po.y)\n\ndefmouse_click(x=None,y=None):\nifnotxisNoneandnotyisNone:\nmouse_move(x,y)\ntime.sleep(0.05)\nwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)\ndefmouse_dclick(x=None,y=None):\nifnotxisNoneandnotyisNone:\nmouse_move(x,y)\ntime.sleep(0.05)\nwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)\nwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)\n\ndefmouse_move(x,y):\nwindll.user32.SetCursorPos(x,y)\ndefmouse_leftdown(x=None,y=None):\nifnotxisNoneandnotyisNone:\nmouse_move(x,y)\nwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)\ndefmouse_leftup(x=None,y=None):\nifnotxisNoneandnotyisNone:\nmouse_move(x,y)\nwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)\ndefmouse_rightdown(x=None,y=None):\nifnotxisNoneandnotyisNone:\nmouse_move(x,y)\nwin32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0,0,0)\ndefmouse_rightup(x=None,y=None):\nifnotxisNoneandnotyisNone:\nmouse_move(x,y)\nwin32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0,0,0)\ndefkey_down(vc):\nwin32api.keybd_event(vc,0,0,0)\ndefkey_up(vc):\nwin32api.keybd_event(vc,0,win32con.KEYEVENTF_KEYUP,0)\n\ndefkey_input(str=''):\nforcinstr:\nwin32api.keybd_event(VK_CODE[c],0,0,0)\nwin32api.keybd_event(VK_CODE[c],0,win32con.KEYEVENTF_KEYUP,0)\ntime.sleep(0.01)\ndefsetClipboard(mystr):\n'''把字符串放到剪切板中,成功返回1,失败返回0'''\nu=ctypes.WinDLL('user32.dll')\nk=ctypes.WinDLL('kernel32.dll')\ns=mystr.encode('utf-16')\ns=s[2:]+b'\\0\\0'\nss=ctypes.c_char_p(s)\nu.OpenClipboard(0)\nu.EmptyClipboard()\nk.GlobalAlloc.argtypes=[ctypes.c_uint32,ctypes.c_uint32]\ntry:\ncb=k.GlobalAlloc(0,len(s))\ncb=ctypes.c_void_p(cb)\n\nctypes.memmove(cb,ss,len(s))\nrr=u.SetClipboardData(13,cb)#13->unicode\nfinally:\nu.CloseClipboard()\nifrr==0:\nreturn0\nelse:\nreturn1\n\ndefkey_input2(s):\nsetClipboard(s)\n#ctrl+v\nkey_down(0x11)\nkey_down(0x56)\nkey_up(0x56)\nkey_up(0x11)\ndefrunActrion(actions):\nalen=len(actions)\nforiinrange(alen):\naction=actions[i]\nt=action[0]\nift=='mouseleftdown':\nmouse_leftdown(int(action[2]),int(action[3]))\nelift=='mouseleftup':\nmouse_leftup(int(action[2]),int(action[3]))\nelift=='mouserightdown':\nmouse_rightdown(int(action[2]),int(action[3]))\nelift=='mouserightup':\nmouse_rightup(int(action[2]),int(action[3]))\nelift=='keyup':\nkey_up(int(action[2]))\nelift=='keydown'ort=='keysysdown':\nkey_down(int(action[2]))\nelift=='keyinput':\nkey_input2(action[2])\nelift=='mousemove':\nmouse_move(int(action[2]),int(action[3]))\nifi<alen-1:\ntime.sleep(float(actions[i+1][1])-float(action[1]))\nif__name__=="__main__":\nuser32=windll.user32\nhwnd=user32.GetForegroundWindow()\nwin32gui.CloseWindow(hwnd)#窗口最小化\n\nfname='hook_log.txt'\niflen(sys.argv)>1:\nfname=sys.argv[1]\n\nactions=[]\nforlineinopen(fname):\niflen(line.strip())>0:\nactions.append(line.split(','))\ntimes=1\niflen(sys.argv)>2:\ntimes=int(sys.argv[2])\nforiinrange(times):\nrunActrion(actions)\n\n

更多功能可以自己去修改代码

可以录制和播放不同脚本,如登录游戏,登录邮箱等等.本人用c#做了个界面,可以显示记录的不同脚本,并可以设置播放次数等功能

pythonrecode.pygame.txt

pythonplay.pygame.txt

标签:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123@。cc举报,一经查实,本站将立刻删除。