[SOLVED] How to capture ENTER Key in TDBGrid OnKeyPress Event lazarus / 31-LCL ---------------------------------------------------------------------- incendio · 2022-01-25 04:50 Hi guys, Just found out that TDBGrid OnKeyPress Event doesn't capture ENTER Key. I have 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 procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin  ShowMessage(Key);end;  Other keys were captured except ENTER key. Is it possible to capture ENTER key in DBGrid? ---------------------------------------------------------------------- Thaddy · 2022-01-25 05:41 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 TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin   if key = VK_RETURN then       ShowMessage('#',Ord(Key), ' Enter pressed'); end; Might work. ---------------------------------------------------------------------- balazsszekely · 2022-01-25 06:02 Quote from: GetMem on January 25, 2022, 07:00:56 amQuote from: Thaddy on January 25, 2022, 06:41:11 amCode: 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 TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin   if key = VK_RETURN then       ShowMessage(Ord(Key), 'Enter pressed'); end Might work. ; It works on KeyDown and KeyUp, on KeyPress you need a char: 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 TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin     if Key = #13 then    ShowMessage('Enter')end; ---------------------------------------------------------------------- Thaddy · 2022-01-25 07:31 Or Ord(key) = VK_Return. I saw it after I posted. ---------------------------------------------------------------------- Zoran · 2022-01-25 08:59 For non-character keys, don't use OnKeyPress. You can freely use OnKeyDown or OnKeyUp, they work with any key. VK_Return constant is defined in LCLType unit. This is not specific to TDBGrid, this is standard behaviour of lcl controls. Read: https://wiki.freepascal.org/LCL_Key_Handling#What_should_be_sent _via_KeyPress Edit: It actually says that Return does generate KeyPress event, sorry. So, Thaddy's code (with Ord) should work. I would still rather avoid KeyPress for non-character key and use OnKeyDown or OnKeyUp. ---------------------------------------------------------------------- incendio · 2022-01-26 01:50 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 procedure TSearchFrm.SrchGridKeyPress(Sender: TObject; var Key: char);begin  if Ord(key) = VK_RETURN  then  begin     ShowMessage('ENTER pressed');  end  else if Key = #27 then      ShowMessage('ESC pressed');end;  or 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 procedure TSearchFrm.SrchGridKeyPress(Sender: TObject; var Key: char);begin  if key = #13  then  begin     ShowMessage('ENTER pressed');  end  else if Key = #27 then      ShowMessage('ESC pressed');end; Both codes unable to capture ENTER key. ESC key detected OK. ---------------------------------------------------------------------- dseligo · 2022-01-26 04:34 Quote from: incendio on January 26, 2022, 02:50:12 amBoth codes unable to capture ENTER key. Why don't you use OnKeyDown (or OnKeyUp) event as Zoran suggested? This code works for Enter key: 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.DBGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin  ShowMessage(Key.ToString);end; ---------------------------------------------------------------------- incendio · 2022-01-26 07:45 Quote from: dseligo on January 26, 2022, 05:34:10 amQuote from: incendio on January 26, 2022, 02:50:12 amBoth codes unable to capture ENTER key. Why don't you use OnKeyDown (or OnKeyUp) event as Zoran suggested? This code works for Enter key: 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.DBGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin  ShowMessage(Key.ToString);end;What is wrong with Keypress? Is it a bug that Keypress does not work on ENTER key? ---------------------------------------------------------------------- balazsszekely · 2022-01-26 08:06 QuoteWhat is wrong with Keypress?Nothing. QuoteIs it a bug that Keypress does not work on ENTER key? It's by design. Take a look at unit dbgrid.pas(especially the key :=  0 part): 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     //...      VK_RETURN:      begin        doOnKeyDown;        if (Key<>0) and ValidDataset then begin          key:=0;           if (dgEditing in Options) and not EditorMode then            EditorMode:=true          else begin            GetDeltaMoveNext(ssShift in Shift, DeltaCol, DeltaRow, AutoAdvance);            MoveSel(True);          end;      end;      end;    //...   Enter is used for editing, OnKeyDown is still called though. This is why people suggesting the KeyDown/KeyUp events. ---------------------------------------------------------------------- Zoran · 2022-01-26 08:12 Quote from: incendio on January 26, 2022, 08:45:21 amWhat is wrong with Keypress? Is it a bug that Keypress does not work on ENTER key? KeyPress is intended for use with character keys. The wiki page (link in my previous post) says that some non-character keys still do generate KeyPress. I wouldn't rely on that. For non-character keys, there are other events, as said. If enter doesn't generate OnKeyPress after all, I don't think it's a bug, because, unlike wiki page, the official documentation for OnKeyPress just says QuoteThis handler only receives characters, not control or other special key codes. Control keys should be handled by an OnKeyDown handler instead. ---------------------------------------------------------------------- incendio · 2022-01-26 08:57 ESC key is a control key right? It still captured by OnKeyPress. But, like, all you guys told me, I will use OnKeyUp to capture ENTER key. Thanks for all your help.