1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Simulating Keystrokes with JRuby

Window Automation the Ruby Way


While most web applications simply serve data from a server, web applications can also be useful in a desktop environment. For example, when running a fullscreen application, or when controlling your computer away from keyboard, you can use a web application running on the desktop computer to provide an interface for performing various tasks. Also, when combined with simple web frameworks like Sinatra, this can also be very quick to set up.

For this project, jRuby will be used. As this is intended to be run on Windows, jRuby is often the best choice for running on Windows. In addition, it allows you to easily make a jar file with your server, all its code and data as well as all of JRuby that anyone with Java can run.

The following articles will walk you through the process and explore one aspect of the application.

  • Automating with Robot - Robot is a class provided by AWT (the base Java windowing toolkit) use for automating native input. This is not for automating the windowing toolkit itself, but rather sending "raw" input through the operating system to be caught by whatever application has focus. Since this is not likely to be your web application server, your web application server can send keystrokes to the application that has focus.

  • Using FFI to Access Win32 - JRuby comes equipped with FFI, the foreign function interface. This allows you to load native code and call it, allowing you to reach out to any Win32 API calls you wish. Using FFI and the user32 dll file, we'll be able to send keystrokes just as we did with Robot.

  • Sending Win32 Keyboard Messages with FFI - Now that we know how to get to Win32 via FFI on JRuby, we'll use this to send a few keyboard messages. The advantage of this is that you can send keyboard messages to any window, not just the window with focus, and since we're dealing with the Win32 API itself, it's very flexible compared to using Robot.

  • Sinatra Web Applications in JRuby - This is perhaps the easiest part of the whole project. Sinatra is so easy to work with this article focuses less on Sinatra and more on putting the whole application together. We'll look at spiffing it up with some CSS and simple graphics, as well as adding a GUI element that allows the user to click a button and start or stop the server.

  • AJAX With Sinatra - AJAX is a powerful technique that allows you to make HTTP requests to your web application without actually refreshing the entire page. This technique can make your frontends seem much more "application" like.

  • Finishing the Project - In this article, we literally just staple the work from the previous articles together.

  • Deploying with JAR Files - While it's more traditional to deploy web applications with .war files, since we're targeting desktop users and not a server environment, this article will focus on deploying using .jar files. The .jar file will not only hold the code and data for the web application, but also all of JRuby as well. End users will only need to have the Java runtime environment in order to run the web application server.

In addition, you may also find the following articles useful.

  • Why JRuby? - Why are we using JRuby here instead of good old MRI? Why use JRuby at all?

  • Installing and Using JRuby - If you don't yet have JRuby installed, or perhaps if you've never really used it, this article should help you.

  • What is Sinatra? - Not familiar with Sinatra? This is the article for you, become acquainted with Rails' smaller, faster, simpler, easier to use cousin. While Rails is more than capable for most any project, it's a bit too complex for tasks such as this.

  1. About.com
  2. Technology
  3. Ruby
  4. JRuby
  5. Automation of Windows using FFI
  6. Simulating Keystrokes with JRuby

©2014 About.com. All rights reserved.