Jump to content

Simulate input/Mouse

From Rosetta Code
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.

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.

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.

#define WINVER 0x500
#include<windows.h>

int main()
{
	int maxX = GetSystemMetrics(SM_CXSCREEN), maxY = GetSystemMetrics(SM_CYSCREEN);
	int x = maxX/2, y = maxY/2;
	double factorX = 65536.0 / maxX,factorY = 65536.0 / maxY;
	
	INPUT ip;
	
	ZeroMemory(&ip,sizeof(ip));
	
	ip.type = INPUT_MOUSE;
	
	while(x > 5 || y < maxY-5){

	ip.mi.mouseData = 0;
	ip.mi.dx = x * factorX;
	ip.mi.dy = y * factorY;
	ip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
		
	SendInput(1,&ip,sizeof(ip));
	Sleep(1);
	if(x>3)	
		x-=1;
	if(y<maxY-3)
		y+=1;
	}
	
	ip.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
	
	SendInput(1,&ip,sizeof(ip));
	
	return 0;
}

Common Lisp

Library: xdotool

The xdotool have to be installed on the machine (installable through apt-get). Tested on Lubuntu 14.04.

(defun sh (cmd)
#+clisp (shell cmd)
#+ecl (si:system cmd)
#+sbcl (sb-ext:run-program "/bin/sh" (list "-c" cmd) :input nil :output *standard-output*)
#+clozure (ccl:run-program "/bin/sh" (list "-c" cmd) :input nil :output *standard-output*))
(sh "xdotool mousemove 0 0 click 1")
(sleep 2)
(sh "xdotool mousemove 300 300 click 1")

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:

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
  }
}

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:

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
  }
}

FreeBASIC

Código sacado de https://www.freebasic.net/forum/

#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)

Output:

https://www.dropbox.com/s/avn8h46iy8ngizc/MouseEvents_FB.png?dl=0

Go

Library: RobotGo


The target GUI may be externally created.

package main

import "github.com/go-vgo/robotgo"

func main() {
    robotgo.MouseClick("left", false) // single clicks left mouse button
    robotgo.MouseClick("right", true) // double clicks right mouse button
}

GUISS

Start,Programs,Accessories,Notepad,Textbox,Type:Hello World[pling],Menu:File,Save,
Inputbox:filename>greetings.txt,Button:Save

Java

You can click on any Component using a Robot and the Component's location:

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);

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:

button.doClick(); //optionally, give an integer argument for the number of milliseconds to hold the button down

Julia

This may be done using Julia's C call FFI:

# Wrap win32 API function mouse_event() from the User32 dll.
function mouse_event_wrapper(dwFlags,dx,dy,dwData,dwExtraInfo)
    ccall((:mouse_event, "User32"),stdcall,Nothing,(UInt32,UInt32,UInt32,UInt32,UInt),dwFlags,dx,dy,dwData,dwExtraInfo)
end

function click()
    mouse_event_wrapper(0x2,0,0,0,0)
    mouse_event_wrapper(0x4,0,0,0,0)
end

Kotlin

// version 1.1.2

import java.awt.Robot
import java.awt.event.InputEvent

fun sendClick(buttons: Int) {
    val r = Robot()
    r.mousePress(buttons)
    r.mouseRelease(buttons)
}

fun main(args: Array<String>) {
    sendClick(InputEvent.BUTTON3_DOWN_MASK) // simulate a click of the mouse's right button
}

Oz

Using Tk events, this only works with internal windows.

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>")}

Phix

Library: Phix/pGUI
--
-- demo\rosetta\Simulate_mouse_input.exw
--
-- Note that CURSORPOS, SCREENPOSITION, and MOUSEBUTTON are known to be a little flaky and/or system-dependent, ymmv.
--
without js -- you'd better hope this sort of thing ain't possible in a browser!
requires("1.0.1") -- (IupGetGlobalIntInt)
include pGUI.e
include timedate.e
 
integer delay = 16  -- (4s @ 250ms)
Ihandle dlg, btn

function action_cb(Ihandle /*btn*/)
    string t = format_timedate(date(),"hh:mm:sspm")
    IupSetStrAttribute(btn,"TITLE","Clicked at %s",{t})
    return IUP_DEFAULT
end function

