Simulate input/Mouse: Difference between revisions

m
→‎{{header|Wren}}: Added compilation hint for C program.
(Added Wren)
m (→‎{{header|Wren}}: Added compilation hint for C program.)
 
(6 intermediate revisions by 5 users not shown)
Line 3:
=={{header|AutoHotkey}}==
target gui may be externally created.
<langsyntaxhighlight AutoHotkeylang="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.</langsyntaxhighlight>
 
=={{header|C}}==
===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
#include<windows.h>
Line 50:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
{{libheader|xdotool}}
The xdotool have to be installed on the machine (installable through apt-get). Tested on Lubuntu 14.04.
<langsyntaxhighlight lang="lisp">
(defun sh (cmd)
#+clisp (shell cmd)
Line 64:
(sleep 2)
(sh "xdotool mousemove 300 300 click 1")
</syntaxhighlight>
</lang>
 
=={{header|Fantom}}==
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:
 
<langsyntaxhighlight lang="fantom">
using fwt
using gfx
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:
 
<langsyntaxhighlight lang="fantom">
using [java] java.awt::Robot
using [java] java.awt.event::InputEvent
Line 137:
}
}
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
Código sacado de https://www.freebasic.net/forum/
<syntaxhighlight lang="freebasic">#include "fbgfx.bi"
#define rect 4
 
Windowtitle "Mouse events"
Screen 19,32
Dim Shared As Integer xres, yres
Screeninfo xres, yres
 
Type box
As Single x, y, z
As String caption
As Uinteger textcol, boxcol
End Type
 
Dim Shared As box label(rect,1)
Dim Shared As box button(rect,1)
Dim Shared As Boolean flag
Dim Shared As fb.event e
Dim As Uinteger background = Rgb(100,100,100)
 
Sub thickline(x1 As Double, y1 As Double, x2 As Double, y2 As Double,_
thickness As Double, colour As Uinteger, im As Any Pointer = 0)
Dim p As Uinteger = Rgb(255, 255, 254)
If thickness < 2 Then
Line(x1,y1)-(x2,y2), colour
Else
Dim As Double h = Sqr((x2-x1)^2+(y2-y1)^2), s = (y1-y2)/h, c = (x2-x1)/h
For x As Integer = 1 To 2
Line im,(x1+s*thickness/2,y1+c*thickness/2)-(x2+s*thickness/2,y2+c*thickness/2),p
Line im,(x1-s*thickness/2,y1-c*thickness/2)-(x2-s*thickness/2,y2-c*thickness/2),p
Line im,(x1+s*thickness/2,y1+c*thickness/2)-(x1-s*thickness/2,y1-c*thickness/2),p
Line im,(x2+s*thickness/2,y2+c*thickness/2)-(x2-s*thickness/2,y2-c*thickness/2),p
Paint im,((x1+x2)/2, (y1+y2)/2), p, p
p = colour
Next x
End If
End Sub
 
Function inbox(p1() As box, p2 As box) As Integer
Type pt2d : As Single x, y : End Type
Type ln2d : As pt2d v1, v2 : End Type
#macro isleft(L,p)
-Sgn((L.v1.x-L.v2.x)*(p.y-L.v2.y) - (p.x-L.v2.x)*(L.v1.y-L.v2.y))
#endmacro
Dim As Single n1 = p1(rect,0).z
Dim As Integer index, nextindex
Dim send As ln2d
Dim As Integer n, wn = 0
For n = 1 To 4
index = n Mod 5 : nextindex = (n+1) Mod 5
If nextindex = 0 Then nextindex = 1
send.v1.x = p1(index,n1).x : send.v2.x = p1(nextindex,n1).x
send.v1.y = p1(index,n1).y : send.v2.y = p1(nextindex,n1).y
If p1(index,n1).y <= p2.y Then
If p1(nextindex,n1).y > p2.y Then
If isleft(send,p2) > 0 Then wn += 1
End If
Else
If p1(nextindex,n1).y <= p2.y Then
If isleft(send,p2) < 0 Then wn -= 1
End If
End If
Next n
Return wn
End Function
 
Sub draw_box(p() As box, col As Uinteger, pnt As String = "paint", im As Any Pointer = 0)
Dim As Integer index, nextindex
Dim As Single n1 = p(rect,0).z
Dim As Double xc, yc
For n As Integer = 1 To 4
xc += p(n,n1).x : yc += p(n,n1).y
index = n Mod 5 : nextindex = (n+1) Mod 5
If nextindex = 0 Then nextindex = 1
thickline(p(index,n1).x,p(index,n1).y, p(nextindex,n1).x,p(nextindex,n1).y,4,col,im)
Next n
xc /= Ubound(p) : yc /= Ubound(p)
If pnt = "paint" Then Paint(xc,yc), col, col
End Sub
 
