Re: [MacPerl-Toolbox] Detecting email coordinates

[email protected] (Greenblatt & Seay) Sat, 1 Sep 2001 09:46:38 +0100
Newsgroups perl.macperl.toolbox
Message-ID <l03110700b7b64b92a724@[207.91.2.197]>
At 2:52 AM +0100 2001/08/28, Adam Jenkins wrote:
>but I desperately need to get
>the x and y coordinates of the mouse after a click within a window

I reread the original post and realized the original poster requested a
method to find window coordinates of a mouse click. The code I sent
previously showed only screen coordinates.

Below is an enhanced version of the program that shows both screen and
window coordinates of mouse clicks as well as indicating whether the click
was in a window, zoom box, size box, close box, menu bar, or title bar.  It
also tracks mouse movement and shows key presses.

David Seay
http://www.mastercall.com/g-s/

---

#!perl

use Mac::Events;
use Mac::Events qw(@Event $CurrentEvent);
use Mac::LowMem;
use Mac::QuickDraw;
use Mac::Windows;

$Event[mouseDown] = \&mouseDownHandler;
$Event[keyDown]   = \&keyDownHandler;
@codes = ("","Menu Bar","","Window","Title Bar","Size Box","Close
Box","","Zoom Box");

print "Hold down the shift, control, option, and/or command keys and click
the mouse or press a key on the keyboard.
  Type command period to quit.\n\n";

	@winList = LMGetWindowList();
	$a = $winList[0]->userState;
	($TLwin, $BRwin) = ($a->topLeft, $a->botRight);
	($left, $top) = ($TLwin->h, $TLwin->v);
	($right, $bottom) = ($BRwin->h, $BRwin->v);
	print "WINDOW RECT: left = $left, top = $top, right = $right,
bottom = $bottom\n";
	print "\n\n--------------\n\n";

while (!$flag) {
	$l = LMGetMouseLocation();
	($h, $v) = ($l->h, $l->v);
	if($hMouse != $h || $vMouse != $v) {
		$hWin = $h - $left;
		$vWin = $v - $top;
		print "SCREEN LOC: H = $h, V = $v   WINDOW LOC: H = $hWin,
V = $vWin \n";
		$hMouse = $h;
		$vMouse = $v;
	}
	WaitNextEvent;
}



sub mouseDownHandler {
	$clk = LMGetMouseLocation();
	($horz, $vert) = ($clk->h, $clk->v);
	$hWin = $horz - $left;
	$vWin = $vert - $top;

	($code, $win) = FindWindow $clk;
	$where = "within the $codes[$code] ";
	if($win =~ "GrafPtr") {
		$winName = GetWTitle $win;
		$where .= "of window \"$winName\" ";
	}

	$modifiers = "";
	$modKey = ($CurrentEvent->modifiers & 256);
	if($modKey == 256) { 	$modifiers .= " command," }
	$modKey = ($CurrentEvent->modifiers & 512);
	if($modKey == 512) { $modifiers .= " shift," }
	$modKey = ($CurrentEvent->modifiers & 1024);
	if($modKey == 1024) { $modifiers .= " caps-lock," }
	$modKey = ($CurrentEvent->modifiers & 2048);
	if($modKey == 2048) { $modifiers .= " option," }
	$modKey = ($CurrentEvent->modifiers & 4096);
	if($modKey == 4096) { $modifiers .= " control," }

	print "\nMOUSE CLICK $where\nSCREEN LOC: H = $h, V = $v   WINDOW
LOC: H = $hWin, V = $vWin\n";
	if ($modifiers) {
		chop($modifiers);
		print " -- MODIFIER KEYS DOWN = $modifiers\n";
	}
	print "\n";
}

sub keyDownHandler {
	my($ev) = @_;
	$k = chr($ev->character);
	print "KEY = $k";

	$modifiers = "";
	$modKey = ($CurrentEvent->modifiers & 384);
	if($modKey == 384) { 	$modifiers .= " command," }
	$modKey = ($CurrentEvent->modifiers & 640);
	if($modKey == 640) { $modifiers .= " shift," }
	$modKey = ($CurrentEvent->modifiers & 1152);
	if($modKey == 1152) { $modifiers .= " caps-lock," }
	$modKey = ($CurrentEvent->modifiers & 2176);
	if($modKey == 2176) { $modifiers .= " option," }
	$modKey = ($CurrentEvent->modifiers & 4224);
	if($modKey == 4224) { $modifiers .= " control," }
	if ($modifiers) {
		chop($modifiers);
		print " -- MODIFIER KEYS DOWN = $modifiers";
	}
	print "\n";
	if (($CurrentEvent->modifiers & 256) == 256 && $k eq ".") { $flag = 1 }
}


__END__