Mouse position: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 31: Line 31:
=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
This returns the mouse's horizontal position in CX and its vertical position in DX, assuming the mouse has been enabled first.
This returns the mouse's horizontal position in CX and its vertical position in DX, assuming the mouse has been enabled first.
<lang asm>mov ax,3
<syntaxhighlight lang="asm">mov ax,3
int 33h</lang>
int 33h</syntaxhighlight>


=={{header|ActionScript}}==
=={{header|ActionScript}}==
This shows the mouse position in a text field at the bottom-right corner
This shows the mouse position in a text field at the bottom-right corner
and updates when the mouse is moved.
and updates when the mouse is moved.
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
package {
Line 75: Line 75:


}
}
</syntaxhighlight>
</lang>


=={{header|Ada}}==
=={{header|Ada}}==
Line 87: Line 87:
It returns mouse coordinates relatively to a window (internally created).
It returns mouse coordinates relatively to a window (internally created).
The following program shows a button, which when pressed indicates coordinates of the mouse pointer relatively to the main window:
The following program shows a button, which when pressed indicates coordinates of the mouse pointer relatively to the main window:
<lang Ada>with GLib; use GLib;
<syntaxhighlight lang="ada">with GLib; use GLib;
with Gtk.Button; use Gtk.Button;
with Gtk.Button; use Gtk.Button;
with Gtk.Label; use Gtk.Label;
with Gtk.Label; use Gtk.Label;
Line 153: Line 153:
Gtk.Main.Main;
Gtk.Main.Main;
end Tell_Mouse;</lang>
end Tell_Mouse;</syntaxhighlight>


=={{header|AmigaBASIC}}==
=={{header|AmigaBASIC}}==


<lang amigabasic>MOUSE ON
<syntaxhighlight lang="amigabasic">MOUSE ON


WHILE 1
WHILE 1
m=MOUSE(0)
m=MOUSE(0)
PRINT MOUSE(1),MOUSE(2)
PRINT MOUSE(1),MOUSE(2)
WEND</lang>
WEND</syntaxhighlight>


=={{header|AppleScript}}==
=={{header|AppleScript}}==
System Events can report the position of the currently active window. The mouse's location is retrieved with help from AppleScriptObjC. This will be relative to the bottom-left corner of the screen. Therefore, we also retrieve the screen's height.
System Events can report the position of the currently active window. The mouse's location is retrieved with help from AppleScriptObjC. This will be relative to the bottom-left corner of the screen. Therefore, we also retrieve the screen's height.


<lang AppleScript>use framework "AppKit"
<syntaxhighlight lang="applescript">use framework "AppKit"


tell application id "com.apple.SystemEvents" to tell ¬
tell application id "com.apple.SystemEvents" to tell ¬
Line 182: Line 182:
end tell
end tell


log coords</lang>
log coords</syntaxhighlight>


{{out}}
{{out}}
Line 189: Line 189:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
The window may be an externally created window.
The window may be an externally created window.
<lang AutoHotkey>#i:: ; (Optional) Assigns a hotkey to display window info, Windows+i
<syntaxhighlight lang="autohotkey">#i:: ; (Optional) Assigns a hotkey to display window info, Windows+i
MouseGetPos, x, y ; Gets x/y pos relative to active window
MouseGetPos, x, y ; Gets x/y pos relative to active window
WinGetActiveTitle, WinTitle ; Gets active window title
WinGetActiveTitle, WinTitle ; Gets active window title
Traytip, Mouse position, x: %x%`ny: %y%`rWindow: %WinTitle%, 4 ; Displays the info as a Traytip for 4 seconds
Traytip, Mouse position, x: %x%`ny: %y%`rWindow: %WinTitle%, 4 ; Displays the info as a Traytip for 4 seconds
return</lang>
return</syntaxhighlight>


===with DllCall===
===with DllCall===
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetCursorPos @github] by jNizM
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetCursorPos @github] by jNizM
<lang AutoHotkey>GetCursorPos()
<syntaxhighlight lang="autohotkey">GetCursorPos()
{
{
static POINT, init := VarSetCapacity(POINT, 8, 0) && NumPut(8, POINT, "Int")
static POINT, init := VarSetCapacity(POINT, 8, 0) && NumPut(8, POINT, "Int")
Line 210: Line 210:
. "POINT structure`n`n"
. "POINT structure`n`n"
. "x-coordinate:`t`t" GetCursorPos[0] "`n"
. "x-coordinate:`t`t" GetCursorPos[0] "`n"
. "y-coordinate:`t`t" GetCursorPos[1]</lang>
. "y-coordinate:`t`t" GetCursorPos[1]</syntaxhighlight>
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetPhysicalCursorPos @github] by jNizM
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetPhysicalCursorPos @github] by jNizM
<lang AutoHotkey>GetPhysicalCursorPos()
<syntaxhighlight lang="autohotkey">GetPhysicalCursorPos()
{
{
static POINT, init := VarSetCapacity(POINT, 8, 0) && NumPut(8, POINT, "Int")
static POINT, init := VarSetCapacity(POINT, 8, 0) && NumPut(8, POINT, "Int")
Line 225: Line 225:
. "POINT structure`n`n"
. "POINT structure`n`n"
. "x-coordinate:`t`t" GetPhysicalCursorPos[0] "`n"
. "x-coordinate:`t`t" GetPhysicalCursorPos[0] "`n"
. "y-coordinate:`t`t" GetPhysicalCursorPos[1]</lang>
. "y-coordinate:`t`t" GetPhysicalCursorPos[1]</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
The mouse coordinates are relative to the bottom-left corner
The mouse coordinates are relative to the bottom-left corner
of the BBC BASIC main output window:
of the BBC BASIC main output window:
<lang bbcbasic> MOUSE xmouse%, ymouse%, buttons%
<syntaxhighlight lang="bbcbasic"> MOUSE xmouse%, ymouse%, buttons%
PRINT xmouse%, ymouse%</lang>
PRINT xmouse%, ymouse%</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
{{libheader|Xlib}}
{{libheader|Xlib}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xlib.h>


Line 271: Line 271:
(void)XCloseDisplay(d); /* and close the display */
(void)XCloseDisplay(d); /* and close the display */
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|c_sharp|C#}}==
=={{header|c_sharp|C#}}==
Writes the absolute Mouse Position of the Screen into the Console
Writes the absolute Mouse Position of the Screen into the Console
<lang csharp>
<syntaxhighlight lang="csharp">
using System;
using System;
using System.Windows.Forms;
using System.Windows.Forms;
Line 287: Line 287:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(let [point (.. java.awt.MouseInfo getPointerInfo getLocation)] [(.getX point) (.getY point)])</lang>
<syntaxhighlight lang="lisp">(let [point (.. java.awt.MouseInfo getPointerInfo getLocation)] [(.getX point) (.getY point)])</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 296: Line 296:
With the ltk library.
With the ltk library.


<lang lisp>
<syntaxhighlight lang="lisp">
(ql:quickload "ltk")
(ql:quickload "ltk")
(in-package :ltk-user)
(in-package :ltk-user)
Line 305: Line 305:
;; create a small window. Enter the mouse to see lots of events.
;; create a small window. Enter the mouse to see lots of events.
(bind *tk* "<Motion>" #'motion))
(bind *tk* "<Motion>" #'motion))
</syntaxhighlight>
</lang>
This prints a lot of events of the form
This prints a lot of events of the form


Line 325: Line 325:
Shows mouse-position on a label on the form.
Shows mouse-position on a label on the form.


<lang Delphi>procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
<syntaxhighlight lang="delphi">procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
Y: Integer);
begin
begin
lblMousePosition.Caption := ('X:' + IntToStr(X) + ', Y:' + IntToStr(Y));
lblMousePosition.Caption := ('X:' + IntToStr(X) + ', Y:' + IntToStr(Y));
end;</lang>
end;</syntaxhighlight>




Line 337: Line 337:
with the help of Windows and classes units.
with the help of Windows and classes units.


<lang Delphi>program Project1;
<syntaxhighlight lang="delphi">program Project1;
{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
uses
uses
Line 351: Line 351:
sleep(300);
sleep(300);
end
end
end.</lang>
end.</syntaxhighlight>


=={{header|EasyLang}}==
=={{header|EasyLang}}==
Line 357: Line 357:
[https://easylang.online/ide/#run=on%20mouse_move%0A%20%20clear%0A%20%20text%20mouse_x%20%26%20%22%20%22%20%26%20mouse_y%0A.%0A Run it]
[https://easylang.online/ide/#run=on%20mouse_move%0A%20%20clear%0A%20%20text%20mouse_x%20%26%20%22%20%22%20%26%20mouse_y%0A.%0A Run it]


<lang>on mouse_move
<syntaxhighlight lang="text">on mouse_move
clear
clear
text mouse_x & " " & mouse_y
text mouse_x & " " & mouse_y
.</lang>
.</syntaxhighlight>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
When the '''plot''' library is loaded, the mouse position - relative to the plotting area coordinates, is displayed inside the info panel.
When the '''plot''' library is loaded, the mouse position - relative to the plotting area coordinates, is displayed inside the info panel.
<lang lisp>
<syntaxhighlight lang="lisp">
(lib 'plot)
(lib 'plot)
(plot-x-minmax 10) ; set logical dimensions of plotting area
(plot-x-minmax 10) ; set logical dimensions of plotting area
Line 372: Line 372:
;; the mouse position is displayed as , for example, [ x: 5.6 y : 88.7]
;; the mouse position is displayed as , for example, [ x: 5.6 y : 88.7]
;; 0 <= x <= 10, 0 <= y <= 100
;; 0 <= x <= 10, 0 <= y <= 100
</syntaxhighlight>
</lang>


--[[User:Neo.abhinav|Neo.abhinav]] 17:00, 6 May 2011 (UTC)
--[[User:Neo.abhinav|Neo.abhinav]] 17:00, 6 May 2011 (UTC)


=={{header|Elm}}==
=={{header|Elm}}==
<lang elm>import Graphics.Element exposing (Element, show)
<syntaxhighlight lang="elm">import Graphics.Element exposing (Element, show)
import Mouse
import Mouse


Line 383: Line 383:
main : Signal Element
main : Signal Element
main =
main =
Signal.map show Mouse.position</lang>
Signal.map show Mouse.position</syntaxhighlight>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
Line 389: Line 389:
the frame the mouse is over and where the mouse is within that frame can be obtained with
the frame the mouse is over and where the mouse is within that frame can be obtained with


<lang Lisp>(mouse-pixel-position)
<syntaxhighlight lang="lisp">(mouse-pixel-position)
;; => (FRAME . (X . Y))</lang>
;; => (FRAME . (X . Y))</syntaxhighlight>


Or <code>mouse-position</code> for characters instead of pixels.
Or <code>mouse-position</code> for characters instead of pixels.
Line 400: Line 400:
=={{header|ERRE}}==
=={{header|ERRE}}==
This example program, taken from distribution disk, shows the mouse position in a text field at the bottom-right corner and updates when the mouse is moved.
This example program, taken from distribution disk, shows the mouse position in a text field at the bottom-right corner and updates when the mouse is moved.
<syntaxhighlight lang="erre">
<lang ERRE>
!
!
! MOUSE WITH 'MOUSE.LIB' LIBRARY
! MOUSE WITH 'MOUSE.LIB' LIBRARY
Line 480: Line 480:
CLS
CLS
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 487: Line 487:
If the question was for the point relative to the current window,
If the question was for the point relative to the current window,
life would be much simpler.
life would be much simpler.
<lang fsharp>open System.Windows.Forms
<syntaxhighlight lang="fsharp">open System.Windows.Forms
open System.Runtime.InteropServices
open System.Runtime.InteropServices


Line 508: Line 508:
ScreenToClient(hwnd, &ptFs) |> ignore
ScreenToClient(hwnd, &ptFs) |> ignore
ptFs
ptFs
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Works only in the graphical listener.
Works only in the graphical listener.
Replaces the text in the button with the relative and absolute coordinates of the mouse
Replaces the text in the button with the relative and absolute coordinates of the mouse
<lang factor>: replace-text ( button text -- )
<syntaxhighlight lang="factor">: replace-text ( button text -- )
[ drop children>> pop drop ] [ >label add-gadget drop ] 2bi ;
[ drop children>> pop drop ] [ >label add-gadget drop ] 2bi ;
: present-locations ( loc1 loc2 -- string )
: present-locations ( loc1 loc2 -- string )
Line 526: Line 526:
]
]
<border-button> gadget. ;
<border-button> gadget. ;
</syntaxhighlight>
</lang>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Also includes the state of the mouse wheel and mouse buttons, since these are provided by the same subroutine that provides the cursor position.
Also includes the state of the mouse wheel and mouse buttons, since these are provided by the same subroutine that provides the cursor position.


<lang freebasic>
<syntaxhighlight lang="freebasic">
type MOUSE
type MOUSE
'mouse position, button state, wheel state
'mouse position, button state, wheel state
Line 559: Line 559:
print "Wheel scrolls: ", mouse.wheel," "
print "Wheel scrolls: ", mouse.wheel," "
end if
end if
wend</lang>
wend</syntaxhighlight>


=={{header|Gambas}}==
=={{header|Gambas}}==
Line 568: Line 568:
this is the only widget that can track pointer movement within gambas.
this is the only widget that can track pointer movement within gambas.


<lang gambas>
<syntaxhighlight lang="gambas">
PUBLIC SUB Form1_MouseMove()
PUBLIC SUB Form1_MouseMove()
PRINT mouse.X
PRINT mouse.X
PRINT Mouse.Y
PRINT Mouse.Y
END
END
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
Line 579: Line 579:
<br>
<br>
The active window may be externally created.
The active window may be externally created.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 613: Line 613:
fmt.Println("Problem when finding PID(s) of", name)
fmt.Println("Problem when finding PID(s) of", name)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 633: Line 633:
=={{header|Groovy}}==
=={{header|Groovy}}==
Based on Java solution:
Based on Java solution:
<lang groovy>1.upto(5) {
<syntaxhighlight lang="groovy">1.upto(5) {
Thread.sleep(1000)
Thread.sleep(1000)
def p = java.awt.MouseInfo.pointerInfo.location
def p = java.awt.MouseInfo.pointerInfo.location
println "${it}: x=${p.@x} y=${p.@y}"
println "${it}: x=${p.@x} y=${p.@y}"
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 649: Line 649:
Mouse click positions for windows created internally.
Mouse click positions for windows created internally.
X and Y are in units of current xy axes (optional: invisible).
X and Y are in units of current xy axes (optional: invisible).
<lang hicest> WINDOW(WINdowhandle=handle)
<syntaxhighlight lang="hicest"> WINDOW(WINdowhandle=handle)
AXIS(WINdowhandle=handle, MouSeCall=Mouse_Callback, MouSeX=X, MouSeY=Y)
AXIS(WINdowhandle=handle, MouSeCall=Mouse_Callback, MouSeX=X, MouSeY=Y)
END
END
Line 655: Line 655:
SUBROUTINE Mouse_Callback()
SUBROUTINE Mouse_Callback()
WRITE(Messagebox, Name) X, Y
WRITE(Messagebox, Name) X, Y
END</lang>
END</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
In Icon/Unicon the mouse position may be tracked between button presses for any window created by the program.
In Icon/Unicon the mouse position may be tracked between button presses for any window created by the program.
The following code snippet taken from the Icon Graphics Book on page 197-198 shows how to track the mouse.
The following code snippet taken from the Icon Graphics Book on page 197-198 shows how to track the mouse.
<lang Icon>until *Pending() > 0 & Event() == "q" do { # loop until there is something to do
<syntaxhighlight lang="icon">until *Pending() > 0 & Event() == "q" do { # loop until there is something to do
px := WAttrib("pointerx")
px := WAttrib("pointerx")
py := WAttrib("pointery")
py := WAttrib("pointery")
Line 666: Line 666:
WDelay(5) # wait and share processor
WDelay(5) # wait and share processor
}
}
</syntaxhighlight>
</lang>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
The mouse position can be checked at any time by calling
The mouse position can be checked at any time by calling
<lang java5>Point mouseLocation = MouseInfo.getPointerInfo().getLocation();</lang>
<syntaxhighlight lang="java5">Point mouseLocation = MouseInfo.getPointerInfo().getLocation();</syntaxhighlight>
This returns a point on the entire screen, rather than relative to a particular window. This call can be combined with <code>getLocation()</code> from any <code>Component</code> (including a <code>Frame</code>, which is a window) to get the location relative to that <code>Component</code>.
This returns a point on the entire screen, rather than relative to a particular window. This call can be combined with <code>getLocation()</code> from any <code>Component</code> (including a <code>Frame</code>, which is a window) to get the location relative to that <code>Component</code>.


Line 681: Line 681:
One of many ways to add an event listener:
One of many ways to add an event listener:


<lang javascript>document.addEventListener('mousemove', function(e){
<syntaxhighlight lang="javascript">document.addEventListener('mousemove', function(e){
var position = { x: e.clientX, y: e.clientY }
var position = { x: e.clientX, y: e.clientY }
}</lang>
}</syntaxhighlight>


In the above example, the window may not be external.
In the above example, the window may not be external.
Line 689: Line 689:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Gtk
<syntaxhighlight lang="julia">using Gtk


const win = GtkWindow("Get Mouse Position", 600, 800)
const win = GtkWindow("Get Mouse Position", 600, 800)
Line 704: Line 704:
signal_connect(endit, win, :destroy)
signal_connect(endit, win, :destroy)
wait(c)
wait(c)
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Groovy}}
{{trans|Groovy}}
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


import java.awt.MouseInfo
import java.awt.MouseInfo
Line 718: Line 718:
println("${it}: x = ${"%-4d".format(p.x)} y = ${"%-4d".format(p.y)}")
println("${it}: x = ${"%-4d".format(p.x)} y = ${"%-4d".format(p.y)}")
}
}
}</lang>
}</syntaxhighlight>
Sample output:
Sample output:
{{out}}
{{out}}
Line 732: Line 732:
This example gets the mouse position based on the active window.
This example gets the mouse position based on the active window.
Click on other windows to get relative mouse position based on those windows.
Click on other windows to get relative mouse position based on those windows.
<lang lb> nomainwin
<syntaxhighlight lang="lb"> nomainwin


UpperLeftX = DisplayWidth-WindowWidth
UpperLeftX = DisplayWidth-WindowWidth
Line 769: Line 769:
y = point.y.struct
y = point.y.struct
CursorPos$=x; ",";y
CursorPos$=x; ",";y
end function</lang>
end function</syntaxhighlight>


=={{header|Lingo}}==
=={{header|Lingo}}==
<lang lingo>put _mouse.mouseLoc
<syntaxhighlight lang="lingo">put _mouse.mouseLoc
-- point(310, 199)</lang>
-- point(310, 199)</syntaxhighlight>


=={{header|xTalk}}==
=={{header|xTalk}}==
{{works with|HyperCard}} {{works with|LiveCode}}
{{works with|HyperCard}} {{works with|LiveCode}}
<syntaxhighlight lang="livecode">
<lang liveCode>
-- Method 1:
-- Method 1:
-- this script in either the stack script or the card script to get position relative to the current stack window
-- this script in either the stack script or the card script to get position relative to the current stack window
Line 803: Line 803:
148,521 -- relative to current screen
148,521 -- relative to current screen


</syntaxhighlight>
</lang>


=={{header|LiveCode Builder}}==
=={{header|LiveCode Builder}}==
<syntaxhighlight lang="livecode builder">
<lang LiveCode Builder>
LiveCode Builder (LCB) is a slightly lower level, strictly typed variant of LiveCode Script (LCS) used for making add-on extensions to LiveCode Script
LiveCode Builder (LCB) is a slightly lower level, strictly typed variant of LiveCode Script (LCS) used for making add-on extensions to LiveCode Script


Line 819: Line 819:
log "mouse position is within the widget bounds"
log "mouse position is within the widget bounds"
end if
end if
</syntaxhighlight>
</lang>


=={{header|Logo}}==
=={{header|Logo}}==
<lang logo>show mousepos ; [-250 250]</lang>
<syntaxhighlight lang="logo">show mousepos ; [-250 250]</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Checkit {
\\ works when console is the active window
\\ works when console is the active window
Line 858: Line 858:
}
}
CheckIt
CheckIt
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>MousePosition["WindowAbsolute"]</lang>
<syntaxhighlight lang="mathematica">MousePosition["WindowAbsolute"]</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
<lang MATLAB>get(0,'PointerLocation')</lang>
<syntaxhighlight lang="matlab">get(0,'PointerLocation')</syntaxhighlight>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Creates a window on the screen and shows mouse position as it moves.
Creates a window on the screen and shows mouse position as it moves.


<syntaxhighlight lang="maxscript">
<lang MAXScript>
try destroydialog mousePos catch ()
try destroydialog mousePos catch ()


Line 890: Line 890:


createdialog mousepos
createdialog mousepos
</syntaxhighlight>
</lang>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
{{works with|Mini Micro}}
{{works with|Mini Micro}}
<lang MiniScript>print mouse.x, mouse.y</lang>
<syntaxhighlight lang="miniscript">print mouse.x, mouse.y</syntaxhighlight>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
<lang nanoquery>import Nanoquery.Util.Windows
<syntaxhighlight lang="nanoquery">import Nanoquery.Util.Windows


// a function to handle the mouse moved event
// a function to handle the mouse moved event
Line 908: Line 908:
w.setSize(500, 500)
w.setSize(500, 500)
w.setHandler(w.mouseMoved, mouse_moved)
w.setHandler(w.mouseMoved, mouse_moved)
w.show()</lang>
w.show()</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|gintro}}
{{libheader|gintro}}
<lang Nim>import gintro/[glib, gobject, gtk, gio]
<syntaxhighlight lang="nim">import gintro/[glib, gobject, gtk, gio]
import gintro/gdk except Window
import gintro/gdk except Window


Line 938: Line 938:
let app = newApplication(Application, "Rosetta.MousePosition")
let app = newApplication(Application, "Rosetta.MousePosition")
discard app.connect("activate", activate)
discard app.connect("activate", activate)
discard app.run()</lang>
discard app.run()</syntaxhighlight>


{{out}}
{{out}}
Line 956: Line 956:
=={{header|OCaml}}==
=={{header|OCaml}}==
equivalent to the C example, uses the Xlib binding [http://decapode314.free.fr/ocaml/Xlib/ ocaml-xlib]
equivalent to the C example, uses the Xlib binding [http://decapode314.free.fr/ocaml/Xlib/ ocaml-xlib]
<lang OCaml>open Xlib
<syntaxhighlight lang="ocaml">open Xlib


let () =
let () =
Line 979: Line 979:


xCloseDisplay d;
xCloseDisplay d;
;;</lang>
;;</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
To get the X,Y coordinates of N mouse clicks in the current figure,
To get the X,Y coordinates of N mouse clicks in the current figure,
one can use this:
one can use this:
<lang Octave>[X, Y, BUTTONS] = ginput(N);</lang>
<syntaxhighlight lang="octave">[X, Y, BUTTONS] = ginput(N);</syntaxhighlight>
Example:
Example:
<pre>>> [X, Y, BUTTONS] = ginput(4)
<pre>>> [X, Y, BUTTONS] = ginput(4)
Line 1,011: Line 1,011:
=={{header|Oz}}==
=={{header|Oz}}==
Repeatedly shows the mouse coordinates relative to the foremost window of the application.
Repeatedly shows the mouse coordinates relative to the foremost window of the application.
<lang oz>declare
<syntaxhighlight lang="oz">declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
WindowClosed = {NewCell false}
WindowClosed = {NewCell false}
Line 1,031: Line 1,031:
#", y: "#(Winfo.pointery - Winfo.rooty))}
#", y: "#(Winfo.pointery - Winfo.rooty))}
{Delay 250}
{Delay 250}
end</lang>
end</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
Line 1,038: Line 1,038:
When you move the mouse over the created window,
When you move the mouse over the created window,
the mouse position get printed and the program terminates.
the mouse position get printed and the program terminates.
<lang Perl>use SDL;
<syntaxhighlight lang="perl">use SDL;
use SDL::Events;
use SDL::Events;
use SDLx::App;
use SDLx::App;
Line 1,051: Line 1,051:
} );
} );
$app->run;
$app->run;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>x=15 y=304</pre>
<pre>x=15 y=304</pre>
Line 1,062: Line 1,062:
Note that canvasmotion coordinates are relative to the top left of the canvas, whereas the other two are absolute.
Note that canvasmotion coordinates are relative to the top left of the canvas, whereas the other two are absolute.
{{libheader|Phix/pGUI}}
{{libheader|Phix/pGUI}}
<lang Phix>-- demo\rosetta\Mouse_position.exw
<syntaxhighlight lang="phix">-- demo\rosetta\Mouse_position.exw
include pGUI.e
include pGUI.e


Line 1,120: Line 1,120:
IupClose()
IupClose()
end procedure
end procedure
main()</lang>
main()</syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 1,128: Line 1,128:
The returned value is (X . Y), where X is the
The returned value is (X . Y), where X is the
column and Y the line number.
column and Y the line number.
<lang PicoLisp>(de mousePosition ()
<syntaxhighlight lang="picolisp">(de mousePosition ()
(prog2
(prog2
(prin "^[[?9h") # Mouse reporting on
(prin "^[[?9h") # Mouse reporting on
Line 1,139: Line 1,139:
(- (char (key)) 32)
(- (char (key)) 32)
(- (char (key)) 32) ) )
(- (char (key)) 32) ) )
(prin "^[[?9l") ) ) # Mouse reporting off</lang>
(prin "^[[?9l") ) ) # Mouse reporting off</syntaxhighlight>
{{out}}
{{out}}
<pre>: (mousePosition)
<pre>: (mousePosition)
Line 1,146: Line 1,146:
=={{header|PureBasic}}==
=={{header|PureBasic}}==
The mouse position can be obtained by these two commands:
The mouse position can be obtained by these two commands:
<lang PureBasic>x = WindowMouseX(#MyWindow)
<syntaxhighlight lang="purebasic">x = WindowMouseX(#MyWindow)
y = WindowMouseY(#MyWindow)</lang>
y = WindowMouseY(#MyWindow)</syntaxhighlight>
This example repeatedly shows the mouse coordinates
This example repeatedly shows the mouse coordinates
relative to the window created in the application.
relative to the window created in the application.
<lang PureBasic>#MyWindow = 0
<syntaxhighlight lang="purebasic">#MyWindow = 0
#Label_txt = 0
#Label_txt = 0
#MousePos_txt = 1
#MousePos_txt = 1
Line 1,169: Line 1,169:
SetGadgetText(#MousePos_txt,"(" + Str(x) + "," + Str(y) + ")")
SetGadgetText(#MousePos_txt,"(" + Str(x) + "," + Str(y) + ")")
ForEver
ForEver
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Processing}}==
=={{header|Processing}}==
<lang java>void setup(){
<syntaxhighlight lang="java">void setup(){
size(640, 480);
size(640, 480);
}
}
Line 1,180: Line 1,180:
ellipse(mouseX, mouseY, 5, 5); // graphic output example
ellipse(mouseX, mouseY, 5, 5); // graphic output example
println("x:" + mouseX + " y:" + mouseY);
println("x:" + mouseX + " y:" + mouseY);
}</lang>
}</syntaxhighlight>


==={{header|Processing Python mode}}===
==={{header|Processing Python mode}}===
<lang python>def setup():
<syntaxhighlight lang="python">def setup():
size(640, 480)
size(640, 480)


Line 1,189: Line 1,189:
# mouseX and mouseY provide the current mouse position
# mouseX and mouseY provide the current mouse position
ellipse(mouseX, mouseY, 5, 5) # graphic output example
ellipse(mouseX, mouseY, 5, 5) # graphic output example
println("x:{} y:{}".format(mouseX, mouseY))</lang>
println("x:{} y:{}".format(mouseX, mouseY))</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 1,201: Line 1,201:


Code is based on post in Daniweb: http://www.daniweb.com/forums/post616327.html#post616327 by ZZucker
Code is based on post in Daniweb: http://www.daniweb.com/forums/post616327.html#post616327 by ZZucker
<lang python>
<syntaxhighlight lang="python">
import Tkinter as tk
import Tkinter as tk


Line 1,221: Line 1,221:


root.mainloop()
root.mainloop()
</syntaxhighlight>
</lang>
----------------
----------------
<lang python>
<syntaxhighlight lang="python">
#simple way of ,get cursor xy data
#simple way of ,get cursor xy data


Line 1,237: Line 1,237:
win.bind("<Motion>",xy)
win.bind("<Motion>",xy)
mainloop()
mainloop()
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
Line 1,243: Line 1,243:
The mouse position can be queried at any time with a function in a GUI context.
The mouse position can be queried at any time with a function in a GUI context.


<lang racket>
<syntaxhighlight lang="racket">
#lang racket/gui
#lang racket/gui
(define-values [point _] (get-current-mouse-state))
(define-values [point _] (get-current-mouse-state))
(send point get-x)
(send point get-x)
(send point get-y)
(send point get-y)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
{{works with|rakudo/jvm|2013-12-03}}
{{works with|rakudo/jvm|2013-12-03}}
<lang perl6>use java::awt::MouseInfo:from<java>;
<syntaxhighlight lang="raku" line>use java::awt::MouseInfo:from<java>;


given MouseInfo.getPointerInfo.getLocation {
given MouseInfo.getPointerInfo.getLocation {
say .getX, 'x', .getY;
say .getX, 'x', .getY;
}</lang>
}</syntaxhighlight>


An implementation that will work on any X11 windowing system. Reports mouse position, window ID# and window name for whichever window the mouse pointer is over. Automatically moves mouse for hands-off testing purposes.
An implementation that will work on any X11 windowing system. Reports mouse position, window ID# and window name for whichever window the mouse pointer is over. Automatically moves mouse for hands-off testing purposes.


{{works with|Rakudo|2018.11}}
{{works with|Rakudo|2018.11}}
<lang perl6>use X11::libxdo;
<syntaxhighlight lang="raku" line>use X11::libxdo;
my $xdo = Xdo.new;
my $xdo = Xdo.new;


Line 1,283: Line 1,283:
}
}


