简体   繁体   中英

Pressing keys with python win32api

I am trying to achieve the following behavior in a PYTHON 2.4 script, here are the steps, and after them, the question:

  1. Python script starts
  2. The script gives a 3 seconds delay to change to 'Z' program's window
  3. The script does some clicks on the 'Z' program's window.
  4. The script stops making clicks
  5. /* ¿? */
  6. Ask to continue with the program's excecution
  7. /* ¿? */
  8. Go to step 2

So, in steps 5 and 7 what I want to do is simulate the pressing of keys Alt+Tab in order to go back to the script window (in step 5), and go back again to the 'Z' program's window (in step 7). And the problem is that I have no idea how to achieve this (the simulation to press keys alt+tab), and didn't find answers to my doubts. I am using the python win32api modules to positionate mouse in a certain point and make the clicks, but I don't find the way to simulate the key pressing.

Try This :

1) Use : https://gist.github.com/chriskiehl/2906125

2)

import win32api
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("app")
win32api.Sleep(100)
shell.AppActivate("myApp")
win32api.Sleep(100)
shell.SendKeys("name")
win32api.Sleep(500)
shell.SendKeys("{ENTER}")
win32api.Sleep(2500)
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text?  Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever

You need the WinApi function SendInput.

See the description in the MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx

An Easier Way

Use this Library W32S

My Library. And If u want , just copy the source instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM