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

查看:33
本文介绍了在 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)WinZipWINZIP32.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 键?(我用的是win 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.

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

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)WinRARWinRAR"'; %// 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天全站免登陆