say '';</lang>
say '';</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
Line 1,290: Line 1,290:
The movement procedure uses the mouse position information
The movement procedure uses the mouse position information


<syntaxhighlight lang="ring">
<lang Ring>
Load "guilib.ring"
Load "guilib.ring"


Line 1,353: Line 1,353:
}
}
ok
ok
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 1,359: Line 1,359:
{{libheader|Shoes}}
{{libheader|Shoes}}


<lang Ruby>Shoes.app(:title => "Mouse Position", :width => 400, :height => 400) do
<syntaxhighlight lang="ruby">Shoes.app(:title => "Mouse Position", :width => 400, :height => 400) do
@position = para "Position : ?, ?", :size => 12, :margin => 10
@position = para "Position : ?, ?", :size => 12, :margin => 10
Line 1,365: Line 1,365:
@position.text = "Position : #{x}, #{y}"
@position.text = "Position : #{x}, #{y}"
end
end
end</lang>
end</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
Prints current location of the mouse cursor relative to any active window.
Prints current location of the mouse cursor relative to any active window.
This example relies on the Windows API.
This example relies on the Windows API.
<lang rust>// rustc 0.9 (7613b15 2014-01-08 18:04:43 -0800)
<syntaxhighlight lang="rust">// rustc 0.9 (7613b15 2014-01-08 18:04:43 -0800)


