简体   繁体   中英

Catching key event in wxperl

I want to capture key presses at top level in a wxperl app. From the docs it seems one should use one of EVT_CHAR EVT_CHAR_HOOK EVT_KEY_DOWN .

In the following code, EVT_LEFT_DOWN works, but EVT_KEY_DOWN (or any of the alternatives) doesn't.

Anyone knows what to do? OS X, btw.

use warnings;
use strict;
use Wx;

package MyApp;
use base 'Wx::App';
use Wx::Event
    qw(EVT_LEFT_DOWN EVT_CHAR EVT_CHAR_HOOK EVT_KEY_DOWN);

sub OnInit {
    my( $self ) = @_;

    my $frame = Wx::Frame->new(
        undef,
        -1,
        'Hello World',
        [-1, -1],
        [250, 150],
    );

    EVT_KEY_DOWN( $frame, \&on_event ); # doesn't work
    # EVT_LEFT_DOWN( $frame, \&on_event ); # works
    $frame->Show( 1 );
    return 1;
}

sub on_event {
    print "peekaboo\n";
}

package main;

my $app = MyApp->new;
$app->MainLoop;

This should work if the window has focus (this is a bit tautological, but, by definition, keyboard events are sent only to the focused window). Under Mac, you could have problems with the frame not having focus initially if you're not running the program from a proper (Mac) bundle, but clicking on it should still give it the focus. If this still doesn't work, try creating a wxPanel as the child of the frame and bind to the events on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM