Recent

Author Topic: [SOLVED] How to capture ENTER Key in TDBGrid OnKeyPress Event  (Read 3525 times)

incendio

  • Full Member
  • ***
  • Posts: 171
[SOLVED] How to capture ENTER Key in TDBGrid OnKeyPress Event
« on: January 25, 2022, 05:50:43 am »
Hi guys,

Just found out that TDBGrid OnKeyPress Event doesn't capture ENTER Key.

I have this code
Code: Pascal  [Select][+][-]
  1. procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   ShowMessage(Key);
  4. end;
  5.  

Other keys were captured except ENTER key.

Is it possible to capture ENTER key in DBGrid?
« Last Edit: January 27, 2022, 02:30:19 am by incendio »

Thaddy

  • Hero Member
  • *****
  • Posts: 11623
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #1 on: January 25, 2022, 06:41:11 am »
Code: Pascal  [Select][+][-]
  1. procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.    if key = VK_RETURN then
  4.        ShowMessage('#',Ord(Key), ' Enter pressed');
  5. end;
Might work.

 
« Last Edit: January 25, 2022, 06:59:40 am by Thaddy »
Black themes should be banned.

GetMem

  • Hero Member
  • *****
  • Posts: 3750
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #2 on: January 25, 2022, 07:02:58 am »
Code: Pascal  [Select][+][-]
  1. procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.    if key = VK_RETURN then
  4.        ShowMessage(Ord(Key), 'Enter pressed');
  5. end
Might work.
;
 

It works on KeyDown and KeyUp, on KeyPress you need a char:
Code: Pascal  [Select][+][-]
  1. procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);
  2. begin  
  3.   if Key = #13 then
  4.     ShowMessage('Enter')
  5. end;
  6.  

Thaddy

  • Hero Member
  • *****
  • Posts: 11623
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #3 on: January 25, 2022, 08:31:56 am »
Or Ord(key) = VK_Return. I saw it after I posted.
Black themes should be banned.

Zoran

  • Hero Member
  • *****
  • Posts: 1730
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #4 on: January 25, 2022, 09:59:53 am »
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.
« Last Edit: January 25, 2022, 10:09:39 am by Zoran »

incendio

  • Full Member
  • ***
  • Posts: 171
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #5 on: January 26, 2022, 02:50:12 am »
This code
Code: Pascal  [Select][+][-]
  1. procedure TSearchFrm.SrchGridKeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   if Ord(key) = VK_RETURN  then
  4.   begin
  5.      ShowMessage('ENTER pressed');
  6.   end
  7.   else if Key = #27 then
  8.      ShowMessage('ESC pressed');
  9. end;
  10.  

or this code

Code: Pascal  [Select][+][-]
  1. procedure TSearchFrm.SrchGridKeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   if key = #13  then
  4.   begin
  5.      ShowMessage('ENTER pressed');
  6.   end
  7.   else if Key = #27 then
  8.      ShowMessage('ESC pressed');
  9. end;
  10.  
Both codes unable to capture ENTER key.
ESC key detected OK.

dseligo

  • Hero Member
  • *****
  • Posts: 613
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #6 on: January 26, 2022, 05:34:10 am »
Both 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][+][-]
  1. procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   ShowMessage(Key.ToString);
  5. end;

incendio

  • Full Member
  • ***
  • Posts: 171
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #7 on: January 26, 2022, 08:45:21 am »
Both 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][+][-]
  1. procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   ShowMessage(Key.ToString);
  5. end;
What is wrong with Keypress?
Is it a bug that Keypress does not work on ENTER key?

GetMem

  • Hero Member
  • *****
  • Posts: 3750
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #8 on: January 26, 2022, 09:06:19 am »
Quote
What is wrong with Keypress?
Nothing.

Quote
Is 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][+][-]
  1.     //...    
  2.     VK_RETURN:
  3.       begin
  4.         doOnKeyDown;
  5.         if (Key<>0) and ValidDataset then begin
  6.           key:=0;
  7.           if (dgEditing in Options) and not EditorMode then
  8.             EditorMode:=true
  9.           else begin
  10.             GetDeltaMoveNext(ssShift in Shift, DeltaCol, DeltaRow, AutoAdvance);
  11.             MoveSel(True);
  12.           end;
  13.         end;
  14.       end;
  15.     //...    

Enter is used for editing, OnKeyDown is still called though. This is why people suggesting the KeyDown/KeyUp events.

Zoran

  • Hero Member
  • *****
  • Posts: 1730
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #9 on: January 26, 2022, 09:12:40 am »
What 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
Quote
This handler only receives characters, not control or other special key codes. Control keys should be handled by an OnKeyDown handler instead.


incendio

  • Full Member
  • ***
  • Posts: 171
Re: How to capture ENTER Key in TDBGrid OnKeyPress Event
« Reply #10 on: January 26, 2022, 09:57:56 am »
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.

 

TinyPortal © 2005-2018