use std::libc::{BOOL, HANDLE, LONG};
use std::libc::{BOOL, HANDLE, LONG};
Line 1,406: Line 1,406:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>x: 550, y: 614
<pre>x: 550, y: 614
Line 1,418: Line 1,418:
=={{header|Scala}}==
=={{header|Scala}}==


{{libheader|Scala}}<lang scala>import java.awt.MouseInfo
{{libheader|Scala}}<syntaxhighlight lang="scala">import java.awt.MouseInfo
object MousePosition extends App {
object MousePosition extends App {
val mouseLocation = MouseInfo.getPointerInfo.getLocation
val mouseLocation = MouseInfo.getPointerInfo.getLocation
println (mouseLocation)
println (mouseLocation)
}</lang>
}</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==
Line 1,428: Line 1,428:
and [http://seed7.sourceforge.net/libraries/graph.htm#pointerYPos%28in_PRIMITIVE_WINDOW%29 pointerYPos] from the
and [http://seed7.sourceforge.net/libraries/graph.htm#pointerYPos%28in_PRIMITIVE_WINDOW%29 pointerYPos] from the
[http://seed7.sourceforge.net/libraries/graph.htm graph.s7i] library determine the actual X and Y position of the mouse pointer, relative to the given window:
[http://seed7.sourceforge.net/libraries/graph.htm graph.s7i] library determine the actual X and Y position of the mouse pointer, relative to the given window:
<lang seed7>xPos := pointerXPos(curr_win);
<syntaxhighlight lang="seed7">xPos := pointerXPos(curr_win);
yPos := pointerYPos(curr_win);</lang>
yPos := pointerYPos(curr_win);</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Sending the message <tt>position</tt> to the <tt>activeHand</tt> of the <tt>World</tt> returns a <tt>Point</tt> object:
Sending the message <tt>position</tt> to the <tt>activeHand</tt> of the <tt>World</tt> returns a <tt>Point</tt> object:


<lang smalltalk>
<syntaxhighlight lang="smalltalk">
World activeHand position. " (394@649.0)"
World activeHand position. " (394@649.0)"
</syntaxhighlight>
</lang>


=={{header|Standard ML}}==
=={{header|Standard ML}}==
Works with PolyML
Works with PolyML
<lang Standard ML>open XWindows ;
<syntaxhighlight lang="standard ml">open XWindows ;
val dp = XOpenDisplay "" ;
val dp = XOpenDisplay "" ;
val w = XCreateSimpleWindow (RootWindow dp) origin (Area {x=0,y=0,w=400,h=300}) 3 0 0xffffff ;
val w = XCreateSimpleWindow (RootWindow dp) origin (Area {x=0,y=0,w=400,h=300}) 3 0 0xffffff ;
Line 1,448: Line 1,448:
) ;
) ;
XQueryPointer focus ;
XQueryPointer focus ;
XQueryPointer w;</lang>
XQueryPointer w;</syntaxhighlight>
result: position wrt root window and active window, resp wrt root window and window w
result: position wrt root window and active window, resp wrt root window and window w
val it =
val it =
Line 1,461: Line 1,461:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Tk}}
{{libheader|Tk}}
<lang tcl>package require Tk 8.5
<syntaxhighlight lang="tcl">package require Tk 8.5
set curWindow [lindex [wm stackorder .] end]
set curWindow [lindex [wm stackorder .] end]
# Everything below will work with anything from Tk 8.0 onwards
# Everything below will work with anything from Tk 8.0 onwards
set x [expr {[winfo pointerx .] - [winfo rootx $curWindow]}]
set x [expr {[winfo pointerx .] - [winfo rootx $curWindow]}]
set y [expr {[winfo pointery .] - [winfo rooty $curWindow]}]
set y [expr {[winfo pointery .] - [winfo rooty $curWindow]}]
tk_messageBox -message "the mouse is at ($x,$y) in window $curWindow"</lang>
tk_messageBox -message "the mouse is at ($x,$y) in window $curWindow"</syntaxhighlight>




