22 Votes

Delphi/Lazarus: Enter-Key in Edit

Question by Guest | Last update on 2024-01-03 | Created on 2016-06-10

I would like to execute a specific action automatically, whenever the enter respectively return key is pressed from within a TEdit field.

For example, this can be the automatic start of the search after entering a keyword, the jump into the next input field or the automatic press of the button next to the field.

How can I realize that?

ReplyPositiveNegativeDateVotes
3Best Answer3 Votes

You can just react to the enter key in the OnKeyPress event of the Edit field.

The return key has the key code #13, so the if statement could look something like that:

procedure TForm1.Edit1KeyPress(Sender: TObject; 
  var Key: Char);
begin
  if Key = #13 then begin
    Button1.Click;  // for example: simulate button click
    Edit2.SetFocus; // or: jump to next edit 
    // ... or anything else
    Key := #0;
  end;
end; 

In this example, we are just calling the procedure Button1Click, so that the therein contained code will be executed after pressing the enter key, and we jump to the next edit field. Of course, you can also execute or write any other code or function at this point.

It is important to set the key to #0 afterwards. With this, you prevent the Edit from further processing the input.
Last update on 2024-01-03 | Created on 2016-06-10

ReplyPositive Negative
11 Vote

If a button should be clicked when pressing the enter key, you can also set the property "Default" of the button to "true". With this, the button will be clicked automatically when pressing Enter.

The same applies for the ESC key. In this case, you have to set the property "Cancel" to "true".
2016-06-11 at 20:12

ReplyPositive Negative
00 Votes

This solution is better for smaller programs or dialog windows!

The above answer is more suitable for longer programs.
2023-06-12 at 21:38

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.