0 0 Votes

Delphi/Lazarus: Enter-Key in Edit-Field

Question by Guest | 17/03/2015 at 15:31

I would like to automatically execute a special action whenever the user is pressing the enter or return key within a specific TEdit field.

For example, after entering a search term into the edit, I would like to start the search procedure automatically or I would like to jump to the next input field or I would like to automatically press the button next to the field doing something else.

How can I implement such functionality?

ReplyPositiveNegativeDateVotes

Delphian

Show Profile | Message
Avatar
13 Votes

You can just respond to the return key in the OnKeyPress event of the edit field. Enter has the key code #13, so the condition could look something like that:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then begin
    Button1.Click; // or something else
    Key := #0;
  end;
end; 

Here, we are just calling the procedure Button1Click, so that the code from this procedure will be executed when pressing the enter key within the edit. Of course, you can also use any other code at this point.

It is important to subsequently set the key to #0 in order to ensure that there will be no further processing in the edit.
18/03/2015 at 22:32

ReplyPositive Negative
00 Votes

Line 4 must read like "Button1.Click" to call the click procedure. Despite that, your code ist correct and working properly.
27/04/2016 at 09:54

Positive Negative

Delphian

Show Profile | Message
Avatar
-11 Vote

Thank you, I have corrected this.

You can either write "Button1.Click" as you said or you can directly call the assigned procedure that is "Button1Click" by default with calling "Button1Click(Sender)".
28/04/2016 at 02:04

Positive Negative

Delphian

Show Profile | Message
Avatar
11 Vote

If you want to press a button when the enter key is hit, you can also set the property "Default" of the button to "true". With this, the button will be clicked automatically when Enter is pressed.

The same applies for the ESC key. In this case, you have to set the property "Cancel" to "true".
20/03/2015 at 23:06

ReplyPositive Negative
00 Votes

If I'm not mistaken, the OP was talking about an edit field, not a button. Edit fields do not have a "Default" property field, AFAICS...
06/06/2016 at 18:49

Positive Negative
Reply

Related Topics

Delphi: Password Edit
Tip | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.