integer cx, cy

function timer_cb(Ihandle timer)
    if delay then
        IupSetStrAttribute(btn,"TITLE","%3.2f",{delay/4})
        delay -= 1
        {cx,cy} = IupGetGlobalIntInt("CURSORPOS")
    else
        integer {xb,yb} = IupGetIntInt(btn,"SCREENPOSITION"),
                {wb,hb} = IupGetIntInt(btn,"RASTERSIZE"),
                {dx,dy} = {xb+floor(wb/2),yb+floor(hb/2)}
        if {dx,dy}!={cx,cy} then
            if IupGetInt(timer,"TIME")=250 then
                IupSetInt(timer,"TIME",40)
                IupSetInt(timer,"RUN",false)
                IupSetInt(timer,"RUN",true)
            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",{dx,dy})
            IupSetStrGlobal("MOUSEBUTTON", s)  
            IupSetAttribute(timer,"RUN","NO")
        end if
    end if
    return IUP_CONTINUE
end function
 
IupOpen()
btn = IupButton("button",Icallback("action_cb"),"SIZE=100x12")
dlg = IupDialog(btn,`TITLE="Simulate mouse input", CHILDOFFSET=10x40, SIZE=200x80`)
IupShow(dlg)
Ihandle timer = IupTimer(Icallback("timer_cb"), 250)
if platform()!=JS then -- (just for consistency)
    IupMainLoop()
    IupClose()
end if

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).

(load "@lib/http.l" "@lib/scrape.l")

# Connect to the demo app at https://7fach.de/app/
(scrape "7fach.de" 80 "app")

# 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

Output:

12.50

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

PureBasic

