http://qs1969.pair.com?node_id=633070


in reply to I don't understand a piece of Perl/Tk code

What does Ev mean in this context ?..........Ev is never defined nowhere in the source.....

Those are X window events. They are mentioned in perldoc Tk::bind under the Events section, but they are built so deep into the Tk windowing system, that it is probably in the Tcl c code, from which Perl/Tk is derived. If you look at perldoc Tk::Widget, you will see that every Tk widget has a $widget->pointerx (y) attribute. That is the x and y mouse pointer location relative to the widget itself...i.e. 0,0 is the widget's upper left corner. Tk bind has built in events, for the mouse and keypress activity, called Ev('x'), Ev('y'), Ev('k') and you can bind to those events. Additionally, most widgets will have a method to determine which widget element is located at any of it's pointerx and pointery locations. Here is a simple example:

#!/usr/bin/perl use warnings; use strict; use Tk; my $dx; my $dy; my $mw = MainWindow->new; $mw->geometry("700x600"); my $canvas = $mw->Canvas(-width => 700, -height => 565, -bg => 'black', -borderwidth => 3, -relief => 'sunken', )->pack; my $closebutton = $mw->Button(-text => 'Exit', -command => sub{Tk::exi +t(0)}) ->pack; my $dragster = $canvas->createRectangle(0, 20, 50, 75, -fill => 'red', -tags => ['move'], ); $canvas->bind('move', '<1>', sub {&mobileStart();}); $canvas->bind('move', '<B1-Motion>', sub {&mobileMove();}); $canvas->bind('move', '<ButtonRelease>', sub {&mobileStop();}); MainLoop; sub mobileStart { my $ev = $canvas->XEvent; ($dx, $dy) = (0 - $ev->x, 0 - $ev->y); $canvas->raise('current'); print "START MOVE-> $dx $dy\n"; } sub mobileMove { my $ev = $canvas->XEvent; $canvas->move('current', $ev->x + $dx, $ev->y +$dy); ($dx, $dy) = (0 - $ev->x, 0 - $ev->y); print "MOVING-> $dx $dy\n"; my $color = $canvas->itemcget($dragster,'-fill'); print "color -> $color\n"; #or use bbox here for odd shapes.. bounding box my @coords = $canvas->coords($dragster); print "coords-> @coords\n"; } sub mobileStop{&mobileMove;}

I'm not really a human, but I play one on earth. Cogito ergo sum a bum