# How to simulate Mouse Click with Ruby, or mouse library

**URL:** <https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297>\
**Category:** Ruby\
**Created:** [January 24, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297 "2007-01-24T23:12:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [January 24, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/1 "2007-01-24T23:12:12Z")

</div>

Hi, I would like to simulate a Mouse click through Ruby. Is there a way  
to do this through Ruby? Does Ruby have any library for simulating  
mouse events. Can someone provide me with some info on this topic.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [January 25, 2007, 12:00am UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/2 "2007-01-25T00:00:01Z")

</div>

On 24 Jan 2007, at 22:12, Kid Kid wrote:

> Hi, I would like to simulate a Mouse click through Ruby. Is there  
> a way  
> to do this through Ruby? Does Ruby have any library for simulating  
> mouse events. Can someone provide me with some info on this topic.  
> Thanks in advance.

I think you’ll need to provide a little more info to help people get  
started with this one. What GUI environment are you using and trying  
to make mouse clicks in?

Cheers,  
Benj

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [January 25, 2007, 12:09am UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/3 "2007-01-25T00:09:06Z")

</div>

Thanks for the suggestion Ben, I would like to use ruby to simulate a  
mouse click on any general Windows program such as Internet Explorer.  
I’m aware that I can use WATIR to automate clicking on elements on the  
webpages. However what i’m looking for is the ability to use Ruby to  
issue a “Mouse click” no matter where the cursor is or what program is  
running.

Thanks,  
KidK

Benjohn B. wrote:

> On 24 Jan 2007, at 22:12, Kid Kid wrote:
> 
> > Hi, I would like to simulate a Mouse click through Ruby. Is there  
> > a way  
> > to do this through Ruby? Does Ruby have any library for simulating  
> > mouse events. Can someone provide me with some info on this topic.  
> > Thanks in advance.
> 
> I think you’ll need to provide a little more info to help people get  
> started with this one. What GUI environment are you using and trying  
> to make mouse clicks in?
> 
> Cheers,  
> Benj

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [January 25, 2007, 12:41am UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/4 "2007-01-25T00:41:39Z")

</div>

Thanks for the help Marcelo. Using AutoIt through Ruby does exactly what  
I needed. Thanks!

Marcelo A. wrote:

> Well, AutoIt is a fine automation program for windows, although it  
> uses it’s own language. But the latest version also provides a DLL to  
> do this programmatically, so you should be able to interface Ruby with  
> the DLL and be all set.
> 
> Marcelo.

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [January 25, 2007, 12:17am UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/5 "2007-01-25T00:17:03Z")

</div>

Well, AutoIt is a fine automation program for windows, although it  
uses it’s own language. But the latest version also provides a DLL to  
do this programmatically, so you should be able to interface Ruby with  
the DLL and be all set.

Marcelo.

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [January 25, 2007, 12:48am UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/6 "2007-01-25T00:48:07Z")

</div>

On 1/24/07, Marcelo A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, AutoIt is a fine automation program for windows, although it  
> uses it’s own language. But the latest version also provides a DLL to  
> do this programmatically, so you should be able to interface Ruby with  
> the DLL and be all set.

In fact, if you’ve installed the Watir gem you already have the AutoIT  
control installed. Just do something like this:

regsvr32 C:\ruby\lib\ruby\gems\1.8\gems\watir-1.4.1\watir\AutoItX3.dll  
irb  
require ‘win32ole’  
a = WIN32OLE.new(“AutoItX3.Control”)  
a.ole\_methods

Then review the AutoIT docs.

Nate

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [May 8, 2007, 8:44am UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/7 "2007-05-08T08:44:16Z")

</div>

Kid Kid wrote:

> Hi, I would like to simulate a Mouse click through Ruby. Is there a way  
> to do this through Ruby? Does Ruby have any library for simulating  
> mouse events. Can someone provide me with some info on this topic.  
> Thanks in advance.

KLUDGE:  
Write a ruby script that outputs this to a file named Bot.java, runs  
javac, then runs the JVM:

import java.awt.AWTException;  
import java.awt.Robot;  
import java.awt.event.InputEvent;  
import java.awt.event.KeyEvent;  
class Bot {  
public static void main(String[] args) {  
try {  
Robot robot = new Robot();  
robot.mousePress(InputEvent.BUTTON1\_MASK);  
robot.mouseRelease(InputEvent.BUTTON1\_MASK);  
} catch (AWTException e) {  
e.printStackTrace();  
}  
}  
}

Less of a kludge: Find how java.awt.Robot is implemented in C now that  
Sun has relased Java as open source.

I hope mouse functionality is part of the standard library in Ruby2.

---

<div class="post-metadata">

**Author:** ![rubykid99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@rubykid99](https://www.ruby-forum.com/u/rubykid99)\
**Post date:** [May 8, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/how-to-simulate-mouse-click-with-ruby-or-mouse-library/83297/8 "2007-05-08T14:45:22Z")

</div>

On May 8, 2:44 am, Chad B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> } catch (AWTException e) {
> 
> ```
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

I would imagine you could use

> }
> 
> Less of a kludge: Find how java.awt.Robot is implemented in C now that  
> Sun has relased Java as open source.
> 
> I hope mouse functionality is part of the standard library in Ruby2.
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

I would imagine you could use the Robot class from jruby, though I  
don’t know if jruby is an option in KidK’s case.

–Brenton