This code is Windows only.

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()
Library: AutoWin
; The same function as above, but using AutoWin UserLibray
AW_MouseClick()
Delay(1000)
AW_MouseClick(#PB_MouseButton_Left, 50, 50)

Python

In Windows (GUI can be externally created):

import ctypes

def click():
    ctypes.windll.user32.mouse_event(0x2, 0,0,0,0)    # Mouse LClick Down, 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()


Library: AutoPy
import autopy
import math
import time
import random

TWO_PI = math.pi * 2.0


def sine_mouse_wave():
    """
    Moves the mouse in a sine wave from the left edge of the screen to 
    the right.
    """
    width, height = autopy.screen.get_size()
    height /= 2
    height -= 10  # Stay in the screen bounds.

    for x in xrange(width):
        y = int(height * math.sin((TWO_PI * x) / width) + height)
        autopy.mouse.move(x, y)
        time.sleep(random.uniform(0.001, 0.003))

sine_mouse_wave()
Library: PyAutoGUI
import pyautogui

pyautogui.moveTo(100, 200)      # moves mouse to X of 100, Y of 200.
pyautogui.moveTo(None, 500)     # moves mouse to X of 100, Y of 500.
pyautogui.moveTo(600, None)     # moves mouse to X of 600, Y of 500.
pyautogui.moveTo(100, 200, 2)   # moves mouse to X of 100, Y of 200 over 2 seconds

pyautogui.moveRel(0, 50)        # move the mouse down 50 pixels.
pyautogui.moveRel(-30, 0)       # move the mouse left 30 pixels.

pyautogui.click()                          # Left button click on current position
pyautogui.click(clicks=2)
pyautogui.click(clicks=2, interval=0.25)   # with a quarter second pause in between clicks

pyautogui.click(10, 5)                     # Mouse left button click, x=10, y=5
pyautogui.click(200, 250, button='right')  # Mouse right button click, x=200, y=250

pyautogui.scroll(10)   # scroll up 10 "clicks"
pyautogui.scroll(10, x=100, y=100)  # move mouse cursor to 100, 200, then scroll up 10 "clicks"

Racket

Translation of: Python

Same as the Python entry: use a User32 function to simulate a mouse click.

#lang at-exp racket

(require ffi/unsafe)

(define mouse-event
  (get-ffi-obj "mouse_event" (ffi-lib "user32")
               (_fun _int32 _int32 _int32 _int32 _pointer -> _void)))

(mouse-event #x2 0 0 0 #f)
(mouse-event #x4 0 0 0 #f)

Raku

(formerly Perl 6)

Works with: Rakudo version 2018.12

Using bindings to libxdo so any window managed by an X11 display server can receive mouse events.

use X11::libxdo;
my $xdo = Xdo.new;

my ($dw, $dh) = $xdo.get-desktop-dimensions( 0 );

sleep .25;

for ^$dw -> $mx {
    my $my = (($mx / $dh * τ).sin * 500).abs.Int + 200;
    $xdo.move-mouse( $mx, $my, 0 );
    my ($x, $y, $window-id, $screen) = $xdo.get-mouse-info;
    my $name = (try $xdo.get-window-name($window-id) if $window-id)
       // 'No name set';

    my $line = "Mouse location: x=$x : y=$y\nWindow under mouse:\nWindow ID: " ~
       $window-id ~ "\nWindow name: " ~ $name ~ "\nScreen #: $screen";

    print "\e[H\e[J", $line;
    sleep .001;
}

say '';

#`[ There are several available routines controlling mouse position and button events.

.move-mouse( $x, $y, $screen ) # Move the mouse to a specific location.

.move-mouse-relative( $delta-x, $delta-y ) # Move the mouse relative to it's current position.

.move-mouse-relative-to-window( $x, $y, $window ) # Move the mouse to a specific location relative to the top-left corner of a window.

.get-mouse-location() # Get the current mouse location (coordinates and screen ID number).

.get-mouse-info() # Get all mouse location-related data.

.wait-for-mouse-to-move-from( $origin-x, $origin-y ) # Wait for the mouse to move from a location.

.wait-for-mouse-to-move-to( $dest-x, $dest-y ) # Wait for the mouse to move to a location.

.mouse-button-down( $window, $button ) # Send a mouse press (aka mouse down) for a given button at the current mouse location.

.mouse-button-up( $window, $button ) # Send a mouse release (aka mouse up) for a given button at the current mouse location.

.mouse-button-click( $window, $button ) # Send a click for 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.
]

Ring

# Project : Simulate input/Mouse

load "guilib.ring"
load "stdlib.ring"

paint = null

new qapp 
        {
        win1 = new qwidget() {
                  setwindowtitle("")
                  setgeometry(100,100,800,600)
                  setwindowtitle("Mouse events")

                  line1 = new qlineedit(win1) {
                              setgeometry(150,450,300,30)
                              settext("")}

                  line2 = new qlineedit(win1) {
                              setgeometry(150,400,300,30)
                              settext("")}

                  new qpushbutton(win1) {
                          setgeometry(150,500,300,30)
                          settext("draw")
                          myfilter = new qallevents(win1)
                          myfilter.setMouseButtonPressevent("drawpress()")
                          myfilter.setMouseButtonReleaseevent("drawrelease()")
                          installeventfilter(myfilter)
                  }
                  show()
        }
        exec()
        }

func drawpress()
        line2.settext("")
        line1.settext("Mouse was pressed")

func drawrelease()
        line1.settext("")
        line2.settext("Mouse was released")

Output:

https://www.dropbox.com/s/kvm9s8qesaufyej/MouseEvents.jpg?dl=0

Rust

Works with: Rust version 1.25+
Library: AutoPilot
extern crate autopilot;
extern crate rand;
use rand::Rng;

// Moves the mouse in a sine wave across the screen.
const TWO_PI: f64 = std::f64::consts::PI * 2.0;
fn sine_mouse_wave() -> Result<(), autopilot::mouse::MouseError> {
    let screen_size = autopilot::screen::size();
    let scoped_height = screen_size.height / 2.0 - 10.0; // Stay in screen bounds.
    for x in 0..screen_size.width as u64 {
        let y = (scoped_height * ((TWO_PI * x as f64) / screen_size.width).sin() + scoped_height)
            .round();
        let duration: u64 = rand::thread_rng().gen_range(1, 3);
        try!(autopilot::mouse::move_to(autopilot::geometry::Point::new(
            x as f64,
            y as f64
        )));
        std::thread::sleep(std::time::Duration::from_millis(duration));
    }
    Ok(())
}

fn main() {
    sine_mouse_wave().expect("Unable to move mouse");
}

Scala

Library: 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)

Tcl

Within an Application

Library: Tk
# 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

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:

.okBtn invoke

Wren

Library: Xlib


The following program - an embedded Wren application using a C host to communicate with Xlib - reuses most of the code from the Simulate input/Keyboard#Wren task.

However, a mouse click here switches the background color of the window from white to green and vice versa. Pressing the 'a' key simulates a mouse click to achieve the safe effect.

Xlib can of course work with any X server, local or remote.

/* Simulate_input_Mouse.wren */

var KeyPressMask    = 1 << 0
var ButtonPressMask = 1 << 2
var Button1Mask     = 1 << 8
var ExposureMask    = 1 << 15
var Button1         = 1
var KeyPress        = 2
var ButtonPress     = 4
var Expose          = 12
var ClientMessage   = 33

var XK_a            = 0x0061
var XK_q            = 0x0071
var XK_Escape       = 0xff1b

foreign class XGC {
    construct default(display, screenNumber) {}
}

foreign class XColormap {
    construct default(display, screenNumber) {}
}

foreign class XColor {
    construct new() {}

    foreign pixel
}

// XEvent is a C union, not a struct, so we amalgamate the properties
foreign class XEvent {
    construct new() {}        // creates the union and returns a pointer to it

    foreign eventType         // gets type field, common to all union members

    foreign eventType=(et)    // sets type field

    foreign button            // gets xbutton.button

    foreign button=(b)        // sets xbutton.button

    foreign state=(st)        // sets xbutton.state

    foreign sameScreen=(ss)   // sets xbutton.same_screen

    foreign dataL             // gets xclient.data.l (data is a union)
}

foreign class XDisplay {
    construct openDisplay(displayName) {}

    foreign defaultScreen()

    foreign rootWindow(screenNumber)

    foreign blackPixel(screenNumber)

    foreign whitePixel(screenNumber)

    foreign selectInput(w, eventMask)

    foreign mapWindow(w)

    foreign closeDisplay()

    foreign nextEvent(eventReturn)

    foreign createSimpleWindow(parent, x, y, width, height, borderWidth, border, background)

    foreign drawString(d, gc, x, y, string, length)

    foreign storeName(w, windowName)

    foreign flush()

    foreign internAtom(atomName, onlyIfExists)

    foreign setWMProtocols(w, protocols, count)

    foreign sendEvent(w, propogate, eventMask, eventSend)

    foreign destroyWindow(w)

    foreign parseColor(colormap, spec, exactDefReturn)

    foreign allocColor(colormap, screenInOut)

    foreign setWindowBackground(w, backgroundPixel)

    foreign clearArea(w, x, y, width, height, exposures)
}

class X {
   foreign static lookupKeysym(keyEvent, index)
}

/* open connection with the server */
var xd = XDisplay.openDisplay("")
if (xd == 0) {
    System.print("Cannot open display.")
    return
}
var s = xd.defaultScreen()

/* create window */
var w = xd.createSimpleWindow(xd.rootWindow(s), 10, 10, 350, 250, 1, xd.blackPixel(s), xd.whitePixel(s))
xd.storeName(w, "Simulate mouse press")
var white = true // current background color

/* select kind of events we are interested in */
xd.selectInput(w, ExposureMask | KeyPressMask | ButtonPressMask)

/* map (show) the window */
xd.mapWindow(w)
xd.flush()

/* default graphics context */
var gc = XGC.default(xd, s)

/* connect the close button in the window handle */
var wmDeleteWindow = xd.internAtom("WM_DELETE_WINDOW", true)
xd.setWMProtocols(w, [wmDeleteWindow], 1)

/* create color green */
var green = XColor.new()
var colormap = XColormap.default(xd, s)
xd.parseColor(colormap, "#00FF00", green)
xd.allocColor(colormap, green)

/* event loop */
var e = XEvent.new()
while (true) {
    xd.nextEvent(e)
    var et = e.eventType
    if (et == Expose) {
        /* draw or redraw the window */
        var msg1 = "Press the 'a' key to switch the background color"
        var msg2 = "between white and green."
        xd.drawString(w, gc, 10, 20, msg1, msg1.count)
        xd.drawString(w, gc, 10, 35, msg2, msg2.count)
    } else if (et == ButtonPress) {
        System.write("\nButtonPress event received, ")
        if (white) {
            System.print("switching from white to green")
            xd.setWindowBackground(w, green.pixel)
        } else {
            System.print("switching from green to white")
            xd.setWindowBackground(w, xd.whitePixel(s))
        }
        xd.clearArea(w, 0, 0, 0, 0, true)
        white = !white
    } else if (et == ClientMessage) {
        /* delete window event */
         if (e.dataL[0] == wmDeleteWindow) break
    } else if (et == KeyPress) {
        System.print("\nKeyPress event received")
        /* exit if q or escape are pressed */
        var keysym = X.lookupKeysym(e, 0)
        if (keysym == XK_q || keysym == XK_Escape) {
            break
        } else if (keysym == XK_a) {
            /* if a is pressed, manufacture a ButtonPress event and send it to the window */
            System.print("> Key 'a' pressed, sending ButtonPress event")
            var e2 = XEvent.new()
            e2.eventType = ButtonPress
            e2.state = Button1Mask
            e2.button = Button1
            e2.sameScreen = true
            xd.sendEvent(w, true, ButtonPressMask, e2)
        } else {
            System.print("> Key other than 'a' pressed, not processed")
        }
    }
}

xd.destroyWindow(w)

/* close connection to server */
xd.closeDisplay()


We now embed this Wren script in the following C program, compile and run it.

/* gcc Simulate_input_Mouse.c -o Simulate_input_Mouse -lX11 -lwren -lm */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "wren.h"

/* C <=> Wren interface functions */

void C_displayAllocate(WrenVM* vm) {
    Display** pdisplay = (Display**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(Display*));
    const char *displayName = wrenGetSlotString(vm, 1);
    if (displayName == "") {
        *pdisplay = XOpenDisplay(NULL);
    } else {
        *pdisplay = XOpenDisplay(displayName);
    }
}

void C_gcAllocate(WrenVM* vm) {
    GC *pgc = (GC *)wrenSetSlotNewForeign(vm, 0, 0, sizeof(GC));
    Display* display = *(Display**)wrenGetSlotForeign(vm, 1);
    int s = (int)wrenGetSlotDouble(vm, 2);
    *pgc = DefaultGC(display, s);
}

void C_eventAllocate(WrenVM* vm) {
    wrenSetSlotNewForeign(vm, 0, 0, sizeof(XEvent));
}

void C_colormapAllocate(WrenVM* vm) {
    Colormap *pcm = (Colormap *)wrenSetSlotNewForeign(vm, 0, 0, sizeof(Colormap));
    Display* display = *(Display**)wrenGetSlotForeign(vm, 1);
    int s = (int)wrenGetSlotDouble(vm, 2);
    *pcm = DefaultColormap(display, s);
}

void C_colorAllocate(WrenVM* vm) {
     wrenSetSlotNewForeign(vm, 0, 0, sizeof(XColor));
}

void C_eventType(WrenVM* vm) {
    XEvent e = *(XEvent *)wrenGetSlotForeign(vm, 0);
    wrenSetSlotDouble(vm, 0, (double)e.type);
}

void C_setEventType(WrenVM* vm) {
    XEvent *e = (XEvent *)wrenGetSlotForeign(vm, 0);
    int type = (int)wrenGetSlotDouble(vm, 1);
    e->type = type;
}

void C_button(WrenVM* vm) {
    XButtonEvent be = *(XButtonEvent *)wrenGetSlotForeign(vm, 0);
    wrenSetSlotDouble(vm, 0, (double)be.button);
}

void C_setButton(WrenVM* vm) {
    XButtonEvent *be = (XButtonEvent *)wrenGetSlotForeign(vm, 0);
    unsigned int button = (unsigned int)wrenGetSlotDouble(vm, 1);
    be->button = button;
}

void C_setState(WrenVM* vm) {
    XButtonEvent *be = (XButtonEvent *)wrenGetSlotForeign(vm, 0);
    unsigned int state = (unsigned int)wrenGetSlotDouble(vm, 1);
    be->state = state;
}

void C_setSameScreen(WrenVM* vm) {
    XButtonEvent *be = (XButtonEvent *)wrenGetSlotForeign(vm, 0);
    Bool sameScreen = (Bool)wrenGetSlotBool(vm, 1);
    be->same_screen = sameScreen;
}

void C_pixel(WrenVM* vm) {
    XColor c = *(XColor *)wrenGetSlotForeign(vm, 0);
    wrenSetSlotDouble(vm, 0, (double)c.pixel);
}   

void C_dataL(WrenVM* vm) {
    XClientMessageEvent cme = *(XClientMessageEvent *)wrenGetSlotForeign(vm, 0);
    wrenEnsureSlots(vm, 2);
    wrenSetSlotNewList(vm, 0);
    int i;
    for (i = 0; i < 5; ++i) {
        wrenSetSlotDouble(vm, 1, (double)cme.data.l[i]);
        wrenInsertInList(vm, 0, i, 1);
    }
}

void C_defaultScreen(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    int screenNumber = DefaultScreen(display);
    wrenSetSlotDouble(vm, 0, (double)screenNumber);
}

void C_rootWindow(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    int screenNumber = (int)wrenGetSlotDouble(vm, 1);
    Window w = RootWindow(display, screenNumber);
    wrenSetSlotDouble(vm, 0, (double)w);
}

void C_blackPixel(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    int screenNumber = (int)wrenGetSlotDouble(vm, 1);
    unsigned long p = BlackPixel(display, screenNumber);
    wrenSetSlotDouble(vm, 0, (double)p);
}

void C_whitePixel(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    int screenNumber = (int)wrenGetSlotDouble(vm, 1);
    unsigned long p = WhitePixel(display, screenNumber);
    wrenSetSlotDouble(vm, 0, (double)p);
}

void C_selectInput(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    long eventMask = (long)wrenGetSlotDouble(vm, 2);
    XSelectInput(display, w, eventMask);
}

void C_mapWindow(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    XMapWindow(display, w);
}

void C_closeDisplay(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    XCloseDisplay(display);
}

void C_nextEvent(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    XEvent* pe = (XEvent*)wrenGetSlotForeign(vm, 1);
    XNextEvent(display, pe);
}

void C_storeName(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    char *windowName = (char *)wrenGetSlotString(vm, 2);
    XStoreName(display, w, windowName);
}

void C_flush(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    XFlush(display);
}

void C_internAtom(WrenVM* vm) {
    Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
    char *atomName = (char *)wrenGetSlotString(vm, 1);
    Bool onlyIfExists = (Bool)wrenGetSlotBool(vm, 2);
    Atom atom = XInternAtom(display, atomName, onlyIfExists);
    wrenSetSlotDouble(vm, 0, (double)atom);
}

void C_setWMProtocols(WrenVM* vm) {
    Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    int count = (int)wrenGetSlotDouble(vm, 3);
    Atom protocols[count];
    int i;
    for (i = 0; i < count; ++i) {
        wrenGetListElement(vm, 2, i, 1);
        protocols[i] = (Atom)wrenGetSlotDouble(vm, 1);
    }
    XSetWMProtocols(display, w, protocols, count);
}

void C_sendEvent(WrenVM* vm) {
    Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    Bool propogate = (Bool)wrenGetSlotBool(vm, 2);
    long eventMask = (long)wrenGetSlotDouble(vm, 3);
    XEvent* pe = (XEvent*)wrenGetSlotForeign(vm, 4);
    XSendEvent(display, w, propogate, eventMask, pe);
}

void C_destroyWindow(WrenVM* vm) {
    Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    XDestroyWindow(display, w);
}

void C_createSimpleWindow(WrenVM* vm) {
    Display *display         = *(Display**)wrenGetSlotForeign(vm, 0);
    Window parent            = (Window)wrenGetSlotDouble(vm, 1);
    int x                    = (int)wrenGetSlotDouble(vm, 2);
    int y                    = (int)wrenGetSlotDouble(vm, 3);
    unsigned int width       = (unsigned int)wrenGetSlotDouble(vm, 4);
    unsigned int height      = (unsigned int)wrenGetSlotDouble(vm, 5);
    unsigned int borderWidth = (unsigned int)wrenGetSlotDouble(vm, 6);
    unsigned long border     = (unsigned long)wrenGetSlotDouble(vm, 7);
    unsigned long background = (unsigned long)wrenGetSlotDouble(vm, 8);
    Window w = XCreateSimpleWindow(display, parent, x, y, width, height, borderWidth, border, background);
    wrenSetSlotDouble(vm, 0, (double)w);
}

void C_drawString(WrenVM* vm) {
    Display *display    = *(Display**)wrenGetSlotForeign(vm, 0);
    Drawable d          = (Drawable)wrenGetSlotDouble(vm, 1);
    GC gc               = *(GC *)wrenGetSlotForeign(vm, 2);
    int x               = (int)wrenGetSlotDouble(vm, 3);
    int y               = (int)wrenGetSlotDouble(vm, 4);
    const char *string  = wrenGetSlotString(vm, 5);
    int length          = (int)wrenGetSlotDouble(vm, 6);
    XDrawString(display, d, gc, x, y, string, length);
}

void C_parseColor(WrenVM* vm) {
    Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
    Colormap cm = *(Colormap *)wrenGetSlotForeign(vm, 1);
    char *spec  = (char *)wrenGetSlotString(vm, 2);
    XColor* pc  = (XColor*)wrenGetSlotForeign(vm, 3);
    XParseColor(display, cm, spec, pc);
}

void C_allocColor(WrenVM* vm) {
    Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
    Colormap cm = *(Colormap *)wrenGetSlotForeign(vm, 1);
    XColor* pc  = (XColor*)wrenGetSlotForeign(vm, 2);
    XAllocColor(display, cm, pc);
}

void C_setWindowBackground(WrenVM* vm) {
    Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w = (Window)wrenGetSlotDouble(vm, 1);
    unsigned long backgroundPixel = (unsigned long)wrenGetSlotDouble(vm, 2);
    XSetWindowBackground(display, w, backgroundPixel);
}

void C_clearArea(WrenVM* vm) {
    Display *display    = *(Display**)wrenGetSlotForeign(vm, 0);
    Window w            = (Window)wrenGetSlotDouble(vm, 1);
    int x               = (int)wrenGetSlotDouble(vm, 2);
    int y               = (int)wrenGetSlotDouble(vm, 3);
    unsigned int width  = (unsigned int)wrenGetSlotDouble(vm, 4);
    unsigned int height = (unsigned int)wrenGetSlotDouble(vm, 5);
    Bool exposures      = (Bool)wrenGetSlotBool(vm, 6);               
    XClearArea(display, w, x, y, width, height, exposures);
}

void C_lookupKeysym(WrenVM* vm) {
    XKeyEvent *pke = (XKeyEvent*)wrenGetSlotForeign(vm, 1);
    int index = (int)wrenGetSlotDouble(vm, 2);
    KeySym k = XLookupKeysym(pke, index);
    wrenSetSlotDouble(vm, 0, (double)k);
}

WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
    WrenForeignClassMethods methods;
    methods.finalize = NULL;
    if (strcmp(className, "XDisplay") == 0) {
        methods.allocate = C_displayAllocate;
    } else if (strcmp(className, "XGC") == 0) {
        methods.allocate = C_gcAllocate;
    } else if (strcmp(className, "XEvent") == 0) {
        methods.allocate = C_eventAllocate;
    } else if (strcmp(className, "XColormap") == 0) {
        methods.allocate = C_colormapAllocate;
    } else if (strcmp(className, "XColor") == 0) {
        methods.allocate = C_colorAllocate;
    } else {
        methods.allocate = NULL;
    }
    return methods;
}

