Win32 :: GUI :: AcceleratorTable的语法

时间:2016-09-02 04:04:09

标签: perl

我希望我的GUI能够对单键击做出反应。

只要windows对象具有焦点,这与-keydown选项一起正常工作。

要获得有关没有焦点的击键的反馈,我想使用Win32 :: GUI :: AcceleratorTable。

在文档中,我使用了代码片段,您可以在我的代码中看到。但我对使用的语法感到困惑。一旦文档说Win32 :: GUI :: Accelerator,其他人就提到了Win32 :: AcceleratorTable(即使CPAN上没有这样的包)。与选项-accel,-accelerators和-acceleratortable相同。

代码如何获得有关击键键码的反馈?

这是我的(不工作)代码:

use strict;
use warnings;

use Win32::GUI();

# define main window
my $window = Win32::GUI::Window->new(
    -name   => 'MainWindow',
    -width  => 250,
    -height => 200,
    -text   => 'keys',
    -accel  => Win32::GUI::AcceleratorTable,    # ????
    # or -accelerator, or -acceleratortable     # ????
);

$window->AddLabel( -name => 'lblStatus', -top => 5, -left => 5, -text => "pressed key", );

# a text field to give feedback about the key press
$window->AddTextfield( -name => 'txtStatus',
    -width => 80,
    -height => 20,
    -top => 20,
    -left => 5,
    -tip => "displays value of key",
);

# I took this from CPAN Win32::GUI::AcceleratorTable which should at least print
# "Hello" on the console if one presses the lowercase letter "b" on the keyboard
$A = Win32::GUI::AcceleratorTable->new(
        "Ctrl-X"       => "Close",
        "Shift-N"      => "New",
        "Ctrl-Alt-Del" => "Reboot",
        "b"      => sub { print "Hello\n"; },
);

# display app
$window->Show();

# start of event handler
Win32::GUI::Dialog();

exit (0);

1 个答案:

答案 0 :(得分:0)

我根据ikegami的提示更改了代码,解决了我的问题。谢谢池上!

# first initialize AcceleratorTable...
my $A = new Win32::GUI::AcceleratorTable(
        "Ctrl-X"       => "Close",
        "Shift-N"      => "New",
        "Ctrl-Alt-Del" => "Reboot",
        "b"      => sub { print "Hello\n"; },
);

# ...and then define main window using the -accel option
my $window = Win32::GUI::Window->new(
    -name   => 'MainWindow',
    -width  => 250,
    -height => 200,
    -text   => 'keys',
    -accel  => $A,
);

...瞧瞧 - 它有效: - )

相关问题