简体   繁体   English

Python:将F3,F10输入Windows中的exe文件

[英]Python: Giving F3,F10 as input to an exe file in windows

I need to give input as F3 (0x72) to an exe file in windows by using python. 我需要使用python将输入作为F3(0x72)输入到Windows中的exe文件。 I use Popen command to issue commands in it. 我使用Popen命令在其中发出命令。 Can anyone guide me? 谁能指导我?

You can find the details of the program here: How do I get all of the output from my .exe using subprocess and Popen? 您可以在此处找到程序的详细信息: 如何使用子进程和Popen从.exe中获取所有输出?

The answer depends on whether the "exe file" is a Win32 GUI application, or a console application. 答案取决于“ exe文件”是Win32 GUI应用程序还是控制台应用程序。

If it's a GUI app, you don't just need to "type in F3 and F10 to the exe file while it's running", you need to type them to the application's main window while it's active. 如果它是GUI应用程序,则不仅需要“在运行时在F3和F10中键入exe文件”,还需要在它们处于活动状态时在应用程序的主窗口中键入它们。 If it's a console app, you need to know which of the various different ways it's reading the keyboard and do the appropriate thing—or you can just type them to the console window while it's active. 如果它是一个控制台应用程序,则需要了解它在读取键盘的各种不同方式中的哪一种并执行适当的操作-或者可以在它们处于活动状态时在控制台窗口中键入它们。

If you can just assume that the appropriate window is active, look at SendKeys , a low-level module that's designed to do nothing but sending keystroke combinations to whatever window is in the foreground: 如果您可以仅假设适当的窗口处于活动状态,请查看SendKeys ,这是一个底层模块,旨在将击键组合发送到前台的任何窗口中,什么也不做:

SendKeys('{F3}')

If you need more control—eg, if you need to work even if the app isn't in the foreground—there are a half dozen or so "window automation libraries" for Python. 如果您需要更多控制权(例如,即使应用程序不在前台也需要工作),则可以使用大约六种针对Python的“窗口自动化库”。 None of them are quite as nice as something like AutoIt, but if you need to use Python, they're not bad. 它们都不比AutoIt更好,但是如果您需要使用Python,它们也不错。 For example, with pywinauto , the pseudocode for what you want to do is something like this: 例如,使用pywinauto ,您要执行的伪代码如下所示:

app = application.find("foo.exe")
wnd = app.TheAppWindow # assuming the app's main window has a title similar to "The App Window"
wnd.TypeKeys('{F3}')

Of course you don't get much benefit out of a library like this unless you need to interact with the GUI—open the menu item named "Edit | Find", type "foo" into the first edit box in the "Find" window, click the button labeled "Next", get the selected text in the main window, etc. 当然,除非您需要与GUI交互,否则从这样的库中不会获得太多好处-打开名为“编辑|查找”的菜单项,在“查找”窗口的第一个编辑框中键入“ foo” ,单击标记为“下一步”的按钮,在主窗口中获取所选文本,等等。

Finally, you can always drop down to the low level and make Win32API calls via PyWin32 . 最后,您始终可以降至较低级别,并通过PyWin32进行Win32API调用。 The pseudocode is: 伪代码为:

hwnd = win32gui.FindWindowEx(0, 0, 'TheAppClass', 'The App Window')
win32gui.PostMessage(hwnd, WM_KEYDOWN, VK_F3, 0)
win32gui.PostMessage(hwnd, WM_KEYUP, VK_F3, 0)

Not really much more complicated, except that now you need the exact window name and the window class. 除了现在您需要确切的窗口名称和窗口类之外,实际上并没有复杂得多。 (To find that, use any of the free "window explorer" tools out there; I think Visual C++ Express still comes with one.) (要发现这一点,请使用其中的任何免费“窗口浏览器”工具;我认为Visual C ++ Express仍然附带一个。)

The advantage here is that you know exactly what's going on, which can make problems easier to debug—and which also means you can take advantage of the detailed documentation at MSDN (although PyWin32 itself comes with some pretty decent docs, so you often don't have to). 这样做的好处是您可以确切地知道发生了什么,这可以使问题更易于调试,并且还意味着您可以利用MSDN上的详细文档(尽管PyWin32本身附带了一些相当不错的文档,所以您通常不这样做)不必)。

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

相关问题 如何在图中切换到f3(Selenium + Python) - How to switch to f3 in the figure (Selenium + Python) 使用 VS Code 按行 (f10) 运行的 jupyter notebook 出现问题 - Trouble in jupyter notebook running by line (f10) with VS Code 我的列表 (f3) 没有打印输出并一直给我 StaleElementReferenceException? - My list (f3) is not printing an output and keeps giving me StaleElementReferenceException? 如何在python3中制作一个可以记录链接(组合)按键的键盘记录器,例如ctrl + x,shift + f10,c + h - How to make a keylogger which can log chained(combined) key presses like ctrl+x , shift + f10, c+h in python3 如何从函数列表 [f1, f2, f3,...fn] 组成嵌套的 function g=fn(...(f3(f2(f1()))...) - How to compose a nested function g=fn(…(f3(f2(f1()))…) from a list of functions [f1, f2, f3,…fn] 带有-F选项并使用变量作为输入文件的python子进程awk - python subprocess awk with -F option and using variable for input file 无法在 Windows 10 中打开 Python Selenium exe 文件 - Unable to Open an Python Selenium exe file in Windows 10 os.system('TASKKILL /F /IM EXCEL.exe') 在 python - os.system('TASKKILL /F /IM EXCEL.exe') in python Windows 10安装f90wrap(64位) - Installation of f90wrap on Windows 10 (64) 在 Windows 上使用 Python 运行可执行文件并提供文本文件作为输入 - Run an executable and giving text file as input using Python on Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM