Recent

Author Topic: [Solved]TForm.OnKeyDown being triggered twice  (Read 702 times)

rdxdt

  • New member
  • *
  • Posts: 9
[Solved]TForm.OnKeyDown being triggered twice
« on: June 27, 2025, 01:38:37 pm »
I've noticed an weird behavior, i have the main form(called frmMain) and a dialog box(called dlgBuscaProduto).
frmMain has KeyPreview enabled and there is a case statement to handle the VK_F5 key and show dlgBuscaProduto.
dlgBuscaProduto has KeyPreview enabled, and OnKeyDown it handles the VK_ESCAPE to have ModalResult := mrCancel;, the DBGrid has two event handlers, OnDblClick that have ModalResult :=mrOK; and also OnKeyDown checking if the Key = VK_RETURN then ModalResult := mrOK;
When i press F5 the frmMain.OnKeyDown is triggered and opens up dlgBuscaProduto, if i solely navigate with the keyboard(write on the edit, press enter which set focus to the DBGrid, then i navigate the DBGrid with arrow keys and press enter, then the TFrmMain.OnKeyDown with the VK_F5 is triggered again, however when i do with the mouse and do the DblClick on the DBGrid it works as expected, i made a screen recording debugging, it will probably explain it a little better.
Note: i even tried to add an Application.ProcessMessages after calling dlgBuscaProduto and it still behaves oddly.

https://youtu.be/zcZUHl6gKJM
« Last Edit: June 30, 2025, 12:40:15 pm by rdxdt »

gues1

  • Guest
Re: TForm.OnKeyDown being triggered twice
« Reply #1 on: June 27, 2025, 07:01:05 pm »
When I catch the correct key in code, I always set the key var to ZERO so the key is not going on.

rdxdt

  • New member
  • *
  • Posts: 9
Re: [Solved]TForm.OnKeyDown being triggered twice
« Reply #2 on: June 30, 2025, 12:40:54 pm »
That was a great advice, it also worked for another undesired behavior that i've noticed.
Thanks.

 

TinyPortal © 2005-2018