在Ruby中使用sendkey发送ctrl + c [英] sending ctrl+c using sendkeys in ruby
问题描述
我需要使用sendkeys函数关闭命令提示符窗口,但是当我使用下面的代码时,由于某些抓取文件的运行正在进行,因此它不起作用,因此未采用以下这些选项.
I need to close the command prompt window using sendkeys function, but when I used the below code it did not work as running of some betch file is in progress so its not taking these below options.
require 'win32ole'
system("start cmd.exe")
sleep(5)
# Create an instance of the Wscript Shell:
wsh = WIN32OLE.new('Wscript.Shell')
# Try to activate the command window:
if wsh.AppActivate('cmd.exe')
sleep(1)
wsh.SendKeys('cd \\')
wsh.SendKeys('{ENTER}')
# change the directory path where mtn folder is residing
wsh.SendKeys('cd ')
wsh.SendKeys "C://mtn-3//mtn-2.2//"
wsh.SendKeys('{ENTER}')
wsh.SendKeys('cd bin')
wsh.SendKeys('{ENTER}')
#run the cad test node file
wsh.SendKeys('CadTestNode.bat')
wsh.SendKeys('{ENTER}')
wsh1.SendKeys('Exit')
wsh1.SendKeys('{ENTER}')
我还尝试用以下内容替换最后两行以终止该过程.
I also tried replacing last two lines with the below to terminate the process.
wsh.SendKeys "^(c)"
wsh.SendKeys('{ENTER}')
但是仍然无法终止在命令提示符下运行的进程.
but still it's not able to terminate the process running in command prompt.
还有其他方法可以终止在命令提示符窗口中运行的批处理吗?
Is there any other way to terminate the batch process running in command prompt window?
推荐答案
尝试一下:
wsh.SendKeys("^C")
在 MSDN SendKeys方法中为此指定了以下字符按键:
In the MSDN SendKeys Method specifies the following characters for this keys:
SHIFT:+
SHIFT: +
CTRL:^
ALT:%
示例:
wsh.SendKeys("+{TAB}") # SHIFT+TAB
wsh.SendKeys("^V") # CTRL+V
wsh.SendKeys("%{F4}") # ALT+F4
这篇关于在Ruby中使用sendkey发送ctrl + c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!