talwyn has asked for the wisdom of the Perl Monks concerning the following question:

Basically I want the equivalent of the pascal readln or perl $input = <STDIN>; from within a textbox.I tried using the Change() event to use the Text() method; this does allow me to gain access to the information entered. However it does not tell me whether the information was committed to by the user (i.e. they pressed the return key). I don't want to use a button if I can get away with it.

Any ideas?

Talwyn </code>

sub ::tfLoad_TapeName_Change{ defined(my $win = $Win32::GUI::Loft::window{winMain}) or return(1); my $inputchar = $win->tfLoad_TapeName->Text(); assert ("tfLoad_TapeName()",$inputchar ne '',"Did not read charact +er!\n"); print_dbg ("tfLoad_TapeName_Change()","I read in:'$inputchar'\n"); if ( index ($inputchar,"\r") > -1){ print_dbg ("tfLoad_TapeName_Change()","Carriage return has bee +n detected ! Yeah!\n"); } print_dbg ("tfLoad_TapeName_Change()","I AM A STUB FIX ME!"); return (1); }
  • Comment on How do I recognize that the user has pressed enter inside a text box? Win32::GUI Question
  • Download Code

Replies are listed 'Best First'.
Re: How do I recognize that the user has pressed enter inside a text box? Win32::GUI Question
by dree (Monsignor) on Jul 02, 2002 at 09:39 UTC
    You need the Class package that you have to associate with the RichEdit widget.
    Then you can bind the KeyPress event.
    This code below works form me with Win32::GUI 0.502 but could have problems with more recent versions.

    I hope dada could give us a more complete answer :)
    use strict; use Win32::GUI; my $win = new Win32::GUI::Window( -name => "Window", -text => "Test", -width => 200, -height => 100, -left => 200, -top => 100, -minsize => [200, 100], ); my $TextClass = new Win32::GUI::Class( -name => "_Editor", -extends => "RichEdit", -widget => "RichEdit", ); my $SearchText = $win->AddRichEdit ( -class => $TextClass, -left => 10, -top => 10, -width => 170, -height => 52, -text=>"", -name=>"searchtext", ); sub searchtext_KeyPress { my($key) = @_; if ($key == 13) { print "Enter!\n"; } return 1; } sub Window_Terminate { exit; return -1; } $win->Show(); Win32::GUI::Dialog();

    UPDATE:

    Above code works with RichEdit. If you want to recognize enter press on a Textfield, recent rumours on the Win32::GUI mailing list, point me to this:
    use strict; use Win32::GUI; my $win = new Win32::GUI::Window( -name => "Window", -text => "Test", -width => 200, -height => 100, -left => 200, -top => 100, -minsize => [200, 100], ); my $SearchText = $win->AddTextfield ( -left => 10, -top => 10, -width => 170, -height => 52, -text=>"", -name=>"searchtext", -multiline=>1, ); sub searchtext_Change{ my $rtest = 0; my $input =''; $input=$SearchText->Text(); $rtest = $input=~/\r\n$/; if($rtest){print "Enter!\n";} } sub Window_Terminate { exit; return -1; } $win->Show(); Win32::GUI::Dialog();
    Thanks to John Rogers to show us the trick (with a little fix from me ;) )
Re: How do I recognize that the user has pressed enter inside a text box? Win32::GUI Question
by BrowserUk (Patriarch) on Jul 02, 2002 at 01:58 UTC

    I haven't used the WIN32::GUI module, but a couple of things to watch for - the user could use the TAB key or the mouse to leave the TEXTFIELD.

    It would be better to use the loose-focus event to validate the TEXTFIELD assuming thats what you want to do and that event is exposed thru Win32::GUI.