Simulate input/Mouse

From Rosetta Code
Revision as of 12:11, 12 July 2011 by rosettacode>Markhobley ({{header|GUISS}})
Task
Simulate input/Mouse
You are encouraged to solve this task according to the task description, using any language you may know.

Simulate the click of a mouse button by the user. Specify if the target GUI may be externally created.

AutoHotkey

target gui may be externally created. <lang AutoHotkey>WinActivate, ahk_class MozillaUIWindowClass Click 200, 200 right  ; relative to external window (firefox) sleep, 2000 WinMinimize CoordMode, Mouse, Screen Click 400, 400 right  ; relative to top left corner of the screen.</lang>

Fantom

You can simulate a mouse click on a button by asking that button to fire its event listeners. This approach only works for the program's own GUI:

<lang fantom> using fwt using gfx

class Main {

 public static Void main ()
 {
   button1 := Button 
   { 
     text = "don't click!"
     onAction.add |Event e|
     {
       echo ("clicked by code")
     }
   }
   button2 := Button 
   { 
     text = "click"
     onAction.add |Event e|
     {
       // fire all the event listeners on button1
       button1.onAction.fire(e)
     }
   }
   Window
   {
     title = "simulate mouse event"
     size = Size (300, 200)
     button1,
     button2,
   }.open
 }

} </lang>

Alternatively, if you are running on the Java Runtime, you can use Java's 'robot' library to click anywhere on the screen, and so interact with widgets from other programs:

<lang fantom> using [java] java.awt::Robot using [java] java.awt.event::InputEvent using fwt using gfx

class Main {

 public static Void main ()
 {
   button := Button 
   { 
     text = "click for robot"
     onAction.add |Event e|
     {
       robot := Robot ()
       robot.mouseMove (50, 50) // move to screen point 50, 50
       robot.mousePress (InputEvent.BUTTON1_MASK) // and click mouse
       robot.mouseRelease (InputEvent.BUTTON1_MASK)
     }
   }
   Window
   {
     title = "simulate mouse event"
     size = Size (300, 200)
     button,
   }.open
 }

} </lang>

GUISS

<lang guiss>Start,Programs,Accessories,Notepad,Textbox,Type:Hello World!,Menu:File,Save,Input Box:filename>greetings.txt,Button:OK</lang>

Java

You can click on any Component using a Robot and the Component's location: <lang java>Point p = component.getLocation(); Robot robot = new Robot(); robot.mouseMove(p.getX(), p.getY()); //you may want to move a few pixels closer to the center by adding to these values robot.mousePress(InputEvent.BUTTON1_MASK); //BUTTON1_MASK is the left button,

                                      //BUTTON2_MASK is the middle button, BUTTON3_MASK is the right button

robot.mouseRelease(InputEvent.BUTTON1_MASK);</lang> If you don't have a reference to the component, you'll need to guess at where it is.

Library: Swing

If you have a reference to the AbstractButton this is simpler: <lang java>button.doClick(); //optionally, give an integer argument for the number of milliseconds to hold the button down</lang>

Oz

Using Tk events, this only works with internal windows. <lang oz>declare

 [QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
 Button
 Window = {QTk.build td(button(text:"Click me" handle:Button))}

in

 {Window show}
 {Delay 500}
 {Tk.send event(generate Button "<ButtonPress-1>")}
 {Delay 500}
 {Tk.send event(generate Button "<ButtonRelease-1>")}</lang>

PicoLisp

PicoLisp comes with a dedicated browser GUI. A library based on web scraping (in "lib/scrape.l") can be used to drive that GUI under program control. It allows to read GUI pages, click on HTML links, enter text into forms, and press submit buttons. In that way one application can control another application.

The documented demo application, which is also available online at app.7fach.de, is used in the following example. Mouse input is simulated with the functions 'click' (click on a HTML link) and 'press' (press a submit button). <lang PicoLisp>(load "@lib/http.l" "@lib/scrape.l")

  1. Connect to the demo app at http://7fach.de/8080

(scrape "7fach.de" 80 "8080")

  1. Log in

(expect "'admin' logged in"

  (enter 3 "admin")       # Enter user name into 3rd field
  (enter 4 "admin")       # Enter password into 4th field
  (press "login") )       # Press the "login" button

(click "Items") # Open "Items" dialog (click "Spare Part") # Click on "Spare Part" article (prinl (value 8)) # Print the price (12.50) (click "logout") # Log out</lang> Output:

12.50

The same example is used in the related task Simulate input/Keyboard#PicoLisp.

PureBasic

This code is Windows only. <lang PureBasic>Macro Click()

 mouse_event_(#MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
 mouse_event_(#MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

EndMacro

Click at the current location

Click()

Delay(1000) ; Wait a second

Move to a new location and click it

SetCursorPos_(50, 50) Click()</lang>

Library: AutoWin

<lang PureBasic>; The same function as above, but using AutoWin UserLibray AW_MouseClick() Delay(1000) AW_MouseClick(#PB_MouseButton_Left, 50, 50)</lang>

Tcl

Within an Application

Library: Tk

<lang tcl># Simulate a full click cycle: button down and up event generate .okBtn <ButtonPress-1> -x 5 -y 5 event generate .okBtn <ButtonRelease-1> -x 5 -y 5</lang> Note that many of Tk's windows also need appropriate <Enter> and <Leave> events in order to work correctly. For the process of actually simulating a click on a button, it is actually easier to work at the method-call level rather than the event generation level: <lang tcl>.okBtn invoke</lang>