Mouse position
Get the current location of the mouse cursor relative to the active window. Please specify if the window may be externally created.
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Ada
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; with Gtk.Label; use Gtk.Label; with Gtk.Window; use Gtk.Window; with Gtk.Widget; use Gtk.Widget; with Gtk.Table; use Gtk.Table;
with Gtk.Handlers; with Gtk.Main;
procedure Tell_Mouse is
Window : Gtk_Window; Grid : Gtk_Table; Button : Gtk_Button; Label : Gtk_Label; package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record); package Return_Handlers is new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean); function Delete_Event (Widget : access Gtk_Widget_Record'Class) return Boolean is begin return False; end Delete_Event; procedure Destroy (Widget : access Gtk_Widget_Record'Class) is begin Gtk.Main.Main_Quit; end Destroy; procedure Clicked (Widget : access Gtk_Widget_Record'Class) is X, Y : GInt; begin Get_Pointer (Window, X, Y); Set_Text (Label, "At" & GInt'Image (X) & GInt'Image (Y)); end Clicked;
begin
Gtk.Main.Init; Gtk.Window.Gtk_New (Window); Gtk_New (Grid, 1, 2, False); Add (Window, Grid); Gtk_New (Label); Attach (Grid, Label, 0, 1, 0, 1); Gtk_New (Button, "Click me"); Attach (Grid, Button, 0, 1, 1, 2); Return_Handlers.Connect ( Window, "delete_event", Return_Handlers.To_Marshaller (Delete_Event'Access) ); Handlers.Connect ( Window, "destroy", Handlers.To_Marshaller (Destroy'Access) ); Handlers.Connect ( Button, "clicked", Handlers.To_Marshaller (Clicked'Access) ); Show_All (Grid); Show (Window); Gtk.Main.Main;
end Tell_Mouse; </lang>
AutoHotkey
Window may be externally created. <lang AutoHotkey> WinGetTitle, window, A MouseGetPos, x, y Msgbox, the mouse is at xpos %x% ypos %y% in window %window% </lang>
C
<lang c>#include <stdio.h>
- include <X11/Xlib.h>
int main() {
Display *d; Window inwin; /* root window the pointer is in */ Window inchildwin; /* child win the pointer is in */ int rootx, rooty; /* relative to the "root" window; we are not interested in these, but we can't pass NULL */ int childx, childy; /* the values we are interested in */ Atom atom_type_prop; /* not interested */ int actual_format; /* should be 32 after the call */ unsigned int mask; /* status of the buttons */ unsigned long n_items, bytes_after_ret; Window *props; /* since we are interested just in the first value, which is
a Window id */
/* default DISPLAY */ d = XOpenDisplay(NULL);
/* ask for active window (no error check); the client must be freedesktop compliant */ (void)XGetWindowProperty(d, DefaultRootWindow(d),
XInternAtom(d, "_NET_ACTIVE_WINDOW", True), 0, 1, False, AnyPropertyType, &atom_type_prop, &actual_format, &n_items, &bytes_after_ret, (unsigned char**)&props);
XQueryPointer(d, props[0], &inwin, &inchildwin,
&rootx, &rooty, &childx, &childy, &mask);
printf("relative to active window: %d,%d\n", childx, childy);
XFree(props); /* free mem */ (void)XCloseDisplay(d); /* and close the display */ return 0;
}</lang>
Java
The mouse position can be checked at any time by calling
<lang java5>Point mouseLocation = MouseInfo.getPointerInfo().getLocation();</lang>
This returns a point on the entire screen, rather than relative to a particular window. This call can be combined with getLocation()
from any Component
(including a Frame
, which is a window) to get the location relative to that Component
.
Mathematica
<lang Mathematica> MousePosition["WindowAbsolute"] </lang>
Tcl
8.5
<lang tcl>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"</lang>