开发者

sending ctrl+c using sendkeys in ruby

开发者 https://www.devze.com 2023-03-25 11:27 出处:网络
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.

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")
s开发者_开发问答leep(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?


Try this:

wsh.SendKeys("^C")

In the MSDN SendKeys Method specifies the following characters for this keys:

SHIFT: +

CTRL: ^

ALT: %

Examples:

wsh.SendKeys("+{TAB}") # SHIFT+TAB

wsh.SendKeys("^V")     # CTRL+V

wsh.SendKeys("%{F4}")  # ALT+F4


Key combinations can be sent by putting the keys into an array.

text_field.send_keys [ :shift, 'a']

puts a 'A' into the text_field. In your example,

wsh1.send_keys [ :control, 'c']

should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号