Sub highlightbox(box() As box, mp As box, col As Uinteger)
box(rect,0).z = 1
If inbox(box(),mp) Then draw_box(box(),col,"dont_paint")
End Sub
 
Sub checkbox(box() As box,mp As box)
flag = True
label(rect,1).caption = box(rect,1).caption
label(rect,1).textcol = box(rect,1).textcol
label(rect,1).boxcol = box(rect,1).boxcol
End Sub
 
Sub drawbox(x As Integer, y As Integer, box()As box, boxlength As Integer, _
boxheight As Integer, col As Uinteger, highlight As Uinteger, caption As String)
Dim As box startpoint
startpoint.x = x : startpoint.y = y
Dim As Integer mmx, mmy
Getmouse mmx, mmy
Dim As box mouse
mouse.x = mmx : mouse.y = mmy
box(rect,1).boxcol = col
box(rect,1).caption = caption
Dim As Uinteger outline, count = 1
outline = Rgb(255,255,255)
For x As Integer = 1 To 4
Select Case x
Case 1
box(1,count).x = startpoint.x
box(1,count).y = startpoint.y
Case 2
box(2,count).x = box(1,count).x + boxlength
box(2,count).y = box(1,count).y
Case 3
box(3,count).x = box(2,count).x
box(3,count).y = box(2,count).y + boxheight
Case 4
box(4,count).x = box(3,count).x - boxlength
box(4,count).y = box(3,count).y
End Select
Next x
box(rect,0).z = 1
draw_box(box(),col)
draw_box(box(),outline,"nopaint")
If inbox(box(),mouse) Then
highlightbox(box(),mouse,highlight)
If (Screenevent(@e)) Then
If e.type = fb.EVENT_MOUSE_BUTTON_PRESS Then checkbox(box(),mouse)
End If
End If
Draw String(box(1,1).x+5,box(1,1).y+5), box(rect,1).caption,box(rect,1).textcol
End Sub
 
' ---------- Main Program ----------
Do
Screenlock
Cls
Paint(0,0), background
Draw String(300,50), "BUTTON CLICK TEST", Rgb(0,0,200)
drawbox(100,100,button(),100,50,Rgb(200,200,0),Rgb(00,0,200),"Box 1")
drawbox(100,200,button(),100,50,Rgb(200,0,190),Rgb(00,0,200),"Box 2")
drawbox(100,300,button(),100,50,Rgb(0,200,190),Rgb(00,0,200),"Box 3")
If flag Then drawbox(400,100,label(),250,250,label(rect,1).boxcol,Rgb(255,255,255),label(rect,1).caption)
If (Screenevent(@e)) Then
If e.type=13 Then End
End If
Screenunlock
Sleep 1, 1
Loop Until Inkey = Chr(27)
</syntaxhighlight>
Output:
 
https://www.dropbox.com/s/avn8h46iy8ngizc/MouseEvents_FB.png?dl=0
 
=={{header|Go}}==
Line 143 ⟶ 300:
<br>
The target GUI may be externally created.
<langsyntaxhighlight lang="go">package main
 
import "github.com/go-vgo/robotgo"
Line 150 ⟶ 307:
robotgo.MouseClick("left", false) // single clicks left mouse button
robotgo.MouseClick("right", true) // double clicks right mouse button
}</langsyntaxhighlight>
 
=={{header|GUISS}}==
 
<langsyntaxhighlight lang="guiss">Start,Programs,Accessories,Notepad,Textbox,Type:Hello World[pling],Menu:File,Save,
Inputbox:filename>greetings.txt,Button:Save</langsyntaxhighlight>
 
=={{header|Java}}==
You can click on any Component using a Robot and the Component's location:
<langsyntaxhighlight 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);</langsyntaxhighlight>
If you don't have a reference to the component, you'll need to guess at where it is.
 
Line 170 ⟶ 327:
 
If you have a reference to the AbstractButton this is simpler:
<langsyntaxhighlight lang="java">button.doClick(); //optionally, give an integer argument for the number of milliseconds to hold the button down</langsyntaxhighlight>
 
=={{header|Julia}}==
This may be done using Julia's C call FFI:
 
<langsyntaxhighlight lang="julia">
# Wrap win32 API function mouse_event() from the User32 dll.
function mouse_event_wrapper(dwFlags,dx,dy,dwData,dwExtraInfo)
ccall((:mouse_event, "User32"),stdcall,VoidNothing,(UInt32,UInt32,UInt32,UInt32,UInt),dwFlags,dx,dy,dwData,dwExtraInfo)
end
 
