#!/usr/bin/env perl use strict; use warnings; use Tk; $|++; my $data = { pressed => 0, }; my $mw = MainWindow->new(); my $canvas = $mw->Canvas(-width => 200, -height => 200)->pack(); $canvas->CanvasBind('', sub {$data->{pressed} = 1; print "pressed\t";}); $canvas->CanvasBind('', sub {$data->{pressed} = 0; print "released\n";}); my @items; for my $x (1 .. 4) { my $x1 = $x * 25; my $y1 = $x1; my $x2 = $x1 + 25; my $y2 = $y1 + 25; $items[$x - 1] = $canvas->createRectangle($x1, $y1, $x2, $y2, -fill => 'red', -width => 0); } $canvas->bind($items[$_], '', sub {if ($data->{pressed}) {print "wtf\t"}}) for 0 .. 3; MainLoop();