11 Vote

Delphi/Lazarus: Enter-Key in Edit

Question by Guest | 2016-06-10 at 09:26

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 the keyword or the jump into the next input field or the automatic button press next to the field.

How can I realize that?

1Best Answer1 Vote

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);
  if Key = #13 then begin
    Button1.Click; // or something else
    Key := #0;

In this example, we are just calling the procedure Button1Click, so that the therein contained code will be executed after pressing the enter key. 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.
2016-06-10 at 21:12

ReplyPositive Negative
00 Votes

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

Related Topics

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.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.