[solved] need help for TEDIT component lazarus / 67-Windows ---------------------------------------------------------------------- compispezi@web.de · 2022-08-06 18:25 Hello, I am learning about Lazarus. I want to catch key up and key down and enter in TEDIT fields to jump to other TEDIT fields. In the example I have written this for TEDIT2. But, as soon as I go into TEDIT2 and press a key, the programme stops and can only be terminated with CTRL+F2. Can you please give me support? Here is the example: unit unit1; {$mode ObjFPC}{$H+} interface uses   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm2 }   TForm2 = class(TForm)     Button1: TButton;     Edit1: TEdit;     Edit2: TEdit;     Edit3: TEdit;     procedure Button1Click(Sender: TObject);     procedure Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);   private   public   end; var   Form2: TForm2; implementation {$R *.lfm} { TForm2 } procedure TForm2.Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState   ); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   begin     case key of       38    : Edit1.SetFocus; //hoch       40,13 : Edit3.SetFocus  //runter, Enter       end;     end; procedure TForm2.Button1Click(Sender: TObject); begin   Form2.Close; end; end. ---------------------------------------------------------------------- Gald · 2022-08-06 19:47 I've tried some things and found out strange behavior. You have to declare LCLType to the uses and then Uses session this code: 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 if key = VK_DOWN then  SelectNext(Self as TWinControl, True, False); A common way to do it is setting the property KeyPreview of your TForm as True. But... Arrow keys are used by the system to move the keyboard cursor around the TEdit. That's why I've tried on the TEdit's key events, but for my surprise, SelectNext it's going back to the first control (TabStop = 0) every time I press the arrow down. So, now I'm confusing: 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 //This is setting focus to the first control (TabStop = 0)SelectNext(Self as TWinControl, False, False); //This is setting focus on the last controlSelectNext(Self as TWinControl, False, False); Sounds like a bug to me! ---------------------------------------------------------------------- HeavyUser · 2022-08-06 19:56 Well, which control you think self is?  Try using the parameter sender instead. ---------------------------------------------------------------------- Gald · 2022-08-06 20:21 Quote from: HeavyUser on August 06, 2022, 09:56:54 pmWell, which control you think self is?  Try using the parameter sender instead. Same results. ---------------------------------------------------------------------- HeavyUser · 2022-08-06 20:24 Quote from: Gald on August 06, 2022, 10:21:32 pmQuote from: HeavyUser on August 06, 2022, 09:56:54 pmWell, which control you think self is?  Try using the parameter sender instead. Same results. attach a small sample that demonstrates the bug please. ---------------------------------------------------------------------- Gald · 2022-08-06 20:39 Use Tab and ESC for better results. Set focus to some control on the center of the screen to see this behavior. ---------------------------------------------------------------------- HeavyUser · 2022-08-06 21:10 Quote from: Gald on August 06, 2022, 10:39:45 pmUse Tab and ESC for better results. Set focus to some control on the center of the screen to see this behavior. tab is the key that changes to the next control by default, which ends up calling SelectNext twice, don't use known keys, use unused keys instead, like up arrow and down arrow. I'll have a look on the sample in a half an hour or so. ---------------------------------------------------------------------- HeavyUser · 2022-08-06 21:37 Attached is my take on the problem and here is the corrected formkeydown event for your code. 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 TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin  if Key = VK_DOWN then begin // VK_Return    SelectNext(ActiveControl, True, False);    Key := 0;  end;  if Key = VK_up then  begin // VK_Escape    SelectNext(ActiveControl, False, False);    Key := 0;  end;end; ---------------------------------------------------------------------- Gald · 2022-08-06 22:07 Quote from: HeavyUser on August 06, 2022, 11:10:24 pmtab is the key that changes to the next control by default Three days without my sleep remedy. I was trying to say Enter but typed TAB, sorry. Quote from: HeavyUser on August 06, 2022, 11:37:26 pmAttached is my take on the problem and here is the corrected formkeydown event for your code. Nice! Thank you so much! ---------------------------------------------------------------------- compispezi@web.de · 2022-08-07 08:43 Tab and ESC is not the usual way while input datas. I only would use keydown, keyup and Enter. ---------------------------------------------------------------------- Thaddy · 2022-08-07 08:56 Quote from: compispezi@web.de on August 07, 2022, 10:43:19 amTab and ESC is not the usual way while input datas. I only would use keydown, keyup and Enter. Note KeyDown is repeating and KeyUp and Enter/Return are single shot. KeyDown should only be used if you need continuous control (navigate, game play) and expensive. The other two are usually much better options. ---------------------------------------------------------------------- compispezi@web.de · 2022-08-08 09:10 Thank you for the very good tips. The resolution is: procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState   ); begin       case key of       38      : Edit1.SetFocus; //hoch       40,13 : Edit3.SetFocus  //runter, Enter       end; end;            So the question is solved.