Jump to content

Mouse position: Difference between revisions

m
Category:Hardware / move Category to top
m (Category:Hardware / move Category to top)
Line 1:
{{task|GUI}} [[Category:Testing]] [[Category:Hardware]] [[Category:Pointing devices]]
{{task|GUI}}[[Category:Testing]]Get the current location of the mouse cursor relative to the active window. Please specify if the window may be externally created.
{{omit from|ACL2}}
{{omit from|AWK|Does not have an active window concept}}
{{omit from|Befunge|No mouse support}}
{{omit from|Brainf***}}
{{omit from|GolfScript}}
{{omit from|GUISS}}
{{omit from|Logtalk}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|PARI/GP}}
{{omit from|PHP}}
{{omit from|PostScript}}
{{omit from|R}}
{{omit from|TI-83 BASIC|Does not have a pointing device.}}
{{omit from|TI-89 BASIC|Does not have a pointing device.}}
{{omit from|UNIX Shell}}
{{omit from|ZX Spectrum Basic|Does not have a pointing device interface or windowing engine}}
 
{{task|GUI}}[[Category:Testing]]Get the current location of the mouse cursor relative to the active window. Please specify if the window may be externally created.
Please specify if the window may be externally created.
 
=={{header|ActionScript}}==
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.
<lang ActionScript3>
package {
Line 50 ⟶ 72:
{{libheader|GLib}}
 
The [[GTK]] procedure is Get_Pointer.
The [[GTK]] procedure is Get_Pointer. 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:
It returns mouse coordinates relatively to a window (internally created).
The [[GTK]] procedure is Get_Pointer. 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:
<lang Ada>with GLib; use GLib;
with Gtk.Button; use Gtk.Button;
Line 120 ⟶ 144:
 
=={{header|AutoHotkey}}==
WindowThe window may be an externally created window.
<lang AutoHotkey>#i:: ; (Optional) Assigns a hotkey to display window info, Windows+i
MouseGetPos, x, y ; Gets x/y pos relative to active window
Line 126 ⟶ 150:
Traytip, Mouse position, x: %x%`ny: %y%`rWindow: %WinTitle%, 4 ; Displays the info as a Traytip for 4 seconds
return</lang>
 
===with DllCall===
Source: [https://github.com/jNizM/AHK_DllCall_WinAPI/ GetCursorPos @github] by jNizM
Line 159 ⟶ 184:
 
=={{header|BBC BASIC}}==
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%
PRINT xmouse%, ymouse%</lang>
Line 219 ⟶ 245:
=={{header|Clojure}}==
<lang lisp>(let [point (.. java.awt.MouseInfo getPointerInfo getLocation)] [(.getX point) (.getY point)])</lang>
 
=={{header|Delphi}}==
Shows Mouse Positionmouse-position on a label on the form.
 
<lang Delphi>procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Line 227 ⟶ 254:
lblMousePosition.Caption := ('X:' + IntToStr(X) + ', Y:' + IntToStr(Y));
end;</lang>
 
 
 
'''Delphi Console Program'''
 
The following program will help capture the mouse position with the help of Windows and classes units.
with the help of Windows and classes units.
 
<lang Delphi>program Project1;
Line 253 ⟶ 280:
 
=={{header|Emacs Lisp}}==
For Emacs' own frames (which is what Emacs calls its window system windows), 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)
Line 259 ⟶ 287:
(FRAME . (X . Y))</lang>
 
Or <code>mouse-position</code> for characters instead of pixels. Works when the mouse is over any Emacs frame, not just when that frame is the active focused window.
Works when the mouse is over any Emacs frame, not just when that frame is the active focused window.
 
There's no particularly easy way to inquire about a non-Emacs focused window, only generic X11 or similar approaches run externally.
only generic X11 or similar approaches run externally.
 
=={{header|Factor}}==
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 -- )
[ drop children>> pop drop ] [ >label add-gadget drop ] 2bi;
Line 281 ⟶ 312:
 
=={{header|F_Sharp|F#}}==
Have to do a lot of interop here.
Have to do a lot of interop here. Primarily because the active window may not be a .NET form/control. If the question was for the point relative to the current window, life would be much simpler.
Primarily because the active window may not be a .NET form/control.
If the mousequestion pointer,was for the point relative to the givencurrent window:,
life would be much simpler.
<lang fsharp>open System.Windows.Forms
open System.Runtime.InteropServices
Line 307 ⟶ 341:
=={{header|Gambas}}==
 
In gambas, the position of the pointer can only be determined when the click button is held down. A window with a drawing area is required, because this is the only widget that can track pointer movement within gambas.
when the click button is held down.
A window with a drawing area is required, because
this is the only widget that can track pointer movement within gambas.
 
<lang gambas>
Line 332 ⟶ 369:
 
=={{header|HicEst}}==
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)
AXIS(WINdowhandle=handle, MouSeCall=Mouse_Callback, MouSeX=X, MouSeY=Y)
Line 342 ⟶ 380:
 
=={{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.
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
px := WAttrib("pointerx")
Line 359 ⟶ 398:
=={{header|JavaScript}}==
 
In a browser environment, it's impossible to actually get the cursor position at the specific moment. You must wait for user input (movement, click, etc). One of many ways to add an event listener:
at the specific moment.
You must wait for user input (movement, click, etc).
One of many ways to add an event listener:
 
<lang javascript>document.addEventListener('mousemove', function(e){
Line 365 ⟶ 407:
}</lang>
 
In the above example, the window may not be external. It must in fact be a web browser window, which runs the script.
It must in fact be a web browser window, which runs the script.
 
=={{header|Liberty BASIC}}==
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
 
Line 446 ⟶ 490:
 
=={{header|Octave}}==
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>
Example:
Line 498 ⟶ 543:
=={{header|Perl}}==
==={{libheader|Perl/SDL}}===
The following code will use the SDL module, a wrapper for the libSDL C-library. When you move the mouse over the created window, the mouse position get printed and the program terminates.
When you move the mouse over the created window,
the mouse position get printed and the program terminates.
<lang Perl>use SDL;
use SDL::Events;
Line 523 ⟶ 570:
say .getX, 'x', .getY;
}</lang>
 
=={{header|PicoLisp}}==
The following works in an XTerm window. After calling (mousePosition), click
After calling (mousePosition), click
into the current terminal window. The returned value is (X . Y), where X is the
into the current terminal window.
into the current terminal window. The returned value is (X . Y), where X is the
column and Y the line number.
<lang PicoLisp>(de mousePosition ()
Line 572 ⟶ 622:
{{libheader|Python Tkinter module (Tk 8.5)}}
 
Mouse position using Tkinter graphics library nearly universally included in installations of Python. There is other alternatives but they are platform specific. Shows position of mouse while it is over the program windows and changes color of window when mouse is near (<10) hot spot 100,100.
There are other alternatives but they are platform specific. <br>
Shows position of mouse while it is over the program windows and
changes color of window when mouse is near (<10) hot spot 100,100.
 
Code is based on post in Daniweb: http://www.daniweb.com/forums/post616327.html#post616327 by ZZucker
Line 690 ⟶ 743:
}
}</lang>
{{out}}
Sample output:
<pre>x: 550, y: 614
x: 650, y: 248
Line 702 ⟶ 755:
The functions [http://seed7.sourceforge.net/libraries/graph.htm#pointerXPos%28in_PRIMITIVE_WINDOW%29 pointerXPos]
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:
the mouse pointer, relative to the given window:
<lang seed7>xPos := pointerXPos(curr_win);
yPos := pointerYPos(curr_win);</lang>
Line 724 ⟶ 776:
 
=={{header|Visual Basic}}==
There are two methods for determining where the mouse pointer is.
There are two methods for determining where the mouse pointer is. The first only works when the pointer is actually over the window containing the code. This actually works for any control that has a MouseMove event, but it doesn't work if the pointer is over ''anything else'', including controls on the 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 first only works when the pointer is actually over the window containing the code.
There are two methods for determining where the mouse pointer is. The first only works when the pointer is actually over the window containing the code. This actually works for any control that has a MouseMove event, but it doesn't work if the pointer is over ''anything else'', including controls on the form (so if the pointer is over a button on the current 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)
'X and Y are in "twips" -- 15 twips per pixel
Line 731 ⟶ 785:
End Sub</lang>
 
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.
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
 
Line 750 ⟶ 805:
 
=={{header|XPL0}}==
GetMousePosition(0) = X coordinate; 1 = Y coordinate. For video modes
For video modes
$0..$E and $13 the maximum coordinates are 639x199, minus the size of the
pointer. For modes $F0..$12E and $13 the maximum coordinates are 639x199, minus the samesize asof the pixelspointer.
For modes $F..$12 the coordinates are the same as the pixels.
VESA graphic modes are (usually) 639x479 regardless of the resolution.
For 80-column text modes divide the mouse coordinates by 8 to get the
Line 769 ⟶ 825:
]</lang>
 
{{out|Example output:}}
<pre>
320,96
</pre>
 
 
{{omit from|ACL2}}
{{omit from|AWK|Does not have an active window concept}}
{{omit from|Befunge|No mouse support}}
{{omit from|Brainf***}}
{{omit from|GolfScript}}
{{omit from|GUISS}}
{{omit from|Logtalk}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|PARI/GP}}
{{omit from|PHP}}
{{omit from|PostScript}}
{{omit from|R}}
{{omit from|TI-83 BASIC|Does not have a pointing device.}}
{{omit from|TI-89 BASIC|Does not have a pointing device.}}
{{omit from|UNIX Shell}}
{{omit from|ZX Spectrum Basic|Does not have a pointing device interface or windowing engine}}
 
[[Category:Pointing devices]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.