Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Gtk2-Perl: How to catch shift-clicking of a button?

by Bloehdian (Beadle)
on Nov 12, 2012 at 18:28 UTC ( [id://1003488]=perlquestion: print w/replies, xml ) Need Help??

Bloehdian has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I am trying to detect whether the shift key is pressed when a button is clicked. Since I did not find a complete prescription for what I am trying to do in one of the documents on Gtk2 I have at hand I "collected" information from several different sources. Therefore I am not sure at all whether the approach distilled into the test program is correct.

Currently, even execution of this prog fails due to the following compilation error:

Bareword "GDK_SHIFT_MASK" not allowed while "strict subs" in use

This is allegeable since this constant is not exported by any of the modules of the toolkit (i found it in the description of the C-API of Gtk).

So, my questions are as follows:

1. Apart from the obviously wrong bit mask: is my approach to the problem solution correct?

2. Which bit mask do I have to use to detect whether the the shift key was pressed simultaneously with the mouse click?

Perl: 5.8.8

Glib: 1.260

Gtk2: 1.270

OS: Red Hat 4.1.2-46 (Kernel: 2.6.18-164.el5)

The test program:

#!/usr/bin/perl -w use strict; use diagnostics; use Gtk2 -init; my $mw = Gtk2::Window->new(); $mw->signal_connect( 'delete_event' => sub { Gtk2->main_quit; } ); my $button=Gtk2::Button->new_from_stock( 'gtk-add' ); $button->add_events( 'key-press-mask' ); $button->signal_connect( 'clicked' => sub { my ( $widget, $event ) = @_; action( $widget, $event ) } ); $mw->add( $button ); $mw->show_all; Gtk2->main; sub action { my ( $widget, $event ) = @_; if ( ($event->state() & GDK_SHIFT_MASK) == GDK_SHIFT_MASK ) { print "The shift key was pressed\n"; } else { print "The shift key was NOT pressed\n"; } }

Thx

Bloehdian

Replies are listed 'Best First'.
Re: Gtk2-Perl: How to catch shift-clicking of a button?
by andal (Hermit) on Nov 13, 2012 at 08:15 UTC

    There are few problems that you have to resolve. First of all. The "key-press-event" signal goes only to widgets that have focus and to the top-level window. Normal button does not have focus until it is clicked, or until you move focus to it using Tab. I would recommend to associate "key-press-event" handler not with button, but with some area that will definitely have focus when you press the Shift button.

    Then comes the event handling. You need both key-press-event and key-release-event handlers. The first one would set some internal variable when it receives event notifying that the Shift was pressed. The second one would clear that variable when the Shift is released. The "clicked" event handler shall check that variable to see if it is set or not set. In the events for key you can even change the label of the button or do some other stuff.

    Note. The "key-press-event" and "key-release-event" handlers should wait for event containing Shift_L or Shift_R as keyval. Here's the example program.

    #!/usr/bin/perl use strict; use Gtk2 -init; use Gtk2::Gdk::Keysyms; my $win = Gtk2::Window->new('toplevel'); $win->signal_connect(destroy => sub { Gtk2->main_quit }); $win->signal_connect(key_press_event => \&report_press); $win->signal_connect(key_release_event => \&report_release); my $but = Gtk2::Button->new('No Shift'); $but->signal_connect(clicked => \&report_click); $win->add($but); $win->show_all; Gtk2->main; sub report_click { my $w = shift; print $w->get_label, "\n"; } sub report_press { my $w = shift; my $ev = shift; if($ev->keyval == $Gtk2::Gdk::Keysyms{Shift_L} || $ev->keyval == $Gtk2::Gdk::Keysyms{Shift_R}) { $but->set_label("With Shift"); } return; } sub report_release { my $w = shift; my $ev = shift; if($ev->keyval == $Gtk2::Gdk::Keysyms{Shift_L} || $ev->keyval == $Gtk2::Gdk::Keysyms{Shift_R}) { $but->set_label("No Shift"); } return; }

      Nope, the OP's approach is correct, just needs to fix the typos :) 90% of the way there.

      Has to find what exports GDK_SHIFT_MASK.

      Has to use  Gtk2->get_current_event because the callback doesn't get an event object

      update: after some basic debugging

      Gtk2::Gdk::Event::Button isa Gtk2::Gdk::Event hasa $modifiertype = $event->get_state which are flags Gtk2::Gdk::ModifierType

      #!/usr/bin/perl -w use strict; use diagnostics; use Gtk2 -init; my $mw = Gtk2::Window->new(); $mw->signal_connect( 'delete_event' => sub { Gtk2->main_quit; } ); my $button=Gtk2::Button->new_from_stock( 'gtk-add' ); $button->add_events( 'key-press-mask' ); $button->signal_connect( 'clicked' => sub { use Data::Dump; my $state = Gtk2->get_current_event->get_state ; dd[ @_, $state ]; dd[ $state->as_arrayref ]; } ); $mw->add( $button ); $mw->show_all; Gtk2->main; __END__ [bless(do{\(my $o = 257)}, "Gtk2::Gdk::ModifierType")] $ perl -MData::Dump -MGtk2 -e " dd [ \%Gtk2::Gdk::ModifierType:: ] do { my $a = [{ ISA => *Gtk2::Gdk::ModifierType::ISA }]; $a->[0]{ISA} = ["Glib::Flags"]; $a; } $ perl -MData::Dump -MGtk2 -e " dd [ \%Glib::Flags:: ] do { my $a = [ { "(!=" => *Glib::Flags::(!=, "(\"\"" => *Glib::Flags::("", "(&" => *Glib::Flags::(&, "()" => *Glib::Flags::(), "(*" => *Glib::Flags::(*, "(+" => *Glib::Flags::(+, "(-" => *Glib::Flags::(-, "(/" => *Glib::Flags::(/, "(==" => *Glib::Flags::(==, "(>=" => *Glib::Flags::(>=, "(\@{}" => *Glib::Flags::(@{}, "(^" => *Glib::Flags::(^, "(bool" => *Glib::Flags::(bool, "(eq" => *Glib::Flags::(eq, "(ne" => *Glib::Flags::(ne, "(|" => *Glib::Flags::(|, "__ANON__" => *Glib::Flags::__ANON__, "all" => *Glib::Flags::all, "as_arrayref" => *Glib::Flags::as_arrayref, "BEGIN" => *Glib::Flags::BEGIN, "bool" => *Glib::Flags::bool, "eq" => *Glib::Flags::eq, "ge" => *Glib::Flags::ge, "intersect" => *Glib::Flags::intersect, "ne" => *Glib::Flags::ne, "new" => *Glib::Flags::new, "OVERLOAD" => *Glib::Flags::OVERLOAD, "sub" => *Glib::Flags::sub, "union" => *Glib::Flags::union, "xor" => *Glib::Flags::xor, }, ]; $a->[0]{"()"} = \1; $a->[0]{"OVERLOAD"} = { dummy => 1 }; $a; } [ bless({}, "Gtk2::Button"), bless(do{\(my $o = 257)}, "Gtk2::Gdk::ModifierType"), ] [["shift-mask", "button1-mask"]]
      I figured it out, examples in Glib, Glib::Flags
      my $state = Gtk2->get_current_event->get_state ; exit warn "How dare you shift" if $state * "shift-mask";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1003488]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found