Re: enter in entry fields; modal windows




On Oct 7, 2006, at 1:46 PM, Michael Hartmann wrote:

Hi everybody,

I'm using this code to run code when someone presses enter in a entry widget:

      $entry->signal_connect("key_press_event" => sub {
        use Gtk2::Gdk::Keysyms;
        my $key = {reverse %Gtk2::Gdk::Keysyms}->{$_[1]->keyval};
        if($key =~ m/^(KP_Enter)|(Return)$/) {
          $button->clicked;
        }
      } );

In general, the -event signals want a boolean return value, saying whether to continue event propagation. You're not explicitly returning anything, so the behavior may be unexpected.


In the documentation I've found activates_default:
’activates-default’ (boolean : readable / writable / private)
Whether to activate the default widget (such as the default button
           in a dialog) when Enter is pressed

However, I don't understand how to use it. How can I define the button that is
activated?

The "default" button is usually marked by a dark ring. Use Gtk2::Widget::can_default to allow a widget to take the default. Gtk2::Widget::has_default tells you whether the widget is the default, and Gtk2::Widget::grab_default makes a widget the default widget for a window.

#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Glib qw(TRUE);
my $window = Gtk2::Window->new;
my $vbox = Gtk2::VBox->new;
my $entry = Gtk2::Entry->new;
my $button = Gtk2::Button->new ("clicky");

$window->add ($vbox);
$vbox->add ($entry);
$vbox->add ($button);

# Make $button the default widget in this window.
$button->can_default (TRUE);
$button->grab_default;

# Make typing "Enter" in $entry activate $button.
$entry->set (activates_default => TRUE);

# And, so you can tell the button was clicked...
$button->signal_connect (clicked => sub { print "You clicked me!\n" });

$window->signal_connect (destroy => sub {Gtk2->main_quit});
$window->show_all;
Gtk2->main;
__END__


I know there is more than one way to do it, but in this partitcular case: Is
there a more elegant way to do it?

"Activates default" is how you want to do it. That's what it's designed for.


Another problem: I have two windows and one window is modal. How can I prevent that the window that is not modal gets the focus. At the moment all widgets
are not sensitive, but the window can still get the focus.

Do you mean that it is in front, or that the keyboard focus is on it? $window->present may be what you want.

--
Brian: If i recall correctly, this is the physics department.
Chris: That explains all that gravity.
    -- Family Guy, "The Story on Page One"




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]