Search code examples
pythonpywin32

how to open a program in python and send keystrokes?


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?


Solution

  • 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)