bla*_*bla 25 matlab ui-automation
在Matlab中我可以启动外部.exe文件,有时会弹出一个需要按下回车键的文件.例如:
system('C:\Program Files (x86)\WinZip\WINZIP32.EXE')
Run Code Online (Sandbox Code Playgroud)
将启动Winzip,然后为了使用它,您需要通过按Enter键传递"立即购买"弹出窗口.现在我的问题不在于winzip,我只是把它作为一个例子(我还是使用winrar :).
在这种情况下,如何以编程方式在Matlab中按Enter键?(我用win 7)
可以使用事件监听器来解决这个问题吗?
编辑:java.awt.Robot类确实适用于资源管理器,但不适用于任何具有需要按下确定按钮的弹出窗口的软件.我不知道为什么它不起作用.我给了winzip的例子,因为我假设每个人都在他们的机器上安装了winzip/winrar.我拥有的实际软件与此问题不同且无关紧要.
Lui*_*ndo 27
有一种方法可以使用Matlab中的Java,特别是java.awt.Robot类.看到这里.
显然有两种类型的程序,关于它们在从Matlab调用时的工作方式system('...'):
对于某些程序,Matlab 会等到程序完成后再运行下一个语句.这种情况发生在例如WinRAR(至少在我的Windows 7机器上).
对于其他程序,这不会发生,Matlab 在外部程序启动后立即继续下一个语句.此类型的一个示例是explorer(标准Windows文件资源管理器).
现在,即使对于类型1程序,也可以立即将执行返回给Matlab:只需&在传递给的字符串末尾添加system.这是在Linux中的Bash shell的标准,并且它也可以在Windows,作为讨论在这里.
那么,你将按如下方式进行:
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
Run Code Online (Sandbox Code Playgroud)
Hok*_*oki 13
如果您的应用程序仅在Windows平台上,则可以尝试使用.net对象.
在SendWait所述的方法 SendKeys的对象允许发送几乎任何键,或键组合,向其中具有焦点,包括像"修饰语"键应用Alt,Shift,Ctrl等...
首先要做的是导入.net库,然后发送ENTER密钥的完整语法是:
NET.addAssembly('System.Windows.Forms');
System.Windows.Forms.SendKeys.SendWait('{ENTER}'); %// send the key "ENTER"
Run Code Online (Sandbox Code Playgroud)
如果只在完整语法确定后才这样做.如果您打算大量使用该命令,可以使用匿名帮助程序功能来帮助自己.
记事本的一个小例子
%% // import the .NET assembly and define helper function
NET.addAssembly('System.Windows.Forms');
sendkey = @(strkey) System.Windows.Forms.SendKeys.SendWait(strkey) ;
%% // prepare a few things to send to the notepad
str1 = 'Hello World' ;
str2 = 'OMG ... my notepad is alive' ;
file2save = [pwd '\SelfSaveTest.txt'] ;
if exist(file2save,'file')==2 ; delete(file2save) ; end %// this is just in case you run the test multiple times.
%% // go for it
%// write a few things, save the file then close it.
system('notepad &') ; %// Start notepad, without matlab waiting for the return value
sendkey(str1) %// send a full string to the notepad
sendkey('{ENTER}'); %// send the {ENTER} key
sendkey(str2) %// send another full string to the notepad
sendkey('{! 3}'); %// note how you can REPEAT a key send instruction
sendkey('%(FA)'); %// Send key combination to open the "save as..." dialog
pause(1) %// little pause to make sure your hard drive is ready before continuing
sendkey(file2save); %// Send the name (full path) of the file to save to the dialog
sendkey('{ENTER}'); %// validate
pause(3) %// just wait a bit so you can see you file is now saved (check the titlebar of the notepad)
sendkey('%(FX)'); %// Bye bye ... close the Notepad
Run Code Online (Sandbox Code Playgroud)
正如Microsoft文档中所解释的那样,SendKeys类有时会出现一些时序问题,因此如果您想进行复杂的操作(例如Tab多次更改实际想要按下的按钮),您可能需要pause在Matlab调用中引入一个SendKeys.
先尝试一下,但不要忘记你正在管理另一个进程而没有它们之间的任何同步,所以在你做对之前,所有这些都需要一些试验和错误,至少对于复杂的序列(简单的应该是直截了当).
在上面我的例子中,例如我从具有ECO功能的外部硬盘驱动器运行我的所有数据,这使得它进入待机状态,因此当我调用"另存为..."对话框时,它需要时间才能显示,因为硬盘必须醒来.如果我没有介绍pause(1),有时文件路径将是不完整的(路径的第一部分是在对话框有焦点之前发送的).
此外,&执行外部程序时不要忘记该字符.所有这些都归功于Luis Mendo的突出显示.(我倾向于忘记它的重要性,因为我默认使用它.如果我必须专门等待程序的返回值,我只会省略它,否则我让它自己运行)
特殊字符有一个特殊代码.以下是一些:
Shift +
Control (Ctrl) ^
Alt %
Tab {TAB}
Backspace {BACKSPACE}, {BS}, or {BKSP}
Validation {ENTER} or ~ (a tilde)
Ins Or Insert {INSERT} or {INS}
Delete {DELETE} or {DEL}
Text Navigation {HOME} {END} {PGDN} {PGUP}
Arrow Keys {UP} {RIGHT} {DOWN} {LEFT}
Escape {ESC}
Function Keys {F1} ... {F16}
Print Screen {PRTSC}
Break {BREAK}
Run Code Online (Sandbox Code Playgroud)
可以在此处找到Microsoft的完整列表
| 归档时间: |
|
| 查看次数: |
6729 次 |
| 最近记录: |