On 07/09/2012 12:48 PM, Barry Brevik wrote:
> I am lost with this simple app, and I hope I am posting to the correct
> list; the Sourceforge list appears to dead.
>
> Anyway, the problem is with the event model (I guess). I have 2 fields
> and all I want is when the user presses ENTER or TAB in the first field,
> I want to capture the text, and have the focus move to the next field. I
> have Change set for the fields, which is alright, but I fail to see how
> I can capture the RETURN or TAB key. Annoyingly, the LostFocus event
> fires every time a key is pressed, which makes no sense to me.
>
> Below is my simplified code which displays the action. I'm sorry that it
> is about 90 lines in length. Can anyone help?

It seems that the dialogui will overwrite the Enter key binding. Also it 
depends on whether the Textfield is multiline or not.

So..... you can roll your own key checking into these boxes. The revised 
code is below.

I have bound Enter in the username to go to the password box. I have 
bound Enter in the password box to show the username and password in a 
label...

##############################################
use strict;
use warnings;
use Win32;
use Win32::GUI();
use Win32::GUI::Constants qw(VK_RETURN VK_TAB);

my $main = Win32::GUI::Window -> new
(
   -name   => 'Main',
   -title  => 'Test v0.1',
   -width  => 430,
   -height => 200
);

my $monoinput = Win32::GUI::Font -> new(-name => 'Consolas', -size =>
10, -bold => 0, -italic => 0);
my $monolabel = Win32::GUI::Font -> new(-name => 'Consolas', -size =>
10, -bold => 1, -italic => 0);

my $userlabel = $main -> AddLabel
(
   -font => $monolabel,
   -pos  => [10, 22],
   -text => 'username:',
   -foreground => 0xff4444
);

# Create a text input field.
my $userfield = $main -> AddTextfield
(
   -eventmodel   => "byname",
   -name         => "username",
   -align        => "left",
   -pos          => [96, 20],
   -size         => [100, 24],
   -width        => 100,
   -height       => 20,
   -password     => 0,
   -passwordChar => chr(249),
   -background   => 0xffffff,
   -font         => $monoinput,
   -text         => '',
);

my $passlabel = $main -> AddLabel
(
   -font => $monolabel,
   -pos  => [10, 64],
   -text => 'password:',
   -foreground => 0xff4444
);

# Create a text input field.
my $passfield = $main -> AddTextfield
(
   -eventmodel   => "byname",
   -name         => "password",
   -align        => "left",
   -tabstop      => 1,
   -pos          => [96, 60],
   -size         => [100, 24],
   -password     => 1,
   -passwordChar => chr(249),
   -background   => 0xffffff,
   -font         => $monoinput,
   -text         => '',
  # -dialogui => 1      # Enable keyboard navigation like DialogBox
);
my $resultLabel = $main -> AddLabel
(
   -font => $monolabel,
   -pos  => [10, 100],
   -text => ' ',
   -wrap => 0,
   -width => 400,
   -foreground => 0x000000,
   -background => 0xffffff
);

$userfield -> MaxLength(16);
$passfield -> MaxLength(16);

$userfield -> SetFocus();
$main -> Show();
Win32::GUI::Dialog();

exit(0);

#----------------------------------------------------------
sub password_KeyDown {
     my($flags,$key) = @_;
     if($key == VK_RETURN){
         my $string = "Username: ". $userfield->Text(). " Password: ". 
$passfield->Text();
         $resultLabel->Change(-text=>$string);
     }
     elsif ($key == VK_TAB) {
         $userfield ->SelectAll();
         $userfield ->SetFocus();
     }
     return 1;
}
#----------------------------------------------------------
sub username_KeyDown {
     my($flags,$key) = @_;
     if($key == VK_RETURN or $key == VK_TAB){
         $passfield ->SelectAll();
         $passfield ->SetFocus();
     }
     return 1;
}
#----------------------------------------------------------
sub Main_Terminate {-1;}

################################################

Jack D.
>
> Thank you,
>
> Barry Brevik
> ---------------------------
> use strict;
> use warnings;
> use Win32;
> use Win32::GUI();
>
> my $main = Win32::GUI::Window -> new
> (
>    -name   => 'Main',
>    -title  => 'Test v0.1',
>    -width  => 400,
>    -height => 200
> );
>
> my $monoinput = Win32::GUI::Font -> new(-name => 'Consolas', -size =>
> 10, -bold => 0, -italic => 0);
> my $monolabel = Win32::GUI::Font -> new(-name => 'Consolas', -size =>
> 10, -bold => 1, -italic => 0);
>
> my $userlabel = $main -> AddLabel
> (
>    -font => $monolabel,
>    -pos  => [10, 22],
>    -text => 'username:',
>    -foreground => 0xff4444
> );
>
> # Create a text input field.
> my $userfield = $main -> AddTextfield
> (
>    -eventmodel   => "byname",
>    -name         => "username",
>    -align        => "left",
>    -tabstop      => 1,
>    -pos          => [76, 20],
>    -size         => [100, 24],
>    -width        => 100,
>    -height       => 20,
>    -password     => 0,
>    -passwordChar => chr(249),
>    -background   => 0xffffff,
>    -font         => $monoinput,
>    -text         => '',
>    -dialogui => 1      # Enable keyboard navigation like DialogBox
> );
>
> my $passlabel = $main -> AddLabel
> (
>    -font => $monolabel,
>    -pos  => [10, 64],
>    -text => 'password:',
>    -foreground => 0xff4444
> );
>
> # Create a text input field.
> my $passfield = $main -> AddTextfield
> (
>    -eventmodel   => "byname",
>    -name         => "password",
>    -align        => "left",
>    -tabstop      => 1,
>    -pos          => [76, 60],
>    -size         => [100, 24],
>    -password     => 1,
>    -passwordChar => chr(249),
>    -background   => 0xffffff,
>    -font         => $monoinput,
>    -text         => '',
>    -dialogui => 1      # Enable keyboard navigation like DialogBox
> );
>
> $userfield -> MaxLength(16);
> $passfield -> MaxLength(16);
>
> $main -> username -> SetFocus();
> $main -> Show();
> Win32::GUI::Dialog();
>
> exit(0);
>
> #----------------------------------------------------------
> sub username_Change
> {
>    Win32::MsgBox("Username field has changed", 48, "Message from
> Username");
>    $userfield -> SetFocus();
> }
>
> #----------------------------------------------------------
> sub username_LostFocus
> {
>    Win32::MsgBox("Username field has lost focus", 48, "Message from
> Password");
> }
>
> #----------------------------------------------------------
> sub Main_Terminate {-1;}
>
> _______________________________________________
> Perl-Win32-Users mailing list
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to