Mouse position: Difference between revisions

m
 
(21 intermediate revisions by 15 users not shown)
Line 28:
Please specify if the window may be externally created.
<br><br>
 
=={{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.
<syntaxhighlight lang="asm">mov ax,3
int 33h</syntaxhighlight>
 
=={{header|ActionScript}}==
This shows the mouse position in a text field at the bottom-right corner
and updates when the mouse is moved.
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
Line 70 ⟶ 75:
 
}
</syntaxhighlight>
</lang>
 
=={{header|Ada}}==
Line 82 ⟶ 87:
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:
<langsyntaxhighlight Adalang="ada">with GLib; use GLib;
with Gtk.Button; use Gtk.Button;
with Gtk.Label; use Gtk.Label;
Line 148 ⟶ 153:
Gtk.Main.Main;
end Tell_Mouse;</langsyntaxhighlight>
 
=={{header|AmigaBASIC}}==
 
<langsyntaxhighlight lang="amigabasic">MOUSE ON
 
WHILE 1
m=MOUSE(0)
PRINT MOUSE(1),MOUSE(2)
WEND</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight AppleScriptlang="applescript">use framework "AppKit"
 
tell application id "com.apple.SystemEvents" to tell ¬
Line 177 ⟶ 182:
end tell
 
log coords</langsyntaxhighlight>
 
{{out}}
Line 184 ⟶ 189:
=={{header|AutoHotkey}}==
The window may be an externally created window.
<langsyntaxhighlight AutoHotkeylang="autohotkey">#i:: ; (Optional) Assigns a hotkey to display window info, Windows+i
MouseGetPos, x, y ; Gets x/y pos relative to active window
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
return</langsyntaxhighlight>
 
===with DllCall===
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetCursorPos @github] by jNizM
<langsyntaxhighlight AutoHotkeylang="autohotkey">GetCursorPos()
{
static POINT, init := VarSetCapacity(POINT, 8, 0) && NumPut(8, POINT, "Int")
Line 205 ⟶ 210:
. "POINT structure`n`n"
. "x-coordinate:`t`t" GetCursorPos[0] "`n"
. "y-coordinate:`t`t" GetCursorPos[1]</langsyntaxhighlight>
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetPhysicalCursorPos @github] by jNizM
<langsyntaxhighlight AutoHotkeylang="autohotkey">GetPhysicalCursorPos()
{
static POINT, init := VarSetCapacity(POINT, 8, 0) && NumPut(8, POINT, "Int")
Line 220 ⟶ 225:
. "POINT structure`n`n"
. "x-coordinate:`t`t" GetPhysicalCursorPos[0] "`n"
. "y-coordinate:`t`t" GetPhysicalCursorPos[1]</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
The mouse coordinates are relative to the bottom-left corner
of the BBC BASIC main output window:
<langsyntaxhighlight lang="bbcbasic"> MOUSE xmouse%, ymouse%, buttons%
PRINT xmouse%, ymouse%</langsyntaxhighlight>
 
=={{header|C}}==
{{libheader|Xlib}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <X11/Xlib.h>
 
Line 266 ⟶ 271:
(void)XCloseDisplay(d); /* and close the display */
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
Mouse pointers are part of the window manager system, and are therefore highly platform dependent.
 
This example works under a Windows operating system.
<syntaxhighlight lang="c++">
#include <iostream>
 
#include <windows.h>
 
int main() {
POINT MousePoint;
if ( GetCursorPos(&MousePoint) ) {
std::cout << MousePoint.x << ", " << MousePoint.y << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
726, 506
</pre>
 
=={{header|c_sharp|C#}}==
Writes the absolute Mouse Position of the Screen into the Console
<langsyntaxhighlight lang="csharp">
using System;
using System.Windows.Forms;
Line 282 ⟶ 308:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(let [point (.. java.awt.MouseInfo getPointerInfo getLocation)] [(.getX point) (.getY point)])</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 291 ⟶ 317:
With the ltk library.
 
<langsyntaxhighlight lang="lisp">
(ql:quickload "ltk")
(in-package :ltk-user)
Line 300 ⟶ 326:
;; create a small window. Enter the mouse to see lots of events.
(bind *tk* "<Motion>" #'motion))
</syntaxhighlight>
</lang>
This prints a lot of events of the form
 
Line 320 ⟶ 346:
Shows mouse-position on a label on the form.
 
<langsyntaxhighlight Delphilang="delphi">procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
lblMousePosition.Caption := ('X:' + IntToStr(X) + ', Y:' + IntToStr(Y));
end;</langsyntaxhighlight>
 
 
Line 332 ⟶ 358:
with the help of Windows and classes units.
 
<langsyntaxhighlight Delphilang="delphi">program Project1;
{$APPTYPE CONSOLE}
uses
Line 346 ⟶ 372:
sleep(300);
end
end.</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=y80vS1UwNFCwMODKz1PIzS8tTo3PBYpxKSgoJOekJhaBGClFieXpRZkpIHZJakUJVF2FgpqCEhCqQfmVXHpcXAA= Run it]
[https://easylang.online/ide/?run=on%20mouse_move%0A%20%20clear_screen%0A%20%20draw_text%20mouse_x%20%26%20%22%20%22%20%26%20mouse_y%0A.%0A Run it]
 
<syntaxhighlight>
<lang>on mouse_move
move 10 80
clear_screen
on mouse_move
draw_text mouse_x & " " & mouse_y
clear
.</lang>
drawgrid
text mouse_x & " " & mouse_y
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
When the '''plot''' library is loaded, the mouse position - relative to the plotting area coordinates, is displayed inside the info panel.
<langsyntaxhighlight lang="lisp">
(lib 'plot)
(plot-x-minmax 10) ; set logical dimensions of plotting area
Line 367 ⟶ 397:
;; the mouse position is displayed as , for example, [ x: 5.6 y : 88.7]
;; 0 <= x <= 10, 0 <= y <= 100
</syntaxhighlight>
</lang>
 
--[[User:Neo.abhinav|Neo.abhinav]] 17:00, 6 May 2011 (UTC)
 
=={{header|Elm}}==
<langsyntaxhighlight lang="elm">import Graphics.Element exposing (Element, show)
import Mouse
 
Line 378 ⟶ 408:
main : Signal Element
main =
Signal.map show Mouse.position</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
Line 384 ⟶ 414:
the frame the mouse is over and where the mouse is within that frame can be obtained with
 
<langsyntaxhighlight Lisplang="lisp">(mouse-pixel-position)
;; => (FRAME . (X . Y))</syntaxhighlight>
=>
(FRAME . (X . Y))</lang>
 
Or <code>mouse-position</code> for characters instead of pixels.
Line 396 ⟶ 425:
=={{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.
<syntaxhighlight lang="erre">
<lang ERRE>
!
! MOUSE WITH 'MOUSE.LIB' LIBRARY
Line 476 ⟶ 505:
CLS
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
Line 483 ⟶ 512:
If the question was for the point relative to the current window,
life would be much simpler.
<langsyntaxhighlight lang="fsharp">open System.Windows.Forms
open System.Runtime.InteropServices
 
Line 504 ⟶ 533:
ScreenToClient(hwnd, &ptFs) |> ignore
ptFs
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Works only in the graphical listener.
Replaces the text in the button with the relative and absolute coordinates of the mouse
<langsyntaxhighlight lang="factor">: replace-text ( button text -- )
[ drop children>> pop drop ] [ >label add-gadget drop ] 2bi ;
: present-locations ( loc1 loc2 -- string )
Line 522 ⟶ 551:
]
<border-button> gadget. ;
</syntaxhighlight>
</lang>
 
=={{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.
 
<langsyntaxhighlight lang="freebasic">
type MOUSE
'mouse position, button state, wheel state
Line 555 ⟶ 584:
print "Wheel scrolls: ", mouse.wheel," "
end if
wend</langsyntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
subclass window 1, @"Click somewhere in the window"
 
void local fn DoDialog( ev as long )
select ( ev )
case _windowMouseDown
CGPoint pt = fn EventLocationInWindow
cls : printf @"%.0fx, %.0fy",pt.x,pt.y
end select
end fn
 
on dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
 
=={{header|Gambas}}==
Line 564 ⟶ 612:
this is the only widget that can track pointer movement within gambas.
 
<langsyntaxhighlight lang="gambas">
PUBLIC SUB Form1_MouseMove()
PRINT mouse.X
PRINT Mouse.Y
END
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
Line 575 ⟶ 623:
<br>
The active window may be externally created.
<langsyntaxhighlight lang="go">package main
 
import (
Line 609 ⟶ 657:
fmt.Println("Problem when finding PID(s) of", name)
}
}</langsyntaxhighlight>
 
{{out}}
Line 629 ⟶ 677:
=={{header|Groovy}}==
Based on Java solution:
<langsyntaxhighlight lang="groovy">1.upto(5) {
Thread.sleep(1000)
def p = java.awt.MouseInfo.pointerInfo.location
println "${it}: x=${p.@x} y=${p.@y}"
}</langsyntaxhighlight>
 
Sample output:
Line 645 ⟶ 693:
Mouse click positions for windows created internally.
X and Y are in units of current xy axes (optional: invisible).
<langsyntaxhighlight lang="hicest"> WINDOW(WINdowhandle=handle)
AXIS(WINdowhandle=handle, MouSeCall=Mouse_Callback, MouSeX=X, MouSeY=Y)
END
Line 651 ⟶ 699:
SUBROUTINE Mouse_Callback()
WRITE(Messagebox, Name) X, Y
END</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
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.
<langsyntaxhighlight Iconlang="icon">until *Pending() > 0 & Event() == "q" do { # loop until there is something to do
px := WAttrib("pointerx")
py := WAttrib("pointery")
Line 662 ⟶ 710:
WDelay(5) # wait and share processor
}
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
The mouse position can be checked at any time by calling
<langsyntaxhighlight lang="java5">Point mouseLocation = MouseInfo.getPointerInfo().getLocation();</langsyntaxhighlight>
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 677 ⟶ 725:
One of many ways to add an event listener:
 
<langsyntaxhighlight lang="javascript">document.addEventListener('mousemove', function(e){
var position = { x: e.clientX, y: e.clientY }
}</langsyntaxhighlight>
 
In the above example, the window may not be external.
Line 685 ⟶ 733:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk
 
const win = GtkWindow("Get Mouse Position", 600, 800)
Line 700 ⟶ 748:
signal_connect(endit, win, :destroy)
wait(c)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Groovy}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.MouseInfo
Line 714 ⟶ 762:
println("${it}: x = ${"%-4d".format(p.x)} y = ${"%-4d".format(p.y)}")
}
}</langsyntaxhighlight>
Sample output:
{{out}}
Line 728 ⟶ 776:
This example gets the mouse position based on the active window.
Click on other windows to get relative mouse position based on those windows.
<langsyntaxhighlight lang="lb"> nomainwin
 
UpperLeftX = DisplayWidth-WindowWidth
Line 765 ⟶ 813:
y = point.y.struct
CursorPos$=x; ",";y
end function</langsyntaxhighlight>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">put _mouse.mouseLoc
-- point(310, 199)</langsyntaxhighlight>
 
=={{header|LiveCodexTalk}}==
{{works with|HyperCard}} {{works with|LiveCode}}
<lang liveCode>
<syntaxhighlight lang="livecode">
-- Method 1:
-- this script in either the stack script or the card script to get position relative to the current stack window
Line 798 ⟶ 847:
148,521 -- relative to current screen
 
</syntaxhighlight>
</lang>
 
=={{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
 
Line 806 ⟶ 856:
use com.livecode.widget -- include the required module
--- in your handler:
variable tPosition as Point -- type declaration, tPosition is a rectpoint array struct, something like [0,1024,0,768]
put the mouse position into tPosition
variable tRect as Rectangle -- type declaration, tRect is a rect array struct, something like [0,1024,0,768]
put my bounds into tRect
if tPosition is within tRect then
log "mouse position is within the widget bounds"
end if
</syntaxhighlight>
 
</lang>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">show mousepos ; [-250 250]</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
\\ works when console is the active window
Line 853 ⟶ 902:
}
CheckIt
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">MousePosition["WindowAbsolute"]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
<syntaxhighlight lang MATLAB="matlab">get(0,'PointerLocation')</langsyntaxhighlight>
 
=={{header|MAXScript}}==
Creates a window on the screen and shows mouse position as it moves.
 
<syntaxhighlight lang="maxscript">
<lang MAXScript>
try destroydialog mousePos catch ()
 
Line 885 ⟶ 934:
 
createdialog mousepos
</syntaxhighlight>
</lang>
 
=={{header|MiniScript}}==
{{works with|Mini Micro}}
<syntaxhighlight lang MiniScript="miniscript">print mouse.x, mouse.y</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">import Nanoquery.Util.Windows
 
// a function to handle the mouse moved event
Line 903 ⟶ 952:
w.setSize(500, 500)
w.setHandler(w.mouseMoved, mouse_moved)
w.show()</langsyntaxhighlight>
 
=={{header|Nim}}==
{{libheader|gintro}}
<langsyntaxhighlight Nimlang="nim">import gintro/[glib, gobject, gtk, gio]
import gintro/gdk except Window
 
Line 933 ⟶ 982:
let app = newApplication(Application, "Rosetta.MousePosition")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
{{out}}
Line 950 ⟶ 999:
 
=={{header|OCaml}}==
equivalent to the C example, uses the Xlib binding [http://wwwdecapode314.linux-nantesfree.orgfr/~fmonnier/OCamlocaml/Xlib/ ocaml-xlib]
<langsyntaxhighlight OCamllang="ocaml">open Xlib
 
let () =
Line 974 ⟶ 1,023:
 
xCloseDisplay d;
;;</langsyntaxhighlight>
 
=={{header|Octave}}==
To get the X,Y coordinates of N mouse clicks in the current figure,
one can use this:
<langsyntaxhighlight Octavelang="octave">[X, Y, BUTTONS] = ginput(N);</langsyntaxhighlight>
Example:
<pre>>> [X, Y, BUTTONS] = ginput(4)
Line 1,003 ⟶ 1,052:
2
</pre>
 
=={{header|Odin}}==
 
<syntaxhighlight lang="odin">package main
 
import "core:fmt"
import "vendor:sdl2"
 
main :: proc() {
using sdl2
 
window: ^Window = ---
renderer: ^Renderer = ---
event: Event = ---
 
Init(INIT_VIDEO)
CreateWindowAndRenderer(
640, 480,
WINDOW_SHOWN,
&window, &renderer
)
 
SetWindowTitle(window, "Empty window")
RenderPresent(renderer)
 
for event.type != .QUIT {
if event.type == .MOUSEMOTION {
using event.motion
fmt.printf("x=%d y=%d\n", x, y)
}
Delay(10)
PollEvent(&event)
}
 
DestroyRenderer(renderer)
DestroyWindow(window)
Quit()
}</syntaxhighlight>
 
=={{header|Oz}}==
Repeatedly shows the mouse coordinates relative to the foremost window of the application.
<langsyntaxhighlight lang="oz">declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
WindowClosed = {NewCell false}
Line 1,026 ⟶ 1,113:
#", y: "#(Winfo.pointery - Winfo.rooty))}
{Delay 250}
end</langsyntaxhighlight>
 
=={{header|Pebble}}==
<syntaxhighlight lang="pebble">;mouse demonstration
;compile with Pebble
;for textmode x86 DOS
;requires mouse driver
 
program examples\mouse
 
use mouse.inc
 
data
 
int mousex[0]
int mousey[0]
int mouseb[0]
int speed[0]
int i[0]
 
begin
 
echo "Enter 1-200 for slow/DOS/emulated machines."
echo "Enter 500-20000 for faster/Windows machines."
input [speed]
 
cls
 
call showmouse
 
label mainloop
 
;clear mouse coordinates
 
cursor 0, 0
echo " "
echo " "
 
;get and display mouse coordinates
 
call readmouse
 
cursor 0, 0
echo [mousey]
crlf
echo [mousex]
 
;display exit button
 
cursor 76, 0
echo "[X]"
 
;check if exit button has been clicked
 
if [mouseb] = 1 & [mousex] >= 76 & [mousex] <= 79 & [mousey] = 0 then
 
kill
 
endif
 
;loop 100 times since some machines do not support the WAIT command
 
[i] = 0
 
label delay
 
+1 [i]
wait 1
 
if [i] < [speed] then delay
 
goto mainloop
 
end</syntaxhighlight>
 
=={{header|Perl}}==
Line 1,033 ⟶ 1,193:
When you move the mouse over the created window,
the mouse position get printed and the program terminates.
<langsyntaxhighlight Perllang="perl">use SDL;
use SDL::Events;
use SDLx::App;
Line 1,046 ⟶ 1,206:
} );
$app->run;
</syntaxhighlight>
</lang>
{{out}}
<pre>x=15 y=304</pre>
Line 1,057 ⟶ 1,217:
Note that canvasmotion coordinates are relative to the top left of the canvas, whereas the other two are absolute.
{{libheader|Phix/pGUI}}
<langsyntaxhighlight Phixlang="phix">-- demo\rosetta\Mouse_position.exw
include pGUI.e
 
Line 1,115 ⟶ 1,275:
IupClose()
end procedure
main()</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Line 1,123 ⟶ 1,283:
The returned value is (X . Y), where X is the
column and Y the line number.
<langsyntaxhighlight PicoLisplang="picolisp">(de mousePosition ()
(prog2
(prin "^[[?9h") # Mouse reporting on
Line 1,134 ⟶ 1,294:
(- (char (key)) 32)
(- (char (key)) 32) ) )
(prin "^[[?9l") ) ) # Mouse reporting off</langsyntaxhighlight>
{{out}}
<pre>: (mousePosition)
Line 1,141 ⟶ 1,301:
=={{header|PureBasic}}==
The mouse position can be obtained by these two commands:
<langsyntaxhighlight PureBasiclang="purebasic">x = WindowMouseX(#MyWindow)
y = WindowMouseY(#MyWindow)</langsyntaxhighlight>
This example repeatedly shows the mouse coordinates
relative to the window created in the application.
<langsyntaxhighlight PureBasiclang="purebasic">#MyWindow = 0
#Label_txt = 0
#MousePos_txt = 1
Line 1,164 ⟶ 1,324:
SetGadgetText(#MousePos_txt,"(" + Str(x) + "," + Str(y) + ")")
ForEver
EndIf</langsyntaxhighlight>
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">void setup(){
size(640, 480);
}
Line 1,175 ⟶ 1,335:
ellipse(mouseX, mouseY, 5, 5); // graphic output example
println("x:" + mouseX + " y:" + mouseY);
}</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
<langsyntaxhighlight lang="python">def setup():
size(640, 480)
 
Line 1,184 ⟶ 1,344:
# mouseX and mouseY provide the current mouse position
ellipse(mouseX, mouseY, 5, 5) # graphic output example
println("x:{} y:{}".format(mouseX, mouseY))</langsyntaxhighlight>
 
=={{header|Python}}==
Line 1,196 ⟶ 1,356:
 
Code is based on post in Daniweb: http://www.daniweb.com/forums/post616327.html#post616327 by ZZucker
<langsyntaxhighlight lang="python">
import Tkinter as tk
 
Line 1,216 ⟶ 1,376:
 
root.mainloop()
</syntaxhighlight>
</lang>
----------------
<langsyntaxhighlight lang="python">
#simple way of ,get cursor xy data
 
Line 1,232 ⟶ 1,392:
win.bind("<Motion>",xy)
mainloop()
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 1,238 ⟶ 1,398:
The mouse position can be queried at any time with a function in a GUI context.
 
<langsyntaxhighlight lang="racket">
#lang racket/gui
(define-values [point _] (get-current-mouse-state))
(send point get-x)
(send point get-y)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo/jvm|2013-12-03}}
<syntaxhighlight lang="raku" perl6line>use java::awt::MouseInfo:from<java>;
 
given MouseInfo.getPointerInfo.getLocation {
say .getX, 'x', .getY;
}</langsyntaxhighlight>
 
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}}
<syntaxhighlight lang="raku" perl6line>use X11::libxdo;
my $xdo = Xdo.new;
 
Line 1,278 ⟶ 1,438:
}
 
say '';</langsyntaxhighlight>
 
=={{header|Ring}}==
Line 1,285 ⟶ 1,445:
The movement procedure uses the mouse position information
 
<syntaxhighlight lang="ring">
<lang Ring>
Load "guilib.ring"
 
Line 1,348 ⟶ 1,508:
}
ok
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Line 1,354 ⟶ 1,514:
{{libheader|Shoes}}
 
<langsyntaxhighlight Rubylang="ruby">Shoes.app(:title => "Mouse Position", :width => 400, :height => 400) do
@position = para "Position : ?, ?", :size => 12, :margin => 10
Line 1,360 ⟶ 1,520:
@position.text = "Position : #{x}, #{y}"
end
end</langsyntaxhighlight>
 
=={{header|Rust}}==
Prints current location of the mouse cursor relative to any active window.
This example relies on the Windows API.
<langsyntaxhighlight lang="rust">// rustc 0.9 (7613b15 2014-01-08 18:04:43 -0800)
 
use std::libc::{BOOL, HANDLE, LONG};
Line 1,401 ⟶ 1,561:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>x: 550, y: 614
Line 1,413 ⟶ 1,573:
=={{header|Scala}}==
 
{{libheader|Scala}}<langsyntaxhighlight lang="scala">import java.awt.MouseInfo
object MousePosition extends App {
val mouseLocation = MouseInfo.getPointerInfo.getLocation
println (mouseLocation)
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 1,423 ⟶ 1,583:
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:
<langsyntaxhighlight lang="seed7">xPos := pointerXPos(curr_win);
yPos := pointerYPos(curr_win);</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Sending the message <tt>position</tt> to the <tt>activeHand</tt> of the <tt>World</tt> returns a <tt>Point</tt> object:
 
<langsyntaxhighlight lang="smalltalk">
World activeHand position. " (394@649.0)"
</syntaxhighlight>
</lang>
 
=={{header|Standard ML}}==
Works with PolyML
<langsyntaxhighlight Standardlang="standard MLml">open XWindows ;
val dp = XOpenDisplay "" ;
val w = XCreateSimpleWindow (RootWindow dp) origin (Area {x=0,y=0,w=400,h=300}) 3 0 0xffffff ;
Line 1,443 ⟶ 1,603:
) ;
XQueryPointer focus ;
XQueryPointer w;</langsyntaxhighlight>
result: position wrt root window and active window, resp wrt root window and window w
val it =
Line 1,456 ⟶ 1,616:
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk 8.5
set curWindow [lindex [wm stackorder .] end]
# Everything below will work with anything from Tk 8.0 onwards
set x [expr {[winfo pointerx .] - [winfo rootx $curWindow]}]
set y [expr {[winfo pointery .] - [winfo rooty $curWindow]}]
tk_messageBox -message "the mouse is at ($x,$y) in window $curWindow"</langsyntaxhighlight>
 
 
screen coordinates
<syntaxhighlight lang="tcl">package require Tk
puts "monitor/display coordinate x = [winfo pointerx .]"</syntaxhighlight>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
|90 @Mouse &vector $2 &x $2 &y $2 &state $1 &pad $3 &scrollx $2 &scrolly $2
 
|0100
;on-mouse .Mouse/vector DEO2
BRK
 
@on-mouse
.Mouse/x DEI2 print-hex2
#20 .Console/write DEO
.Mouse/y DEI2 print-hex2
#0a .Console/write DEO
BRK
 
@print-hex
DUP #04 SFT print-digit #0f AND print-digit
JMP2r
 
@print-hex2
SWP print-hex print-hex
JMP2r
 
@print-digit
DUP #09 GTH #27 MUL ADD #30 ADD .Console/write DEO
JMP2r</syntaxhighlight>
 
=={{header|Vala}}==
<syntaxhighlight lang="vala">// GTK 4
public class Example : Gtk.Application {
public Example() {
Object(application_id: "my.application",
flags: ApplicationFlags.FLAGS_NONE);
activate.connect(() => {
var window = new Gtk.ApplicationWindow(this);
var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 20);
var button = new Gtk.Button.with_label("Get Cursor Position");
button.clicked.connect((a) => {
double x, y;
var device_pointer= window.get_display().get_default_seat().get_pointer();
window.get_surface().get_device_position(device_pointer, out x, out y, null);
label.set_text(x.to_string() + "," + y.to_string());
});
box.append(label);
box.append(button);
window.set_child(box);
window.present();
});
}
public static int main(string[] argv) {
return new Example().run(argv);
}
}</syntaxhighlight>
 
=={{header|Visual Basic}}==
Line 1,471 ⟶ 1,692:
(so if the pointer is over a button on the current form,
the event will only fire for the button, ''not'' the form).
<langsyntaxhighlight 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
Me.Print "X:" & X
Me.Print "Y:" & Y
End Sub</langsyntaxhighlight>
 
The second method uses the [[wp:Windows API|Windows API]],
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.
<langsyntaxhighlight lang="vb">Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
 
Private Type POINTAPI
Line 1,495 ⟶ 1,716:
Me.Print "X:" & Pt.X
Me.Print "Y:" & Pt.Y
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas
import "input" for Mouse
Line 1,531 ⟶ 1,752:
 
static draw(alpha) {}
}</langsyntaxhighlight>
 
{{out}}
Line 1,561 ⟶ 1,782:
does not make Windows applications (however see "Simple windowed application").
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\stdlib;
if not OpenMouse then Text(0, "A mouse is required")
else [ShowMouse(true);
IntOut(0, GetMousePosition(0)); ChOut(0, ^,);
IntOut(0, GetMousePosition(1)); CrLf(0);
]</langsyntaxhighlight>
 
{{out|Example output}}
1,964

edits