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;}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.