WrenForeignMethodFn bindForeignMethod(
    WrenVM* vm,
    const char* module,
    const char* className,
    bool isStatic,
    const char* signature) {
    if (strcmp(module, "main") == 0) {
        if (strcmp(className, "XEvent") == 0) {
            if (!isStatic && strcmp(signature, "eventType") == 0)             return C_eventType;
            if (!isStatic && strcmp(signature, "eventType=(_)") == 0)         return C_setEventType;
            if (!isStatic && strcmp(signature, "button") == 0)                return C_button;
            if (!isStatic && strcmp(signature, "button=(_)") == 0)            return C_setButton;
            if (!isStatic && strcmp(signature, "state=(_)") == 0)             return C_setState;
            if (!isStatic && strcmp(signature, "sameScreen=(_)") == 0)        return C_setSameScreen;
            if (!isStatic && strcmp(signature, "dataL") == 0)                 return C_dataL;
        } else if (strcmp(className, "XColor") == 0) {
            if (!isStatic && strcmp(signature, "pixel") == 0)                 return C_pixel;
        } else if (strcmp(className, "XDisplay") == 0) {
            if (!isStatic && strcmp(signature, "defaultScreen()") == 0)       return C_defaultScreen;
            if (!isStatic && strcmp(signature, "rootWindow(_)") == 0)         return C_rootWindow;
            if (!isStatic && strcmp(signature, "blackPixel(_)") == 0)         return C_blackPixel;
            if (!isStatic && strcmp(signature, "whitePixel(_)") == 0)         return C_whitePixel;
            if (!isStatic && strcmp(signature, "selectInput(_,_)") == 0)      return C_selectInput;
            if (!isStatic && strcmp(signature, "mapWindow(_)") == 0)          return C_mapWindow;
            if (!isStatic && strcmp(signature, "closeDisplay()") == 0)        return C_closeDisplay;
            if (!isStatic && strcmp(signature, "nextEvent(_)") == 0)          return C_nextEvent;
            if (!isStatic && strcmp(signature, "storeName(_,_)") == 0)        return C_storeName;
            if (!isStatic && strcmp(signature, "flush()") == 0)               return C_flush;
            if (!isStatic && strcmp(signature, "internAtom(_,_)") == 0)       return C_internAtom;
            if (!isStatic && strcmp(signature, "setWMProtocols(_,_,_)") == 0) return C_setWMProtocols;
            if (!isStatic && strcmp(signature, "sendEvent(_,_,_,_)") == 0)    return C_sendEvent;
            if (!isStatic && strcmp(signature, "destroyWindow(_)") == 0)      return C_destroyWindow;
            if (!isStatic && strcmp(signature, "createSimpleWindow(_,_,_,_,_,_,_,_)") == 0) return C_createSimpleWindow;
            if (!isStatic && strcmp(signature, "drawString(_,_,_,_,_,_)") == 0)  return C_drawString;
            if (!isStatic && strcmp(signature, "parseColor(_,_,_)") == 0)        return C_parseColor;
            if (!isStatic && strcmp(signature, "allocColor(_,_)") == 0)          return C_allocColor;
            if (!isStatic && strcmp(signature, "setWindowBackground(_,_)") == 0) return C_setWindowBackground;
            if (!isStatic && strcmp(signature, "clearArea(_,_,_,_,_,_)") == 0)   return C_clearArea;
        } else if (strcmp(className, "X") == 0) {
            if (isStatic && strcmp(signature, "lookupKeysym(_,_)") == 0)         return C_lookupKeysym;
        }
    }
    return NULL;
}

