Dear Monks, this has been really confusing me. Say we have MainWindow $mw that contains a widget $w. Say that $w belongs to a class that has default bindings for the events <Control-v> and <<Paste>> built in and bound to the same paste routine. Let's say you want to make a paste occur by using the eventGenerate method. For the real event, <Control-v> it appears you can call either $mw->eventGenerate('<Control-v>') or $w->eventGenerate('<Control-v>'). That is, eventGenerate can be called as a method of either the MainWindow or the widget, and it works the same. However, for the virtual event, only $w->eventGenerate('<<Paste>>') will work. trying to generate the virtual event off of the MainWindow ($mw->eventGenerate('<<Paste>>')doesn't do anything.

Here's some complete example code below. Examples 1, 2, and 3, work. Example 4 does not. ???

Example 1.

#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; $mw->Button(-text => "Paste", -command => \&DoPaste)->pack(); my $t = $mw->Text(-width => 30, -height => 5)->pack(); $t->focus; sub DoPaste { print "DoPaste\n"; $t->eventGenerate('<Control-v>'); } Tk->MainLoop;

Example 2

#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; $mw->Button(-text => "Paste", -command => \&DoPaste)->pack(); my $t = $mw->Text(-width => 30, -height => 5)->pack(); $t->focus; sub DoPaste { print "DoPaste\n"; $mw->eventGenerate('<Control-v>'); } Tk->MainLoop;

Example 3 (the virtual event generated off of the widget)

#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; $mw->Button(-text => "Paste", -command => \&DoPaste)->pack(); my $t = $mw->Text(-width => 30, -height => 5)->pack(); $t->focus; sub DoPaste { print "DoPaste\n"; $t->eventGenerate('<<Paste>>'); } Tk->MainLoop;

Example 4. Paste button doesn't work. The widget's virtual paste event is not being generated, it seems. If the virtual paste binding weren't tagged with the widget's class then I think it would work. But the real control-v event works regardless. How rude!

#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; $mw->Button(-text => "Paste", -command => \&DoPaste)->pack(); my $t = $mw->Text(-width => 30, -height => 5)->pack(); $t->focus; sub DoPaste { print "DoPaste\n"; $mw->eventGenerate('<<Paste>>'); } Tk->MainLoop;

Can anybody explain why this works the way it does. Why generating a virtual event doesn't seem to work the same as generating a 'real' event? My initial impulse is to assume this is a bug, but I make that assumption a lot, and I've never turned out to be right. It's never a bug, I'm just always misunderstanding something. What am I misunderstanding?


In reply to virtual events and real events with eventGenerate seem to not work the same... by gleepglop

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.