Re: How to disable all keys except <Enter>?




On Mar 29, 2007, at 12:28 PM, Lumir Jasiok wrote:

Thx. for your code, but in MY case it's not working. I have the $window
defined as 'popup' and in this case does'n working
signal_connect->( 'key_press_event' ...

Technically, it *is* working --- the callback gets connected to the signal. However, you simply aren't getting key press events in your popup.

That's because popups are not like normal toplevel windows; focus (and therefore key events) does not go to them by default.


Do you have any idea how to implement fullscreen window which has
disabled all keyboard events?

The "fullscreen" bit is easy enough. By "disabled all keyboard events" i think you actually mean "has performed a global grab", and that's tricky stuff. Tip: when playing with grabs, make sure you have another machine from which you can shell into your development box and kill your misbehaving program. If you mess up while your program has the grab, it can make your display unusable.

Here's a little sample, pattered after the way GtkComboBox handles its popup list.


#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Glib ':constants';
use Gtk2::Gdk::Keysyms;

my $popup = Gtk2::Window->new ('popup');
$popup->set_position ('center');
my $vbox = Gtk2::VBox->new;
$popup->add ($vbox);
my $label = Gtk2::Label->new ("I have grabbed the keyboard and pointer.\n" ."\nDouble-click or hit Esc to close. \n");
$vbox->add ($label);
my $key_label = Gtk2::Label->new ("Keyval:");
$key_label->set_alignment (0.0, 0.5);
$vbox->add ($key_label);

#
# "map-event" is what happens when the window goes onto the screen.
# We'll connect a handler to run immediately after that, and grab the
# keyboard and mouse.
#
$popup->signal_connect_after (map_event => sub {
    $popup->can_focus (1);
    $popup->grab_focus ();

    my $time = Gtk2->get_current_event_time ();

    if (Gtk2::Gdk->pointer_grab ($popup->window, TRUE,
                                 ['button-press-mask',
                                  'button-release-mask'],
                                 undef, undef, $time)) {
        if (Gtk2::Gdk->keyboard_grab ($popup->window, TRUE, $time)) {
            Gtk2->grab_add ($popup);

        } else {
            $popup->get_display->pointer_ungrab ($time);
        }
    }
});

sub popdown {
    my ($popup, $time) = @_;;
    Gtk2->grab_remove ($popup);
    $popup->get_display->pointer_ungrab ($time);
    $popup->get_display->keyboard_ungrab ($time);
    $popup->destroy;
}

$popup->signal_connect (destroy => sub {
    Gtk2->main_quit;
});

$popup->signal_connect (event => sub {
    my ($widget, $event) = @_;

    print "".$event->type."\n";

    if ($event->type eq '2button-press') {
        #
        # Double click.  Kill it.
        #
        popdown ($popup, $event->time);

    } elsif ($event->type eq 'key-press') {
        #
        # Show the value of the pressed key.
        #
        $key_label->set_text ("Keyval: ".$event->keyval." ("
                              .Gtk2::Gdk->keyval_name ($event->keyval)
                              .")");

        if ($event->keyval == $Gtk2::Gdk::Keysyms{Escape}) {
            #
            # Escape; kill it, but not right now.  Give it
            # a little bit of time to show the key before
            # going down.
            #
            Glib::Timeout->add (500, sub {
                popdown ($widget, 0);
                return FALSE;
            });
        }
    }

    return 0;
});

$popup->show_all();
Gtk2->main;

__END__



--
I hate to break it to you, but magic data pixies don't exist.
  -- Simon Cozens





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