Board index » delphi » Hot to catch the [Enter] key??

Hot to catch the [Enter] key??

Hi!

How do I get hold of the [Enter] key in an Keypressed event in an Editbox??
Insted the form OK button executes..

procedure TRotateForm.AngleEditKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin         { dont work }
    {do something}
  end;
end;

Regards,
Christer Strandh

***********************************************************
CosmoNorr http://www.jamtnet.se/cosmonorr
Ph: +46-63-181612,43785 Fax: +46-63-105181
***********************************************************

 

Re:Hot to catch the [Enter] key??


Hello!
I don't see why it should not work. The following is copied directly from a
project and is working fine:

procedure Tny.DBEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  If Key = #13 then
  begin
    self.Perform(WM_NEXTDLGCTL,0,0);
  end;
end;
--
Regards
Kai Inge

Quantum <quan...@jamtnet.se> wrote in article <35b2288...@195.66.60.18>...

Quote
> Hi!

> How do I get hold of the [Enter] key in an Keypressed event in an
Editbox??
> Insted the form OK button executes..

> procedure TRotateForm.AngleEditKeyPress(Sender: TObject; var Key: Char);
> begin
>   if key = #13 then begin         { dont work }
>     {do something}
>   end;
> end;

> Regards,
> Christer Strandh

> ***********************************************************
> CosmoNorr http://www.jamtnet.se/cosmonorr
> Ph: +46-63-181612,43785 Fax: +46-63-105181
> ***********************************************************

Re:Hot to catch the [Enter] key??


It does work. You're just forgetting one little detail...
What happens is that after the keypress event has finished your program
continues to execute the OK button, just like it would as if you did not
have a KeyPress event. That you made such an event changes nothing.
Unless you do this in the KeyPress event:
  if Key = #13 then
      Key := #0;
This turns the Enter key into nothing and your OK button will not execute.

Quantum <quan...@jamtnet.se> wrote in article <35b2288...@195.66.60.18>...

Quote
> Hi!

> How do I get hold of the [Enter] key in an Keypressed event in an
Editbox??
> Insted the form OK button executes..

> procedure TRotateForm.AngleEditKeyPress(Sender: TObject; var Key: Char);
> begin
>   if key = #13 then begin         { dont work }
>     {do something}
>   end;
> end;

> Regards,
> Christer Strandh

> ***********************************************************
> CosmoNorr http://www.jamtnet.se/cosmonorr
> Ph: +46-63-181612,43785 Fax: +46-63-105181
> ***********************************************************

Re:Hot to catch the [Enter] key??


In article <35b2288...@195.66.60.18>, Quantum <quan...@jamtnet.se>
writes

Quote
>Hi!

>How do I get hold of the [Enter] key in an Keypressed event in an Editbox??
>Insted the form OK button executes..

>procedure TRotateForm.AngleEditKeyPress(Sender: TObject; var Key: Char);
>begin
>  if key = #13 then begin         { dont work }
>    {do something}
>  end;
>end;

You have to set the KeyPreview property of the form to True. Look in
the help for the reason.
--
Jeremy Collins
Kansai Business Systems

(return address not altered 'coz I get spammed *whatever* I do!)

Re:Hot to catch the [Enter] key??


Christer,

The problem is that the Ok button's Default property is set to
True; set it to False instead.

Ken
--
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc/

Quote
Quantum wrote:

> Hi!

> How do I get hold of the [Enter] key in an Keypressed event in an Editbox??
> Insted the form OK button executes..

> procedure TRotateForm.AngleEditKeyPress(Sender: TObject; var Key: Char);
> begin
>   if key = #13 then begin         { dont work }
>     {do something}
>   end;
> end;

> Regards,
> Christer Strandh

> ***********************************************************
> CosmoNorr http://www.jamtnet.se/cosmonorr
> Ph: +46-63-181612,43785 Fax: +46-63-105181
> ***********************************************************

Other Threads