4

IS there a way in ruby I can inject a keystroke in shell to have the program go? I need to run a program in shell through

sh " #{another program}"

And there is a "Press any key to continue" at the end of the program. How can I make it move on ?

Is there something similar in ruby like java

http://alvinalexander.com/java/java-robot-class-example-mouse-keystroke

icn
  • 17,126
  • 39
  • 105
  • 141
  • Look for an 'expect' equivalent. See http://stackoverflow.com/questions/7142978/is-there-an-expect-equivalent-gem-for-ruby – Mark Thomas Oct 30 '12 at 20:31

2 Answers2

1

I think your best bet is with Autoit, it can be controlled through the COM interface like this

require 'win32ole'
ai = WIN32OLE.new("AutoItX3.Control")
ai.WinWaitActive("Untitled - Notepad")
1.upto(10) do |i|
  ai.Send "#{i}{ENTER}"
end
peter
  • 41,770
  • 5
  • 64
  • 108
  • ERROR: Could not find a valid gem 'WIN32OLE' (>= 0) in any repository ERROR: Possible alternatives: win32-file, win32ole-pp, win32olerot, win32rc, windoze – icn Oct 31 '12 at 17:07
  • use it on multiple vista an win7 boxes, no problem, win32ole is in the standard libray, no gem required, autoix needs to be installed so that the COM object is avaiable – peter Nov 02 '12 at 17:37
0

You can use Open3.popen2 to start the process and get handles to stdout and stdin, and then you should be able to "press a key" by doing stdin.puts "Y".

Dan Fitch
  • 2,480
  • 2
  • 23
  • 39
  • Thanks. I got this error Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read. – icn Oct 30 '12 at 20:23
  • Okay, now you're confusing me a bit. Are you running inside IronRuby or JRuby or something? `System.InvalidOperationException` sounds like a .NET thing. The ruby implementation you're using is very important, because I doubt the Open3 library is going to run correctly in IronRuby. – Dan Fitch Oct 30 '12 at 21:07
  • It is the exception of the .Net programming I was running, not ruby itself. – icn Oct 30 '12 at 21:21
  • 1
    Apparently the .NET program doesn't like redirected console input and is expecting actual keypresses; my program would only work if the spawned code was using Console.Read. I'm not sure how to support this scenario from Ruby! – Dan Fitch Oct 30 '12 at 21:24