简体   繁体   English

使用 python win32api 按键

[英]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:我试图在 PYTHON 2.4 脚本中实现以下行为,这里是步骤,然后是问题:

  1. Python script starts Python 脚本启动
  2. The script gives a 3 seconds delay to change to 'Z' program's window该脚本有 3 秒的延迟以更改为“Z”程序的窗口
  3. The script does some clicks on the 'Z' program's window.该脚本在“Z”程序的窗口上进行了一些点击。
  4. The script stops making clicks脚本停止点击
  5. /* ¿? /* ? */ */
  6. Ask to continue with the program's excecution要求继续执行程序
  7. /* ¿? /* ? */ */
  8. Go to step 2转到第 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).因此,在第 5 步和第 7 步中,我想要做的是模拟按 Alt+Tab 键以返回脚本窗口(在第 5 步中),然后再次返回到“Z”程序的窗口(在第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.问题是我不知道如何实现这一点(模拟按 alt+tab 键),并且没有找到我疑惑的答案。 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.我正在使用 python win32api 模块将鼠标定位在某个点并进行点击,但我没有找到模拟按键的方法。

Try This :试试这个:

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

2) 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.您需要 WinApi 函数 SendInput。

See the description in the MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx请参阅 MSDN 中的说明: http : //msdn.microsoft.com/en-us/library/windows/desktop/ms646310( v= vs.85).aspx

An Easier Way更简单的方法

Use this Library W32S使用这个库W32S

My Library.我的图书馆。 And If u want , just copy the source instead如果你愿意,只需复制源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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