在Matlab中启动.exe文件后,以编程方式按Enter键 [英] programmatically press an enter key after starting .exe file in Matlab

查看:207
本文介绍了在Matlab中启动.exe文件后,以编程方式按Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,我可以启动外部.exe文件,该文件有时会弹出,需要按Enter键.例如:

In Matlab I can start external .exe files that sometime have a pop up that requires an enter key pressed. For example:

system('C:\Program Files (x86)\WinZip\WINZIP32.EXE')

将启动Winzip,然后要使用它,则需要按Enter键传递立即购买"弹出窗口. 现在我的问题不在于winzip,我仅以它为例(无论如何我都使用winrar:).

will start Winzip, and then in order to use it you need to pass the "buy now" pop up window by pressing enter. Now my problem is not with winzip, I only gave it as an example (i use winrar anyway :).

在这种情况下,如何在Matlab中以编程方式按Enter键? (我使用赢7)

How can I programmatically press an enter key in Matlab in such cases ? (I use win 7)

可以使用事件监听器来解决此问题吗?

Can an event listener be used to solve that?

java.awt.Robot类确实适用于资源管理器,但不适用于具有弹出窗口且需要按下确定"按钮的任何软件.我不知道为什么它不起作用.我给出了winzip示例,因为我假设每个人的计算机上都安装了winzip/winrar.我所拥有的实际软件与该问题有所不同且无关紧要.

The java.awt.Robot class indeed works on explorer, but not on any software that has a pop up window with an OK button that needs to be pressed. I don't know why it doesn't work for that. I gave the winzip example because I assume everybody has winzip/winrar installed in their machine. The actual software I have is different and irrelevant for the question.

推荐答案

有一种方法可以使用Matlab中的Java,特别是java.awt.Robot类.请参见此处.

There is a way using Java from Matlab, specifically the java.awt.Robot class. See here.

关于使用system('...')从Matlab调用它们时的工作方式,显然有两种程序:

Apparently there are two types of programs, regarding the way they work when called from Matlab with system('...'):

  1. 对于某些程序,Matlab 等待直到程序完成,然后再运行下一条语句.例如,WinRAR(至少在我的Windows 7计算机中)会发生这种情况.

  1. For some programs, Matlab waits until the program has finished before running the next statement. This happens for example with WinRAR (at least in my Windows 7 machine).

对于其他程序,这不会发生,并且在外部程序启动后,Matlab继续执行下一条语句.这种类型的示例是explorer(标准Windows文件浏览器).

For other programs this doesn't happen, and Matlab proceeds with the next statement right after the external program has been started. An example of this type is explorer (the standard Windows file explorer).

现在,即使对于类型1程序,也可以立即将执行返回给Matlab:只需在传递给system的字符串的末尾添加&.这是 Linux Bash shell中的标准,并且它也可以在Windows中使用,如

Now, it is possible to return execution to Matlab immediately even for type 1 programs: just add & at the end of the string passed to system. This is standard in Linux Bash shell, and it also works in Windows, as discussed here.

因此,您将按照以下步骤操作:

So, you would proceed as follows:

robot = java.awt.Robot;
command = '"C:\Program Files (x86)\WinRAR\WinRAR"'; %// external program; full path
system([command ' &']); %// note: ' &' at the end
pause(5) %// allow some time for the external program to start
robot.keyPress (java.awt.event.KeyEvent.VK_ENTER); %// press "enter" key
robot.keyRelease (java.awt.event.KeyEvent.VK_ENTER); %// release "enter" key

这篇关于在Matlab中启动.exe文件后,以编程方式按Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