Simulate input/Mouse: Difference between revisions
Content added Content deleted
(Simulate input/Mouse in FreeBASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 3: | Line 3: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
target gui may be externally created. |
target gui may be externally created. |
||
< |
<syntaxhighlight lang="autohotkey">WinActivate, ahk_class MozillaUIWindowClass |
||
Click 200, 200 right ; relative to external window (firefox) |
Click 200, 200 right ; relative to external window (firefox) |
||
sleep, 2000 |
sleep, 2000 |
||
WinMinimize |
WinMinimize |
||
CoordMode, Mouse, Screen |
CoordMode, Mouse, Screen |
||
Click 400, 400 right ; relative to top left corner of the screen.</ |
Click 400, 400 right ; relative to top left corner of the screen.</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
===Windows=== |
===Windows=== |
||
Animates the movement of the mouse pointer from the screen center to the bottom left corner where the Windows button is usually present on most Windows desktops. Once there, a left click is simulated. The exact speed, motion and behaviour of the pointer will vary from desktop to desktop. Compatible with MinGW or GCC for Windows. |
Animates the movement of the mouse pointer from the screen center to the bottom left corner where the Windows button is usually present on most Windows desktops. Once there, a left click is simulated. The exact speed, motion and behaviour of the pointer will vary from desktop to desktop. Compatible with MinGW or GCC for Windows. |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
#define WINVER 0x500 |
#define WINVER 0x500 |
||
#include<windows.h> |
#include<windows.h> |
||
Line 50: | Line 50: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{libheader|xdotool}} |
{{libheader|xdotool}} |
||
The xdotool have to be installed on the machine (installable through apt-get). Tested on Lubuntu 14.04. |
The xdotool have to be installed on the machine (installable through apt-get). Tested on Lubuntu 14.04. |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun sh (cmd) |
(defun sh (cmd) |
||
#+clisp (shell cmd) |
#+clisp (shell cmd) |
||
Line 64: | Line 64: | ||
(sleep 2) |
(sleep 2) |
||
(sh "xdotool mousemove 300 300 click 1") |
(sh "xdotool mousemove 300 300 click 1") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Line 70: | Line 70: | ||
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: |
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: |
||
< |
<syntaxhighlight lang="fantom"> |
||
using fwt |
using fwt |
||
using gfx |
using gfx |
||
Line 104: | Line 104: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</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: |
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: |
||
< |
<syntaxhighlight lang="fantom"> |
||
using [java] java.awt::Robot |
using [java] java.awt::Robot |
||
using [java] java.awt.event::InputEvent |
using [java] java.awt.event::InputEvent |
||
Line 137: | Line 137: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Código sacado de https://www.freebasic.net/forum/ |
Código sacado de https://www.freebasic.net/forum/ |
||
< |
<syntaxhighlight lang="freebasic">#include "fbgfx.bi" |
||
#define rect 4 |
#define rect 4 |
||
Line 291: | Line 291: | ||
Sleep 1, 1 |
Sleep 1, 1 |
||
Loop Until Inkey = Chr(27) |
Loop Until Inkey = Chr(27) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 300: | Line 300: | ||
<br> |
<br> |
||
The target GUI may be externally created. |
The target GUI may be externally created. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "github.com/go-vgo/robotgo" |
import "github.com/go-vgo/robotgo" |
||
Line 307: | Line 307: | ||
robotgo.MouseClick("left", false) // single clicks left mouse button |
robotgo.MouseClick("left", false) // single clicks left mouse button |
||
robotgo.MouseClick("right", true) // double clicks right mouse button |
robotgo.MouseClick("right", true) // double clicks right mouse button |
||
}</ |
}</syntaxhighlight> |
||
=={{header|GUISS}}== |
=={{header|GUISS}}== |
||
< |
<syntaxhighlight lang="guiss">Start,Programs,Accessories,Notepad,Textbox,Type:Hello World[pling],Menu:File,Save, |
||
Inputbox:filename>greetings.txt,Button:Save</ |
Inputbox:filename>greetings.txt,Button:Save</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
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: |
||
< |
<syntaxhighlight lang="java">Point p = component.getLocation(); |
||
Robot robot = new Robot(); |
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.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, |
robot.mousePress(InputEvent.BUTTON1_MASK); //BUTTON1_MASK is the left button, |
||
//BUTTON2_MASK is the middle button, BUTTON3_MASK is the right button |
//BUTTON2_MASK is the middle button, BUTTON3_MASK is the right button |
||
robot.mouseRelease(InputEvent.BUTTON1_MASK);</ |
robot.mouseRelease(InputEvent.BUTTON1_MASK);</syntaxhighlight> |
||
If you don't have a reference to the component, you'll need to guess at where it is. |
If you don't have a reference to the component, you'll need to guess at where it is. |
||
Line 327: | Line 327: | ||
If you have a reference to the AbstractButton this is simpler: |
If you have a reference to the AbstractButton this is simpler: |
||
< |
<syntaxhighlight lang="java">button.doClick(); //optionally, give an integer argument for the number of milliseconds to hold the button down</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
This may be done using Julia's C call FFI: |
This may be done using Julia's C call FFI: |
||
< |
<syntaxhighlight lang="julia"> |
||
# Wrap win32 API function mouse_event() from the User32 dll. |
# Wrap win32 API function mouse_event() from the User32 dll. |
||
function mouse_event_wrapper(dwFlags,dx,dy,dwData,dwExtraInfo) |
function mouse_event_wrapper(dwFlags,dx,dy,dwData,dwExtraInfo) |
||
Line 342: | Line 342: | ||
mouse_event_wrapper(0x4,0,0,0,0) |
mouse_event_wrapper(0x4,0,0,0,0) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.awt.Robot |
import java.awt.Robot |
||
Line 359: | Line 359: | ||
sendClick(InputEvent.BUTTON3_DOWN_MASK) // simulate a click of the mouse's right button |
sendClick(InputEvent.BUTTON3_DOWN_MASK) // simulate a click of the mouse's right button |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Using Tk events, this only works with internal windows. |
Using Tk events, this only works with internal windows. |
||
< |
<syntaxhighlight lang="oz">declare |
||
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']} |
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']} |
||
Button |
Button |
||
Line 372: | Line 372: | ||
{Tk.send event(generate Button "<ButtonPress-1>")} |
{Tk.send event(generate Button "<ButtonPress-1>")} |
||
{Delay 500} |
{Delay 500} |
||
{Tk.send event(generate Button "<ButtonRelease-1>")}</ |
{Tk.send event(generate Button "<ButtonRelease-1>")}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/pGUI}} |
{{libheader|Phix/pGUI}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\Simulate_mouse_input.exw |
-- demo\rosetta\Simulate_mouse_input.exw |
||
Line 439: | Line 439: | ||
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 451: | Line 451: | ||
in the following example. Mouse input is simulated with the functions 'click' |
in the following example. Mouse input is simulated with the functions 'click' |
||
(click on a HTML link) and 'press' (press a submit button). |
(click on a HTML link) and 'press' (press a submit button). |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/http.l" "@lib/scrape.l") |
||
# Connect to the demo app at https://7fach.de/app/ |
# Connect to the demo app at https://7fach.de/app/ |
||
Line 465: | Line 465: | ||
(click "Spare Part") # Click on "Spare Part" article |
(click "Spare Part") # Click on "Spare Part" article |
||
(prinl (value 8)) # Print the price (12.50) |
(prinl (value 8)) # Print the price (12.50) |
||
(click "logout") # Log out</ |
(click "logout") # Log out</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>12.50</pre> |
<pre>12.50</pre> |
||
Line 472: | Line 472: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
This code is Windows only. |
This code is Windows only. |
||
< |
<syntaxhighlight lang="purebasic">Macro Click() |
||
mouse_event_(#MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) |
mouse_event_(#MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) |
||
mouse_event_(#MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) |
mouse_event_(#MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) |
||
Line 484: | Line 484: | ||
; Move to a new location and click it |
; Move to a new location and click it |
||
SetCursorPos_(50, 50) |
SetCursorPos_(50, 50) |
||
Click()</ |
Click()</syntaxhighlight> |
||
{{libheader|AutoWin}} |
{{libheader|AutoWin}} |
||
< |
<syntaxhighlight lang="purebasic">; The same function as above, but using AutoWin UserLibray |
||
AW_MouseClick() |
AW_MouseClick() |
||
Delay(1000) |
Delay(1000) |
||
AW_MouseClick(#PB_MouseButton_Left, 50, 50)</ |
AW_MouseClick(#PB_MouseButton_Left, 50, 50)</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
In Windows (GUI can be externally created): |
In Windows (GUI can be externally created): |
||
< |
<syntaxhighlight lang="python">import ctypes |
||
def click(): |
def click(): |
||
Line 500: | Line 500: | ||
ctypes.windll.user32.mouse_event(0x4, 0,0,0,0) # Mouse LClick Up, relative coords, dx=0, dy=0 |
ctypes.windll.user32.mouse_event(0x4, 0,0,0,0) # Mouse LClick Up, relative coords, dx=0, dy=0 |
||
click()</ |
click()</syntaxhighlight> |
||
{{libheader|AutoPy}} |
{{libheader|AutoPy}} |
||
< |
<syntaxhighlight lang="python">import autopy |
||
import math |
import math |
||
import time |
import time |
||
Line 526: | Line 526: | ||
time.sleep(random.uniform(0.001, 0.003)) |
time.sleep(random.uniform(0.001, 0.003)) |
||
sine_mouse_wave()</ |
sine_mouse_wave()</syntaxhighlight> |
||
{{libheader|PyAutoGUI}} |
{{libheader|PyAutoGUI}} |
||
< |
<syntaxhighlight lang="python">import pyautogui |
||
pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200. |
pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200. |
||
Line 551: | Line 551: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 558: | Line 558: | ||
Same as the Python entry: use a User32 function to simulate a mouse click. |
Same as the Python entry: use a User32 function to simulate a mouse click. |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang at-exp racket |
#lang at-exp racket |
||
Line 569: | Line 569: | ||
(mouse-event #x2 0 0 0 #f) |
(mouse-event #x2 0 0 0 #f) |
||
(mouse-event #x4 0 0 0 #f) |
(mouse-event #x4 0 0 0 #f) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 576: | Line 576: | ||
Using bindings to libxdo so any window managed by an X11 display server can receive mouse events. |
Using bindings to libxdo so any window managed by an X11 display server can receive mouse events. |
||
<lang |
<syntaxhighlight lang="raku" line>use X11::libxdo; |
||
my $xdo = Xdo.new; |
my $xdo = Xdo.new; |
||
Line 623: | Line 623: | ||
.mouse-button-multiple( $window, $button, $repeat = 2, $delay? ) # Send a one or more clicks of a specific mouse button at the current mouse location. |
.mouse-button-multiple( $window, $button, $repeat = 2, $delay? ) # Send a one or more clicks of a specific mouse button at the current mouse location. |
||
] |
] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Simulate input/Mouse |
# Project : Simulate input/Mouse |
||
Line 669: | Line 669: | ||
line1.settext("") |
line1.settext("") |
||
line2.settext("Mouse was released") |
line2.settext("Mouse was released") |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 678: | Line 678: | ||
{{libheader|AutoPilot}} |
{{libheader|AutoPilot}} |
||
< |
<syntaxhighlight lang="rust">extern crate autopilot; |
||
extern crate rand; |
extern crate rand; |
||
use rand::Rng; |
use rand::Rng; |
||
Line 702: | Line 702: | ||
fn main() { |
fn main() { |
||
sine_mouse_wave().expect("Unable to move mouse"); |
sine_mouse_wave().expect("Unable to move mouse"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
< |
<syntaxhighlight lang="scala"> val (p , robot)= (component.location, new Robot()) |
||
robot.mouseMove(p.getX().toInt, p.getY().toInt) //you may want to move a few pixels closer to the center by adding to these values |
robot.mouseMove(p.getX().toInt, p.getY().toInt) //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 |
robot.mousePress(InputEvent.BUTTON1_MASK) //BUTTON1_MASK is the left button |
||
robot.mouseRelease(InputEvent.BUTTON1_MASK)</ |
robot.mouseRelease(InputEvent.BUTTON1_MASK)</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
===Within an Application=== |
===Within an Application=== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
< |
<syntaxhighlight lang="tcl"># Simulate a full click cycle: button down and up |
||
event generate .okBtn <ButtonPress-1> -x 5 -y 5 |
event generate .okBtn <ButtonPress-1> -x 5 -y 5 |
||
event generate .okBtn <ButtonRelease-1> -x 5 -y 5</ |
event generate .okBtn <ButtonRelease-1> -x 5 -y 5</syntaxhighlight> |
||
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: |
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 |
<syntaxhighlight lang="tcl">.okBtn invoke</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 728: | Line 728: | ||
Xlib can of course work with any X server, local or remote. |
Xlib can of course work with any X server, local or remote. |
||
< |
<syntaxhighlight lang="ecmascript">/* simulate_input_mouse.wren */ |
||
var KeyPressMask = 1 << 0 |
var KeyPressMask = 1 << 0 |
||
Line 907: | Line 907: | ||
/* close connection to server */ |
/* close connection to server */ |
||
xd.closeDisplay()</ |
xd.closeDisplay()</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed this Wren script in the following C program, compile and run it. |
We now embed this Wren script in the following C program, compile and run it. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 1,284: | Line 1,284: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{omit from|ACL2}} |
{{omit from|ACL2}} |