Mouse position: Difference between revisions
(Put in testing cat) |
m (Fixed lang tags.) |
||
Line 6: | Line 6: | ||
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: |
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> |
<lang Ada>with GLib; use GLib; |
||
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 73: | Line 72: | ||
Gtk.Main.Main; |
Gtk.Main.Main; |
||
end Tell_Mouse; |
end Tell_Mouse;</lang> |
||
</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Window may be externally created. |
Window may be externally created. |
||
<lang AutoHotkey> |
<lang AutoHotkey>WinGetTitle, window, A |
||
WinGetTitle, window, A |
|||
MouseGetPos, x, y |
MouseGetPos, x, y |
||
Msgbox, the mouse is at xpos %x% ypos %y% in window %window% |
Msgbox, the mouse is at xpos %x% ypos %y% in window %window%</lang> |
||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 130: | Line 126: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo> |
<lang logo>show mousepos ; [-250 250]</lang> |
||
show mousepos ; [-250 250] |
|||
</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
<lang Mathematica> |
<lang Mathematica>MousePosition["WindowAbsolute"]</lang> |
||
MousePosition["WindowAbsolute"] |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
Revision as of 15:58, 20 November 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Get the current location of the mouse cursor relative to the active window. Please specify if the window may be externally created.
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
.
Logo
<lang logo>show mousepos ; [-250 250]</lang>
Mathematica
<lang Mathematica>MousePosition["WindowAbsolute"]</lang>
OCaml
equivalent to the C example, uses the Xlib binding ocaml-xlib <lang OCaml>open Xlib
let () =
let d = xOpenDisplay "" in
(* ask for active window (no error check); the client must be freedesktop compliant *) let _, _, _, _, props = xGetWindowProperty_window d (xDefaultRootWindow d) (xInternAtom d "_NET_ACTIVE_WINDOW" true) 0 1 false AnyPropertyType in
let _, _, _, child, _ = xQueryPointer d props in begin match child with | Some(_, childx, childy) -> Printf.printf "relative to active window: %d,%d\n%!" childx childy; | None -> print_endline "the pointer is not on the same screen as the specified window" end;
xCloseDisplay d;
- </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>