Mouse position

From Rosetta Code
Revision as of 20:27, 27 May 2009 by rosettacode>Dkf (→‎Tcl: Added implementation)
Task
Mouse position
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.

AutoHotkey

<lang AutoHotkey> WinGetTitle, window, A MouseGetPos, x, y Msgbox, the mouse is at xpos %x% ypos %y% in window %window% </lang>

Tcl

Library: Tk

8.5

<lang tcl>set curWindow [lindex [wm stackorder .] end]

  1. 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>