screen coordinates
screen coordinates
<lang tcl>package require Tk
<syntaxhighlight lang="tcl">package require Tk
puts "monitor/display coordinate x = [winfo pointerx .]"</lang>
puts "monitor/display coordinate x = [winfo pointerx .]"</syntaxhighlight>


=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
Line 1,481: Line 1,481:
(so if the pointer is over a button on the current form,
(so if the pointer is over a button on the current form,
the event will only fire for the button, ''not'' the form).
the event will only fire for the button, ''not'' the form).
<lang vb>Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
<syntaxhighlight lang="vb">Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'X and Y are in "twips" -- 15 twips per pixel
'X and Y are in "twips" -- 15 twips per pixel
Me.Print "X:" & X
Me.Print "X:" & X
Me.Print "Y:" & Y
Me.Print "Y:" & Y
End Sub</lang>
End Sub</syntaxhighlight>


The second method uses the [[wp:Windows API|Windows API]],
The second method uses the [[wp:Windows API|Windows API]],
and can be easily translated to any language that can make API calls.
and can be easily translated to any language that can make API calls.
This example uses a <code>Timer</code> control to check the mouse position.
This example uses a <code>Timer</code> control to check the mouse position.
<lang vb>Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
<syntaxhighlight lang="vb">Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


Private Type POINTAPI
Private Type POINTAPI
Line 1,505: Line 1,505:
Me.Print "X:" & Pt.X
Me.Print "X:" & Pt.X
Me.Print "Y:" & Pt.Y
Me.Print "Y:" & Pt.Y
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|DOME}}
<lang ecmascript>import "dome" for Window
<syntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas
import "graphics" for Canvas
import "input" for Mouse
import "input" for Mouse
Line 1,541: Line 1,541:


static draw(alpha) {}
static draw(alpha) {}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,571: Line 1,571:
does not make Windows applications (however see "Simple windowed application").
does not make Windows applications (however see "Simple windowed application").


<lang XPL0>include c:\cxpl\stdlib;
<syntaxhighlight lang="xpl0">include c:\cxpl\stdlib;
if not OpenMouse then Text(0, "A mouse is required")
if not OpenMouse then Text(0, "A mouse is required")
else [ShowMouse(true);
else [ShowMouse(true);
IntOut(0, GetMousePosition(0)); ChOut(0, ^,);
IntOut(0, GetMousePosition(0)); ChOut(0, ^,);
IntOut(0, GetMousePosition(1)); CrLf(0);
IntOut(0, GetMousePosition(1)); CrLf(0);
]</lang>
]</syntaxhighlight>


{{out|Example output}}
{{out|Example output}}