List Info

Thread: confused about keyboard focus




confused about keyboard focus
user name
2005-12-30 20:11:47
I'm confused about keyboard focus.  I'm writing code that I
*think*
should make one specific widget receive keyboard input, yet
when I
type at the screen, the keyboard input goes somewhere else.

I've got a big gtk2 app, but I've boiled the issue down to a
simple
demo program:

	#!/usr/bin/perl -w

	use Glib qw/TRUE FALSE/;
	use Gtk2 '-init';


	my $window = Gtk2::Window->new;
	$window->signal_connect (destroy => sub {
Gtk2->main_quit });
	my $vbox = new Gtk2::VBox( FALSE, 0 );
	my $entry1 = Gtk2::Entry->new();
	my $entry2 = Gtk2::Entry->new();
	$entry2->has_focus(TRUE); # <---- IMPORTANT LINE
	$vbox->pack_start( $entry1, FALSE, FALSE, FALSE );
	$vbox->pack_start( $entry2, FALSE, FALSE, FALSE );
	$window->add ($vbox);
	$window->show_all;
	Gtk2->main;

I would expect that when the gtk2 application was focused,
any
characters I'd type would end up in  'entry2' gtk2::entry.

...but they don't.  They end up in 'entry1'.

1) Why?

2) How do I accomplish my goal of making one widget receive
keyboard
input with out (a) making it first on the screen ; (b)
requiring the
user to click or tab around?

Thanks in advance!

TJIC
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
confused about keyboard focus
user name
2005-12-30 21:08:42
On Fri, 2005-12-30 at 15:11 -0500, Travis J.I. Corcoran
wrote:

> 	$entry2->has_focus(TRUE); # <---- IMPORTANT LINE

has_focus just tells the entry that it's supposed to have
focus now and
should thus draw the blinking cursor, etc. that you see. 
Here are three
ways that work:

  Glib::Idle->add (sub { $entry2->grab_focus; 0; });
  Glib::Idle->add (sub { $window->set_focus ($entry2);
0; });
  $vbox->set_focus_child ($entry2);

-- 
Bye,
-Torsten

_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
confused about keyboard focus
user name
2005-12-30 22:24:53
On Dec 30, 2005, at 4:08 PM, Torsten Schoenfeld wrote:

> On Fri, 2005-12-30 at 15:11 -0500, Travis J.I. Corcoran
wrote:
>
>> 	$entry2->has_focus(TRUE); # <---- IMPORTANT
LINE
>
> has_focus just tells the entry that it's supposed to
have focus now  
> and
> should thus draw the blinking cursor, etc. that you
see.

Specifically, this just sets a flag in the widget, without
doing  
anything else.  This is something intended for use by widget
 
implementations, not by application code.


> Here are three ways that work:
>
>   Glib::Idle->add (sub { $entry2->grab_focus; 0;
});
>   Glib::Idle->add (sub { $window->set_focus
($entry2); 0; });

If i recall correctly, the purpose of using the idle here is
to defer  
the focus grab until after the window is on the screen,
since the  
window will try to override the focus as it goes onscreen. 
I've not  
tested this, but i believe an alternative to using an idle
here is to  
perform this operation in an "after" handler for
the window's realize  
or map signals.

>   $vbox->set_focus_child ($entry2);


--
Meg: Brian!  Chris picked his nose and keeps trying to touch
me with  
his finger!
Chris: What good is mining nose gold if I can't share it
with the  
townspeople?
    -- Family Guy


_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )