2016-09-02 27 views
0

我希望我的GUI能夠對單個擊鍵作出反應。Win32 :: GUI :: AcceleratorTable的語法

只要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); 
+0

最起碼,必須'-accel => $ A',這也意味着你將需要移動的初始化'$ A'。 ('-accel','-accelerator'和'-accelerratortable'是別名。) – ikegami

回答

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, 
); 

...萬歲 - 它可以工作:-)