Thank you for the advice Rocco, I've done what you suggested and 'hidden' the blessed postback in an unblessed one.

The one thing I'm still not sure about is do you how I can access the keysym information once the "ev_key" event has fired? I have tried using $_[ARG0] and $_[ARG1], but without success.

An idea I had was to put Ev('K') in the callback (as in 1. below) but I couldn't get it to work with postback() because the entire callback needed to be surrounded with [] (according to http://search.cpan.org/~ni-s/Tk-804.025_beta13/pod/callbacks.pod).

In the code without POE, I used this:

******************************1. Without POE****************************

#Any keypresses, call the keypress sub
#Ev('K') provides the keysym info
$main->bind( '<Any-KeyPress>' => [\&keypress, Ev('K')] );
        
sub keypress
{
        my ($window, $keysym) = @_;
        print "$keysym\n";          # e.g. comma
}


When using POE, I've done the following:


******************************2. With POE*******************************

POE::Session->create (
                     inline_states => {
                        _start => \&ui_start,
                        ev_key => \&ui_keypress
                      },
                      );


sub ui_start {


        #Create main window
        $_[HEAP]->{MainWindow} = $poe_main_window;

        #On keypress, fire event
        my $post_back = $_[SESSION]->postback( "ev_key" );
        $poe_main_window->bind(
                        '<Any-KeyPress>' => sub {$post_back->(@_);}
                               );
        
}

sub ui_keypress {

        my ($c) = $_[ARG1];
        my $event_obj = $c->XEvent; #Get Event Object - line 82
        my $keysym= $event_obj->K;
        print "The $keysym key was pressed\n";

}

The above script (2.) gives me this error on pressing a key:

Tk::Error: Can't call method "XEvent" on unblessed reference at lingo.pl line 82.
Tk::After::once at D:/perl/site/lib/Tk/After.pm line 83
[once,[{},after#62,0,once,[\&POE::Kernel::_loop_event_callback]]]
("after" script)



Sorry to keep bothering you with newbie questions - I'm learning.


Thanks for your time.
Regards,

James.

Rocco Caputo wrote:
On Wed, Dec 31, 2003 at 01:00:05PM +0000, James Brown wrote:

Hi All,

I need a bit of help here. I'm trying get keyboard input (under UNIX and Windows) into a POE app which I'm developing. Having tried Curses and ReadLine, which work fine on UNIX, but won't install on Windows, I think I'll have to explore the POE-Tk route.

I've managed to get the code working without POE, but (being new to POE) I struggle to understand how to use the postback() and bind() functions.

...etc...


[...]


Basically, I see this error:

Tk::Error: Can't call method "XEvent" on unblessed reference at **line 79.**Tk::After::once at D:/perl/site/lib/Tk/After.pm line 83
[once,[{},after#43,0,once,[\&POE::Kernel::_loop_event_callback]]]
("after" script)


I think my problem is that when the keypress event occurs, the "ev_key" happens, but I'm not so sure of how to extract the parameters that Tk would normally send along with this event.


Tk is not passing the event information to POE.

As you know, postbacks are anonymous subroutine references that post POE
events when they're called.  They're used as a thin, flexible interface
between POE and Tk, among other things.

Postbacks are blessed, and their DESTROY methods are used to notify POE
when Tk is done with them.  From Tk's point of view, the only difference
between a callback and a postback is this blessing.

For some reason, Tk does not pass parameters to a blessed callback.  To
demonstrate:

#!/usr/bin/perl

  use warnings;
  use strict;

use Tk;

my $mw = Tk::MainWindow->new();

my $sub = \&callback;

$mw->bind("<Any-KeyPress>" => $sub);

  Tk::MainLoop();
  exit 0;

  sub callback {
      warn "@_";
  }

displays

Tk::MainWindow=HASH(0x828b7e4) at tk-callback.perl line 18.

A subtle change

#!/usr/bin/perl

  use warnings;
  use strict;

use Tk;

my $mw = Tk::MainWindow->new();

my $sub = bless \&callback, "whatever";

$mw->bind("<Any-KeyPress>" => $sub);

  Tk::MainLoop();
  exit 0;

  sub callback {
      warn "@_";
  }

changes the output to

Warning: something's wrong at tk-callback.perl line 18.

There is a workaround.  Tk wants an unblessed callback, so you can hide
the blessed one inside a plain one.

  ##Start Event##
  sub ui_start {
    my $pb = $_[SESSION]->postback("ev_key");
    $poe_main_window->bind (
      '<Any-KeyPress>' => sub {
        $pb->(@_);
      }
    )
  }

A copy of this message has been Cc'd to <[EMAIL PROTECTED]>.






Reply via email to