sending ctrl+c using sendkeys in ruby

旧时模样 提交于 2020-01-02 16:54: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.

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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/6908662/sending-ctrlc-using-sendkeys-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!