Dave Ackerman notified me that he got inconsistent results with the mouse
location script I sent earlier in this tread.  He sent sample code that
demonstrated this and suggested that adding a call to GlobalToLocal() would
produce more consistent results. After a bit of testing I agree whole
heartedly with Dave. The following is the much more reliable updated
script. Thanks Dave!

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";

    $~="LOC";
        @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(); # get screen location
        ($h, $v) = ($l->h, $l->v);

        my $gtl =       GlobalToLocal ($l); # get window location (thanks
to Dave Ackerman)
        ($hLocal, $vLocal) = ($gtl->h, $gtl->v);

        if($hMouse != $h || $vMouse != $v) {
                write;
                $hMouse = $h;
                $vMouse = $v;
        }
        WaitNextEvent;
}

 $~="STDOUT";
exit(0);


format LOC =
  SCREEN LOC: H = @<<<  V = @<<<   WINDOW LOC: H = @<<<  V = @<<<
  $h, $v, $hLocal, $vLocal
.


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__






Reply via email to