1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > RF自动化--python模拟按键精灵

RF自动化--python模拟按键精灵

时间:2020-04-22 18:58:26

相关推荐

RF自动化--python模拟按键精灵

#Python下在windows系统下仿照按键精灵

功能:找到对应软件程序,并对界面进行操作,主要进行点击、信息输入、输出信息得检测

##所用到得库

##封装所要用到得函数(自定义)

下面数字键和对应键码

#各个函数代码及解说

titles = set()process_window_title=U"软件窗口标题"translate_dict={}def create_translate_table():"""生成键盘上数字和对应的键码:键值:键码"""for i in range(48,58):translate_dict[chr(i)]=itranslate_dict["\."]=110def find_process_window():"""根据窗口进行查process界面,并返回句柄以及左上角坐标left up;右下角坐标right,bottom"""win = win32gui.FindWindow(None, process_window_title)win32gui.ShowWindow(win, win32con.SW_SHOWNORMAL)#将窗口显示到屏幕time.sleep(3)# 获取窗口左上角和右下角坐标(left, up, right, bottom) = win32gui.GetWindowRect(win)return win,left, up,right, bottomdef foo(hwnd,mouse):"""查找打开的所有窗口"""if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):titles.add(GetWindowText(hwnd))def judge_process_run_state():"""判断是否已经打开软件窗口,打开返回True,未打开返回False"""EnumWindows(foo, 0)lt = [t.decode("GBK") for t in titles if t]lt.sort()if process_window_title in lt:return Trueelse:return Falsedef fix_window(win,left,up):"""确保软件窗口总显示在最前"""hWndList = []titlelist=[]win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)#获取所有窗口句柄for hwd in hWndList:title=win32gui.GetWindowText(hwd)#返回各个窗口的标题if title == '':continueelse:titlelist.append(title)#将非空标题的窗口标题组成一个列表if (titlelist.index(process_window_title.decode("utf-8").encode("GBK")) >= 2): #若是process标题索引不为0,表明process窗口不在最前面win32gui.SetWindowPos(win, win32con.HWND_TOPMOST, left, up, 1300, 760, win32con.SWP_SHOWWINDOW)# 将应用窗口显示到最前面def mouse_event(left,up,x,y):"""定义鼠标事件"""int_x=int(x)int_y=int(y)win32api.SetCursorPos((left + int_x, up + int_y))win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)time.sleep(2)def clear_input(times):"""通过backspace删除输入框的内容,且需要指定删除次数"""for i in range(times):win32api.keybd_event(8, 0, 0, 0)win32api.keybd_event(8, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(0.1)def input_text(input_num):"""进行界面数据输入"""create_translate_table()for i in str(input_num):if i == ".":ma=110else:ma=translate_dict[i]win32api.keybd_event(ma,0,0,0)win32api.keybd_event(ma,0,win32con.KEYEVENTF_KEYUP,0)time.sleep(0.1)def select_all():"""实现crtl+a,注意keyup键盘的顺序,不然会概率出现失效"""win32api.keybd_event(17,0,0,0)win32api.keybd_event(65,0,0,0)win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(0.1)def copy_all():"""实现crtl+c,注意keyup键盘的顺序,不然会概率出现失效"""w.OpenClipboard()w.EmptyClipboard()w.CloseClipboard()win32api.keybd_event(17,0,0,0)win32api.keybd_event(67,0,0,0)win32api.keybd_event(67, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(0.1)def pasue_all():"""实现crtl+v,注意keyup键盘的顺序,不然会概率出现失效"""win32api.keybd_event(17,0,0,0)win32api.keybd_event(86,0,0,0)win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)time.sleep(0.1)def get_clipboard_text():"""获取windows下粘贴板的内容--PS需要选择unicodetext格式,不然会出现乱码"""w.OpenClipboard()t=w.GetClipboardData(win32con.CF_UNICODETEXT) #一定要选unicode数据类型,不然会出现乱码w.CloseClipboard()return tdef click_process(left,up,x,y):"""点击process界面"""int_x=int(x)int_y=int(y)mouse_event(left,up,int_x,int_y)def input_text_for_process(left,up,x,y,input_text):"""对process输入框,先清除然后再进行数据输入"""int_x=int(x)int_y=int(y)mouse_event(left,up,int_x,int_y)clear_input(12)input_text(input_text)

总结

大家根据各自所需,对函数进行自由搭配从而实现自己所需要的功能

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。