First of all, Look at the demo that comes with the Goo module. It has all sorts of usage examples contained in it. Second to move an item, see Goo Canvas and transparent images

Back to your problem:

$item->signal_connect ('event' => \&event_handler); probably should be
$canvas->signal_connect ('event' => \&event_handler);

I had trouble getting keyboard functionality with Goo myself in the past, and this is what I came up with. I had to resort to using a package to grab the keyboard first, it works, but gives "wide print" errors on the F* keys, but you should be able to fix that. Someone on the maillist may know how to do this without resorting to a package.

#!/usr/bin/perl -w use strict; package Goo::Graph; use Gtk2; use Glib qw/TRUE FALSE/; use base 'Goo::Canvas'; use Gtk2::Gdk::Keysyms; sub new { my $class = shift; my %params = ( @_ ); my $self = bless Goo::Canvas->new(), $class; # size settings $self->set_size_request($params{'width'},$params{'height'} ); $self->set_bounds(0, 0, $params{'xbound'},$params{'ybound'}); $self->{'root'} = $self->get_root_item(); $self->{'scale'} = 1; $self->set_color( %params ); $self->set_fonts( %params ); $self->signal_connect ('expose_event' => \&on_expose_event); # $window->signal_connect(event_after => \&event_after); return $self; } sub on_expose_event(){ my ($self,$event) = @_; $self->can_focus(TRUE); $self->grab_focus($self->{'root'}); $self->signal_connect('button-press-event', \&on_can_button_press); $self->signal_connect_after('key_press_event', \&on_key_press ); return FALSE; } sub set_color { my $self = shift; my %params = @_; $self->{'bg'} = Gtk2::Gdk::Color->new( @{$params{'bg'}} ); $self->modify_base('normal', $self->{'bg'}); $self->{'black'} = Gtk2::Gdk::Color->new (0x0000,0x0000,0x0000); $self->{'white'} = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF); } sub set_fonts { my $self = shift; my %params = @_; # get font size for height spacing my $font = $params{'font'}; my $font_desc = Gtk2::Pango::FontDescription->from_string($font); my $pangolayout = $self->create_pango_layout(""); $pangolayout->set_font_description($font_desc); my $markup = "<span font_family ='Arial' foreground = '#000000' size = '14000' weight = 'bold'> 'W' </span>"; $pangolayout->set_markup( $markup ); my ($lw,$lh) = $pangolayout->get_pixel_size(); $self->{'lwidth'} = $lw; $self->{'lheight'} = $lh; print $self->{'lheight'},"\n"; $markup = "<span font_family ='Arial ' foreground = '#000000' size = '14000' weight = 'bold'> Goo </span>"; my $text = Goo::Canvas::Text->new( $self->{'root'}, $markup, 100 , 100 , -1, 'center', 'use markup' => 1, ); my $y = 100 + $self->{'lheight'}; $text = Goo::Canvas::Text->new( $self->{'root'}, $markup, 100 , $y , -1, 'center', 'use markup' => 1, ); return 1; } sub on_can_button_press { # print "@_\n"; my ($widget, $event ) = @_; print $widget ,' ',$event->type, ' ','button',' ',$event->button +,"\n"; my ($x,$y) = ($event->x,$event->y); print "$x $y\n"; return 0; } sub on_key_press { # print "@_\n"; my ( $self, $event ) = @_; # print $event->type,"\n"; print "key was ", chr( $event->keyval ), "\n"; return 0; } 1; package main; use Gtk2 -init; my $window = Gtk2::Window->new; $window->set_title( 'GooGraph' ); $window->set_border_width( 6 ); $window->signal_connect( delete_event => sub { Gtk2->main_quit; 1 } ); $window->set_size_request(640, 600); my $vbox = Gtk2::VBox->new; $window->add( $vbox ); $vbox->show; my $swin = Gtk2::ScrolledWindow->new; $swin->set_shadow_type('in'); $swin->show; my $graph = Goo::Graph->new( width => 1000, height => 1000, xbound => 5000, ybound => 5000, bg => [0xee22, 0xee22, 0xff22], font => 'Arial Bold 14', ); $swin->add($graph); $vbox->pack_start( $swin, 1, 1, 0 ); $window->show_all; #$graph->can_focus(1); #$graph->grab_focus($graph->get_root_item()); Gtk2->main; __END__

