Catching a KeyPress in a StringGrid-Cell lazarus / 31-LCL ---------------------------------------------------------------------- Zvoni · 2022-03-10 15:34 Hi Folks, could someone point me in the right direction how to catch a KeyPress in a specific cell of a StringGrid? And before you ask: No, i cannot use KeyDown/KeyUp I've seen the doc's regarding "TheEditor", and i tried assigning a custom "Procedure OnCellKeyPress(Sender:TObject;var aKey:Char);" to it in the SelectEditor-Event (which gets fired). The Editor.OnKeyPress:=@OnCellKeyPress; but on subsequent keypresses it doesn't fire (i have a Writeln inside the event). Do i really have to use the Grids own KeyPress for that (Which i currently do, but it looks "ungainly" since i have to save the CurCol and CurRow in private Fields)? ---------------------------------------------------------------------- Sieben · 2022-03-10 17:38 Assigning a KeyPress handler in OnSelectEditor works for me - did you include goEditing in Options? I always stumble over that one... ---------------------------------------------------------------------- Zvoni · 2022-03-10 18:16 Quote from: Sieben on March 10, 2022, 06:38:58 pmAssigning a KeyPress handler in OnSelectEditor works for me - did you include goEditing in Options? I always stumble over that one... Ahhhh… Ok, will have to check on monday, since i‘m out of office tomorrow ---------------------------------------------------------------------- Zvoni · 2022-03-14 08:47 Right, doesn't work. It's not firing. What am i missing? OnCellKeyPress is a Procedure of my Form My StringGrid has goEditing=True 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 TfrmConfig.grdConfigSelectEditor(Sender: TObject; aCol,  aRow: Integer; var Editor: TWinControl);begin  Editor.OnKeyPress  := @OnCellKeyPress;  If DebugMode Then    DebugWindow.AddToLog('FConfigODBC',Succ({$I %LINENUM%}),'Editor-Type: '+Editor.ClassName);  //Returns TStringCellEditorend;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 TfrmConfig.OnCellKeyPress(Sender: TObject; var Key: char);begin  ShowMessage('Cell KeyPress');   end; ---------------------------------------------------------------------- Sieben · 2022-03-14 13:30 But the pressed keys appear in the cell...? AutoEdit True as well? ---------------------------------------------------------------------- Zvoni · 2022-03-14 13:56 Yes, the Characters appear in the cell, since i currently catch them via the KeyPress of the Grid itself My Stringgrid has following Options set to True: goAlwaysShowEditor goAutoAddRows goCellEllipsis goEditing all 4 horiz/vert Lines some RowSelect-Optons (highlight, full row etc.) EDIT: Yes, AutoEdit = True ---------------------------------------------------------------------- Sieben · 2022-03-14 14:37 Made a small test project. A KeyPress handler of the grid should not interfere. Run it with and without attaching EditorKeyPress / GridKeyPress. ---------------------------------------------------------------------- Zvoni · 2022-03-14 15:36 Right, after some testing, i think i found the culprit: goAlwaysShowEditor=True supresses the KeyPress-Event???? Doesn't make sense, since i tested with/without that option in a vanilla-project having only 1 StringGrid, and a SelectEditor-Event where i set the Handler, and there it fires for both EDIT: You gotta be kidding me: My First Column is read only. If ReadOnly is True it supresses KeyPress-Event of the Editor for 4th column, irrespective of any other options Bug? See attachment. Toggle the ReadOnly for the First column and write into the second ---------------------------------------------------------------------- Sieben · 2022-03-14 15:45 Quote from: Zvoni on March 14, 2022, 04:36:30 pmRight, after some testing, i think i found the culprit: goAlwaysShowEditor=True supresses the KeyPress-Event???? I'd rather think that maybe OnSelectEditor does not fire in this case...? Anyway, with the test project here on Linux/Gtk2 it does, and it works w/wo goAlwaysShowEditor. Edit: Or maybe you attached OnSelecetEditor in code as well? With goAlwaysShowEditor it fires very early, on loading the grid. ---------------------------------------------------------------------- Zvoni · 2022-03-14 15:50 SelectEditor definitely does fire. See my edit above ---------------------------------------------------------------------- Sieben · 2022-03-14 16:32 Yes, I can confirm your findings in Reply #7 / EDIT. Editor.OnKeyPress is not fired in the second column if the first is readonly AND goAlwaysShowEditor=True. If goAlwaysShowEditor=False it still misses the first KeyPress, but fires on the second ff. ---------------------------------------------------------------------- Zvoni · 2022-03-14 17:37 Just for fun: set the first column to cbNone (instead of cbAuto), Readonly=false ---------------------------------------------------------------------- Sieben · 2022-03-14 18:21 Apart from OnSelectEditor firing with Editor being nil - easy to catch - I can see no difference to the ReadOnly scenario. ---------------------------------------------------------------------- Zvoni · 2022-03-14 18:32 Not completely if first column is cbNone, but you click directly into the second column, which is cbAuto, you still get the AV I was under the impression that the editor is columnwise, so why is the editor nil? ---------------------------------------------------------------------- Sieben · 2022-03-14 19:31 No, the editor(s) are gridwise, all created in the grid's constructor and just picked up according to column settings. So if you don't want OnKeyPress with other columns you would have to reset it. But Editor shouldn't be nil on the second column. Probably same cause as with missing the first keystroke - some hick-up in the event chain or sth like that. ---------------------------------------------------------------------- Zvoni · 2022-03-14 21:01 Sooo…….. Bug or feature? ---------------------------------------------------------------------- Zvoni · 2022-03-15 07:35 Quote from: Sieben on March 14, 2022, 05:32:49 pm If goAlwaysShowEditor=False it still misses the first KeyPress, but fires on the second ff. Cannot confirm. Fires correctly ---------------------------------------------------------------------- Sieben · 2022-03-15 12:09 Had a closer look at the sources today. Column ReadOnly issue left aside for the moment, it indeed fires correctly if the editor is already shown, either by setting goAlwaysShowEditor or by clicking the selected cell twice before entering the first key. Otherwise it will only fire at the second key. Reason is that in this case the editor is invoked by the grid's KeyPress handler by calling a method called EditorShowChar, which does not trigger it's own KeyPress and thus not your custom handler. Next thing is that the editor's OnKeyPress is already hooked to a handler inside the grid called EditorKeyPress(). In this method there's a comment reading 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 // grid must get all keypresses, even if they are from the editor followed by calling the grid's handler. So it seems to be a bad idea to just replace this method by your custom handler. You can, however, call this method from within your custom handler: 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 StringGrid1.EditorKeyPress(Sender, Key); If you don't the grid will possibly not work as intended with key strokes like Esc and with a TDBGrid it won't check anymore if the key is accepted by the underlying field. Given all this it might indeed not be a good idea to interfere with this rather complex arrangement but to use the grid's exposed OnKeyPress instead. ---------------------------------------------------------------------- Zvoni · 2022-03-15 13:48 hmmm..... you're right. OK, if implementing a custom OnCellKeyPress for the Editor, never forget to call the Grids EditorKeyPress. Got it. Albeit, the "First-Column-ReadOnly"-Issue forces me to do everything in the Grids OnKeyPress currently, since for the Editor itself it doesn't fire