static void writeFn(WrenVM* vm, const char* text) {
    printf("%s", text);
}

void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
    switch (errorType) {
        case WREN_ERROR_COMPILE:
            printf("[%s line %d] [Error] %s\n", module, line, msg);
            break;
        case WREN_ERROR_STACK_TRACE:
            printf("[%s line %d] in %s\n", module, line, msg);
            break;
        case WREN_ERROR_RUNTIME:
            printf("[Runtime Error] %s\n", msg);
            break;
    }
}

char *readFile(const char *fileName) {
    FILE *f = fopen(fileName, "r");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    rewind(f);
    char *script = malloc(fsize + 1);
    fread(script, 1, fsize, f);
    fclose(f);
    script[fsize] = 0;
    return script;
}

int main(int argc, char **argv) {
    WrenConfiguration config;
    wrenInitConfiguration(&config);
    config.writeFn = &writeFn;
    config.errorFn = &errorFn;
    config.bindForeignClassFn = &bindForeignClass;
    config.bindForeignMethodFn = &bindForeignMethod;
    WrenVM* vm = wrenNewVM(&config);
    const char* module = "main";
    const char* fileName = "Simulate_input_Mouse.wren";
    char *script = readFile(fileName);
    WrenInterpretResult result = wrenInterpret(vm, module, script);
    switch (result) {
        case WREN_RESULT_COMPILE_ERROR:
            printf("Compile Error!\n");
            break;
        case WREN_RESULT_RUNTIME_ERROR:
            printf("Runtime Error!\n");
            break;
        case WREN_RESULT_SUCCESS:
            break;
    }
    wrenFreeVM(vm);
    free(script);
    return 0;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.