matlab写错了按了enter,在Matlab中启动.exe文件后,以编程方式按Enter键

如果您的应用程序只在Windows平台上,您可以尝试使用.net对象。

SendKeys对象的SendWait方法允许发送几乎任何按键或组合键到具有焦点的应用程序,包括诸如Alt , Shift , Ctrl等的“修饰键”。

要做的第一件事是导入.net库,那么发送ENTER键的完整语法是:

NET.addAssembly('System.Windows.Forms'); System.Windows.Forms.SendKeys.SendWait('{ENTER}'); %// send the key "ENTER"

如果只有完整的语法是可以的, 如果您打算广泛使用该命令,则可以使用匿名帮助函数来帮助自己。

记事本的一个小例子

%% // 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

正如Microsoft文档中所解释的那样, SendKeys类有时可能会有一些计时问题,所以如果您想要进行复杂的操作(比如多次selectTab来更改实际想要按下的button),则可能需要在Matlab调用中引入一个pause SendKeys 。

尽量不要尝试,但是不要忘记你正在pipe理另一个进程而没有进行任何同步,因此在你做出正确的决定之前对所有可能需要一些试验和错误的进行计时,至less对于复杂的序列(简单的应该是直截了当)。

在我上面的例子中,我正在运行一个ECO函数的外部硬盘驱动器的所有数据,它将其置于待机状态,所以当我调用“另存为…”对话框时,需要花费时间来显示,因为硬盘必须唤醒。 如果我没有引入pause(1) ,有时文件path将不完整(path的第一部分在对话框焦点之前发送) 。

此外,执行外部程序时不要忘记&字符。 所有信贷路易斯门多突出显示它。 (我倾向于忘记它是多么重要,因为我默认使用它,如果我必须特别等待来自程序的返回值,我才会忽略它,否则我让它自己运行 )

特殊字符有一个特殊的代码。 这里有几个:

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}

微软的完整列表可以在这里find

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值