and here is an example of how to control items

#!/usr/bin/perl -w use strict; use warnings; use Goo::Canvas; use Gtk2 '-init'; use Glib qw(TRUE FALSE); my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_default_size(680, 600); my $vbox = Gtk2::VBox->new; $vbox->set_border_width(4); $vbox->show; my $hbox = Gtk2::HBox->new(FALSE, 4); $vbox->pack_start($hbox, FALSE, FALSE, 0); $hbox->show; $window->add($vbox); my $swin = Gtk2::ScrolledWindow->new; $swin->set_shadow_type('in'); $vbox->pack_start($swin, 1, 1, 0); my $cwidth = 1000; my $cheight = 1000; my $canvas = Goo::Canvas->new(); $canvas->set_size_request(600, 450); # minimum size on screen $canvas->set_bounds(0, 0, $cwidth, $cheight); # scrollregion $swin->add($canvas); my $root = $canvas->get_root_item(); $canvas->signal_connect('button-press-event', \&on_can_button_press); $canvas->signal_connect (event => \&event_handler); my $g = Goo::Canvas::Group->new($root); $g->translate(310,300); my $button = Gtk2::Button->new ('Draw'); $button->signal_connect (clicked => \&clicked ); $hbox->pack_start($button, FALSE, FALSE, 10); $button->show(); $window->show_all(); Gtk2->main; sub clicked { my $cur_lab = $button->get_label(); #print "$cur_lab\n"; if($cur_lab eq 'Draw'){ $button->set_label('Clear'); print "Starting action\n"; &draw(); }elsif($cur_lab eq 'Clear'){ $button->set_label('Draw'); print "Stopping action\n"; &clear; } } sub draw{ my $markup = "<span font_family ='Arial ' foreground = '#000000' size = '18000' weight = 'bold'> Remove items with right click or clear button +</span>"; my $text = Goo::Canvas::Text->new( $root, $markup, 10 , 10 , -1, #-1 means a single line, no space constraint 'nw', 'use markup' => 1, ); # item = Goo::Canvas::Ellipse->new ($parent, $center_x, $center_y, + # $radius_x, $radius_y, ...) my $e0 = Goo::Canvas::Ellipse->new( $g, 0, 0,300, 225, 'stroke-color' => 'black', 'line-width' => 2, 'fill-color-rgba' => 0xffb3f111, 'title' => 'foo', ); my $e1 = Goo::Canvas::Ellipse->new( $g, 100, 100,300, 225, 'stroke-color' => 'black', 'line-width' => 2, 'fill-color-rgba' => 0x3cb3f111, ); print 'g ',$g->get_n_children,"\n"; print 'root ',$root->get_n_children,"\n"; print $g->find_child ($e0),"\n"; print $g->find_child ($e1),"\n"; return 1; } sub clear{ #dosn't work, the number seems to shift as you remove # $g->remove_child(0); # $g->remove_child(1); #works # $g->remove_child(1); # $g->remove_child(0); #works for(1..$g->get_n_children){ $g->remove_child(0); } # or you can remove the group from root # works #$root->remove_child(0); return 1; } sub on_can_button_press { my ( $widget, $event ) = @_; # print $widget ,' ',$event->type,"\n"; my ($x,$y) = ($event->x,$event->y); print "$x $y\n"; return TRUE; } $canvas->signal_connect (event => \&event_handler); sub event_handler{ my ( $widget, $event ) = @_; # print $widget ,' ',$event->type,"\n"; #remove items with right click if ($event->type eq "button-press" && $event->button == 3){ my ($x,$y) = ($event->x,$event->y); print "right click\n"; my $item = $canvas->get_item_at($x,$y,TRUE); # check for click on empty space if( ! defined $item ){return 1} my $parent = $item->get_parent; print "$parent\n"; my $num = $parent->find_child ($item); print "$num\n"; $parent->remove_child($num); return 0; } if ( $event->type eq "button-release" ) { return 0; } if ( $event->type eq "focus-change" ) { return 0; } if ( $event->type eq "expose" ) { return 0; } Gtk2->main_iteration while Gtk2->events_pending; return 0; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

In reply to Re: Moving Goo::Canvas canvas item using keyboard keys. by zentara
in thread Moving Goo::Canvas canvas item using keyboard keys. by msinfo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.