Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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; }


In reply to Re: Gtk2-Perl: How to catch shift-clicking of a button? by andal
in thread Gtk2-Perl: How to catch shift-clicking of a button? by Bloehdian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (9)
    As of 2014-04-17 06:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (440 votes), past polls