Forum > LCL

[SOLVED] How to capture ENTER Key in TDBGrid OnKeyPress Event

(1/3) > >>

incendio:
Hi guys,

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

I have this code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin   if key = VK_RETURN then       ShowMessage('#',Ord(Key), ' Enter pressed'); end; Might work.

 

GetMem:

--- Quote from: GetMem on January 25, 2022, 07:00:56 am ---
--- Quote from: Thaddy on January 25, 2022, 06:41:11 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin   if key = VK_RETURN then       ShowMessage(Ord(Key), 'Enter pressed'); end Might work.
;
 

--- End quote ---

--- End quote ---

It works on KeyDown and KeyUp, on KeyPress you need a char:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TInpUsrFrm.DBGrid1KeyPress(Sender: TObject; var Key: char);begin     if Key = #13 then    ShowMessage('Enter')end; 

Thaddy:
Or Ord(key) = VK_Return. I saw it after I posted.

Zoran:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version