views:

252

answers:

1

Is there an operating system neutral way for Ruby to send keyboard and mouse events to the underlying operating system?

One obvious (to me) approach is to use the Ruby/Java bindings and use java.awt.Robot, but that just seems silly.

+3  A: 

For Mac:

$ sudo gem install rb-appscript

Then you can test it with a script like this:

require "rubygems"
require "appscript"
include Appscript

app("TextEdit").activate
app("System Events").keystroke("Look Ma, keystrokes!")

For Windows: (untested, borrowed from this thread)

require "win32ole"

wsh = WIN32OLE.new("WScript.Shell")
wsh.Run("Notepad.exe")
while not wsh.AppActivate("Notepad")
  sleep .1
end
wsh.SendKeys("Look Ma, keystrokes!")
Ryan McGeary