00 Votes

Delphi/Lazarus: Enter-Key in Edit-Field

Question by Guest | 2015-03-17 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
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.
2015-03-18 at 22:32

ReplyPositive Negative
11 Vote

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

Positive Negative
-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)".
2016-04-28 at 02:04

Positive Negative
Reply
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".
2015-03-20 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...
2016-06-06 at 18:49

Positive Negative
Reply
Reply

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

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 article on askingbox.com. That’s how it’s done.