Simulate input/Mouse: Difference between revisions
Added Oz. |
→{{header|Java}}: {{lines_too_long}} template |
||
Line 10: | Line 10: | ||
Click 400, 400 right ; relative to top left corner of the screen.</lang> |
Click 400, 400 right ; relative to top left corner of the screen.</lang> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{lines_too_long}} |
|||
You can click on any Component using a Robot and the Component's location: |
You can click on any Component using a Robot and the Component's location: |
||
<lang java>Point p = component.getLocation(); |
<lang java>Point p = component.getLocation(); |
Revision as of 03:43, 26 January 2010
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>
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.
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>
Tcl
Within an Application
<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>