Cannot cancel Enter key press on TEdit lazarus / 31-LCL ---------------------------------------------------------------------- leduyquang753 · 2019-05-04 14:02 I am making an expression parser with an input box (TEdit) that when a mistake is present in the expression, the caret moves to where it is. So I hook OnKeyPress to the TEdit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMainWindow.ExpressionKeyPress(Sender: TObject; var Key: char);begin  if key = #13 then begin    self.calculateIt; // This moves the caret when an error happens.    key := #0; // Supposed to cancel the key press.  end;end; When an erroneous expression is entered, the caret is seen to be moved to the mistake's location, however shortly after, the whole text in the box is selected, which indicates the Enter key was not canceled, despite having the code to do so. I tried these workarounds: Hooking an additional OnKeyDown event to cancel the key.Setting TForm.KeyPreview to true.None of them worked. Am I doing something wrong or is there a bug in LCL/Windows OS? ---------------------------------------------------------------------- Thaddy · 2019-05-04 14:34 Quote from: leduyquang753 on May 04, 2019, 04:02:05 pmI tried these workarounds: Hooking an additional OnKeyDown event to cancel the key.Setting TForm.KeyPreview to true.None of them worked. Am I doing something wrong or is there a bug in LCL/Windows OS? Try using OnKeyUp, because only then you can evaluate correctly. (This is a very common error) ---------------------------------------------------------------------- Bart · 2019-05-04 16:19 Use OnEditingDone for that (it is fired when user presses Enter or when user leaves the control). Bart ---------------------------------------------------------------------- Thaddy · 2019-05-04 17:32 Quote from: Bart on May 04, 2019, 06:19:42 pmUse OnEditingDone for that (it is fired when user presses Enter or when user leaves the control). Bart No. ---------------------------------------------------------------------- jamie · 2019-05-04 21:11 I assume you set "Autofocus" := False ? ---------------------------------------------------------------------- leduyquang753 · 2019-05-05 00:47 Quote from: Thaddy on May 04, 2019, 04:34:58 pmTry using OnKeyUp, because only then you can evaluate correctly. (This is a very common error) Oh, the key release event is also triggered. You made my day, thank you.