Line 185 ⟶ 342:
mouse_event_wrapper(0x4,0,0,0,0)
end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.Robot
Line 202 ⟶ 359:
sendClick(InputEvent.BUTTON3_DOWN_MASK) // simulate a click of the mouse's right button
}
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
Using Tk events, this only works with internal windows.
<langsyntaxhighlight lang="oz">declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
Button
Line 215 ⟶ 372:
{Tk.send event(generate Button "<ButtonPress-1>")}
{Delay 500}
{Tk.send event(generate Button "<ButtonRelease-1>")}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<!--<syntaxhighlight lang="phix">(notonline)-->
Note that CUSRORPOS, SCREENPOSITION, and MOUSEBUTTON are known to be a little flaky and/or system-dependent, ymmv.
<span style="color: #000080;font-style:italic;">--
<lang Phix>include pGUI.e
-- demo\rosetta\Simulate_mouse_input.exw
include timedate.e
--
-- Note that CURSORPOS, SCREENPOSITION, and MOUSEBUTTON are known to be a little flaky and/or system-dependent, ymmv.
--</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- you'd better hope this sort of thing ain't possible in a browser!</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (IupGetGlobalIntInt)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">delay</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">16</span> <span style="color: #000080;font-style:italic;">-- (4s @ 250ms)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">btn</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">action_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*btn*/</span><span style="color: #0000FF;">)</span>
integer delay = 16 -- (4s @ 250ms)
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">date</span><span style="color: #0000FF;">(),</span><span style="color: #008000;">"hh:mm:sspm"</span><span style="color: #0000FF;">)</span>
Ihandle button1
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">btn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Clicked at %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">})</span>
 
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function action_cb(Ihandle /*button1*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
string t = format_timedate(date(),"hh:mm:sspm")
IupSetStrAttribute(button1,"TITLE","Clicked at %s",{t})
<span style="color: #004080;">integer</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cy</span>
return IUP_IGNORE
end function
 
integer cx, cy
 
function timer_cb(Ihandle timer)
if delay then
IupSetStrAttribute(button1,"TITLE","%3.2f",{delay/4})
delay -= 1
{cx,cy} = IupGetGlobal("CURSORPOS")
else
integer {btnx,btny} = IupGetIntInt(button1,"SCREENPOSITION"),
{sx,sy} = IupGetIntInt(button1,"SIZE"),
{dx,dy} = {btnx+sx/2,btny+sy/2}
if {dx,dy}!={cx,cy} then
if IupGetAttribute(timer,"TIME")="250" then
IupSetAttribute(timer,"TIME","40")
IupSetAttribute(timer,"RUN","NO")
IupSetAttribute(timer,"RUN","YES")
end if
for i=1 to 15 do
if cx<dx then cx += 1 end if
if cx>dx then cx -= 1 end if
if cy<dy then cy += 1 end if
if cy>dy then cy -= 1 end if
end for
string cxcy = sprintf("%dx%d ",{cx,cy})
IupSetGlobal("CURSORPOS",cxcy)
else
string s = sprintf("%dx%d 1 2",{btnx+sx/2,btny+sy/2})
IupSetStrGlobal("MOUSEBUTTON", s)
IupSetAttribute(timer,"RUN","NO")
end if
end if
return IUP_CONTINUE
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">timer</span><span style="color: #0000FF;">)</span>
IupOpen()
<span style="color: #008080;">if</span> <span style="color: #000000;">delay</span> <span style="color: #008080;">then</span>
button1 = IupButton("button1",Icallback("action_cb"),"SIZE=100x12")
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">btn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3.2f"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">delay</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span><span style="color: #0000FF;">})</span>
Ihandle dlg = IupDialog(button1,`TITLE="Simulate mouse input", CHILDOFFSET=10x40, SIZE=200x80`)
<span style="color: #000000;">delay</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
IupShow(dlg)
<span style="color: #0000FF;">{</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetGlobalIntInt</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"CURSORPOS"</span><span style="color: #0000FF;">)</span>
Ihandle timer = IupTimer(Icallback("timer_cb"), 250)
<span style="color: #008080;">else</span>
IupMainLoop()
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">xb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">yb</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">btn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SCREENPOSITION"</span><span style="color: #0000FF;">),</span>
IupClose()</lang>
<span style="color: #0000FF;">{</span><span style="color: #000000;">wb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hb</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">btn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">xb</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wb</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">yb</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hb</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">if</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">}!={</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TIME"</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">250</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TIME"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RUN"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RUN"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">15</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;"><</span><span style="color: #000000;">dx</span> <span style="color: #008080;">then</span> <span style="color: #000000;">cx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">></span><span style="color: #000000;">dx</span> <span style="color: #008080;">then</span> <span style="color: #000000;">cx</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;"><</span><span style="color: #000000;">dy</span> <span style="color: #008080;">then</span> <span style="color: #000000;">cy</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;">></span><span style="color: #000000;">dy</span> <span style="color: #008080;">then</span> <span style="color: #000000;">cy</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">cxcy</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%dx%d "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetGlobal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"CURSORPOS"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cxcy</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%dx%d 1 2"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetStrGlobal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"MOUSEBUTTON"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RUN"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"NO"</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>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">btn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"button"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"action_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"SIZE=100x12"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">btn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Simulate mouse input", CHILDOFFSET=10x40, SIZE=200x80`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">timer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">250</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (just for consistency)</span>
<span style="color: #7060A8;">IupMainLoop</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>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
Line 284 ⟶ 451:
in the following example. Mouse input is simulated with the functions 'click'
(click on a HTML link) and 'press' (press a submit button).
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/http.l" "@lib/scrape.l")
 
# Connect to the demo app at https://7fach.de/app/
Line 298 ⟶ 465:
(click "Spare Part") # Click on "Spare Part" article
(prinl (value 8)) # Print the price (12.50)
(click "logout") # Log out</langsyntaxhighlight>
Output:
<pre>12.50</pre>
Line 305 ⟶ 472:
=={{header|PureBasic}}==
This code is Windows only.
<langsyntaxhighlight PureBasiclang="purebasic">Macro Click()
mouse_event_(#MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event_(#MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Line 317 ⟶ 484:
; Move to a new location and click it
SetCursorPos_(50, 50)
Click()</langsyntaxhighlight>
{{libheader|AutoWin}}
<langsyntaxhighlight PureBasiclang="purebasic">; The same function as above, but using AutoWin UserLibray
AW_MouseClick()
Delay(1000)
AW_MouseClick(#PB_MouseButton_Left, 50, 50)</langsyntaxhighlight>
 
=={{header|Python}}==
In Windows (GUI can be externally created):
<langsyntaxhighlight Pythonlang="python">import ctypes
 
def click():
Line 333 ⟶ 500:
ctypes.windll.user32.mouse_event(0x4, 0,0,0,0) # Mouse LClick Up, relative coords, dx=0, dy=0
 
click()</langsyntaxhighlight>
 
 
{{libheader|AutoPy}}
<langsyntaxhighlight Pythonlang="python">import autopy
import math
import time
Line 359 ⟶ 526:
time.sleep(random.uniform(0.001, 0.003))
 
sine_mouse_wave()</langsyntaxhighlight>
 
{{libheader|PyAutoGUI}}
<langsyntaxhighlight Pythonlang="python">import pyautogui
 
pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
Line 384 ⟶ 551:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 391 ⟶ 558:
Same as the Python entry: use a User32 function to simulate a mouse click.
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang at-exp racket
 
Line 402 ⟶ 569:
(mouse-event #x2 0 0 0 #f)
(mouse-event #x4 0 0 0 #f)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 409 ⟶ 576:
Using bindings to libxdo so any window managed by an X11 display server can receive mouse events.
 
<syntaxhighlight lang="raku" perl6line>use X11::libxdo;
my $xdo = Xdo.new;
 
Line 456 ⟶ 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.
]
</syntaxhighlight>
</lang>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Simulate input/Mouse
 
Line 502 ⟶ 669:
line1.settext("")
line2.settext("Mouse was released")
</syntaxhighlight>
</lang>
Output:
 
Line 511 ⟶ 678:
{{libheader|AutoPilot}}
 
<langsyntaxhighlight Rustlang="rust">extern crate autopilot;
extern crate rand;
use rand::Rng;
Line 535 ⟶ 702:
fn main() {
sine_mouse_wave().expect("Unable to move mouse");
}</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="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.mousePress(InputEvent.BUTTON1_MASK) //BUTTON1_MASK is the left button
robot.mouseRelease(InputEvent.BUTTON1_MASK)</langsyntaxhighlight>
 
=={{header|Tcl}}==
===Within an Application===
{{libheader|Tk}}
<langsyntaxhighlight 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</langsyntaxhighlight>
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:
<syntaxhighlight lang ="tcl">.okBtn invoke</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 561 ⟶ 728:
 
Xlib can of course work with any X server, local or remote.
<langsyntaxhighlight ecmascriptlang="wren">/* simulate_input_keyboardSimulate_input_Mouse.wren */
 
var KeyPressMask = 1 << 0
Line 740 ⟶ 907:
 
/* close connection to server */
xd.closeDisplay()</langsyntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Simulate_input_Mouse.c -o Simulate_input_Mouse -lX11 -lwren -lm */
<lang c>#include <stdio.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,101 ⟶ 1,270:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "simulate_input_mouseSimulate_input_Mouse.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,117 ⟶ 1,286:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{omit from|ACL2}}
9,476

edits