how to open a program in python and send keystrokes?

雨燕双飞 提交于 2019-12-05 07:48:20

问题


I want to open a browser, click on some fields and then send keystrokes.

The following code clicks at any point on screen.

win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

Now i want to send keystrokes to that active application. How can i do that?


回答1:


I think that library from Google code will do the job: http://code.google.com/p/sendkeys-ctypes/

Check that link also for a sample code:

http://win32com.goermezer.de/content/view/136/254/

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("outlook")
shell.AppActivate("Outlook")
shell.SendKeys("^o", 0) # 1 für Pause = true 0 für nein
shell.SendKeys("^a", 0)
shell.SendKeys("^c", 0)


来源:https://stackoverflow.com/questions/19351758/how-to-open-a-program-in-python-and-send-keystrokes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!