I am trying to create an AcceleratorTable that allows me when I press 1-9 to press the GUI buttons on the screen.

I have added the AccelaratorTable so ALT+1 should depress the button_1 but it doesn't.

I know there is some effect because with the code below when I press alt+1 no sound occurs but alt plus other keys makes a beep.

But how do I link the accelarator table to do the same thing as clicking the button_1 widget?



#!/usr/bin/perl -w --
# generated by wxGlade 0.6.3 on Fri Aug 14 14:57:39 2009
# To get wxPerl visit http://wxPerl.sourceforge.net/

use Wx 0.15 qw[:allclasses];
use strict;

package MyFrame;

use Wx qw[:everything];
use Wx::Event qw[EVT_BUTTON EVT_MENU EVT_KEY_DOWN EVT_CHAR];
use base qw(Wx::Frame);
use strict;

sub new {
   my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_;
   $parent = undef              unless defined $parent;
   $id     = -1                 unless defined $id;
   $title  = ""                 unless defined $title;
   $pos    = wxDefaultPosition  unless defined $pos;
   $size   = wxDefaultSize      unless defined $size;
   $name   = ""                 unless defined $name;

# begin wxGlade: MyFrame::new

   $style = wxDEFAULT_FRAME_STYLE
       unless defined $style;
$self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name );
$self->{button_1} = Wx::Button->new($self, 201, "1");
$self->{button_2} = Wx::Button->new($self, 202, "2");
$self->{button_3} = Wx::Button->new($self, 203, "3");


   $self->__set_properties();
   $self->__do_layout();
EVT_BUTTON($self, $self->{button_1}->GetId, \&keyPress);
   EVT_BUTTON($self, $self->{button_2}->GetId, \&keyPress);
   EVT_BUTTON($self, $self->{button_3}->GetId, \&keyPress);
my $atable = new Wx::AcceleratorTable( [ wxACCEL_ALT, ord('1'), $self->{button_1} , ] );
   $self->SetAcceleratorTable( $atable );

# end wxGlade return $self;

}


sub __set_properties {
   my $self = shift;

# begin wxGlade: MyFrame::__set_properties

   $self->SetTitle("A sample keypad program");

# end wxGlade
}

sub __do_layout {
   my $self = shift;

# begin wxGlade: MyFrame::__do_layout

   $self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL);
   $self->{grid_sizer_1} = Wx::GridSizer->new(3, 3, 0, 0);
   $self->{grid_sizer_1}->Add($self->{button_1}, 0, 0, 0);
   $self->{grid_sizer_1}->Add($self->{button_2}, 0, 0, 0);
   $self->{grid_sizer_1}->Add($self->{button_3}, 0, 0, 0);
$self->{sizer_1}->Add($self->{grid_sizer_1}, 1, wxEXPAND, 0);
   $self->SetSizer($self->{sizer_1});
   $self->{sizer_1}->Fit($self);
   $self->Layout();

# end wxGlade
}

sub keyPress {
   my ($self, $event) = @_;
# wxGlade: MyFrame::keyPress <event_handler>

   #warn "Event handler (keyPress) not implemented";


# end wxGlade

       # this is the key to getting which button is clicked
   my $key = $event->GetEventObject()->GetLabel();
   my $keyid = $event->GetEventObject()->GetId();

Wx::MessageBox("You pressed: $key\nKey ID: $keyid", "wxPerl Sample", wxICON_INFORMATION );
    $event->Skip;

}

# end of class MyFrame

1;

1;

package main;

unless(caller){
   local *Wx::App::OnInit = sub{1};
   my $app = Wx::App->new();
   Wx::InitAllImageHandlers();

   my $frame_1 = MyFrame->new();
$app->SetTopWindow($frame_1);
   $frame_1->Show(1);
   $app->MainLoop();
}

Reply via email to