Recent

Author Topic: Catching a KeyPress in a StringGrid-Cell  (Read 3595 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Catching a KeyPress in a StringGrid-Cell
« on: March 10, 2022, 04:34:25 pm »
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)?
« Last Edit: March 10, 2022, 04:57:32 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #1 on: March 10, 2022, 06:38:58 pm »
Assigning a KeyPress handler in OnSelectEditor works for me - did you include goEditing in Options? I always stumble over that one...
« Last Edit: March 10, 2022, 06:41:46 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #2 on: March 10, 2022, 07:16:31 pm »
Assigning 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #3 on: March 14, 2022, 09:47:38 am »
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][+][-]
  1. procedure TfrmConfig.grdConfigSelectEditor(Sender: TObject; aCol,
  2.   aRow: Integer; var Editor: TWinControl);
  3. begin
  4.   Editor.OnKeyPress  := @OnCellKeyPress;
  5.   If DebugMode Then
  6.     DebugWindow.AddToLog('FConfigODBC',Succ({$I %LINENUM%}),'Editor-Type: '+Editor.ClassName);  //Returns TStringCellEditor
  7. end;
Code: Pascal  [Select][+][-]
  1. procedure TfrmConfig.OnCellKeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   ShowMessage('Cell KeyPress');  
  4. end;      
  5.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #4 on: March 14, 2022, 02:30:13 pm »
But the pressed keys appear in the cell...? AutoEdit True as well?
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #5 on: March 14, 2022, 02:56:10 pm »
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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #6 on: March 14, 2022, 03:37:42 pm »
Made a small test project. A KeyPress handler of the grid should not interfere. Run it with and without attaching EditorKeyPress / GridKeyPress.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #7 on: March 14, 2022, 04:36:30 pm »
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
« Last Edit: March 14, 2022, 04:50:12 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #8 on: March 14, 2022, 04:45:33 pm »
Right, 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.
« Last Edit: March 14, 2022, 04:49:17 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #9 on: March 14, 2022, 04:50:44 pm »
SelectEditor definitely does fire.
See my edit above
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #10 on: March 14, 2022, 05:32:49 pm »
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.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #11 on: March 14, 2022, 06:37:09 pm »
Just for fun: set the first column to cbNone (instead of cbAuto), Readonly=false
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #12 on: March 14, 2022, 07:21:24 pm »
Apart from OnSelectEditor firing with Editor being nil - easy to catch - I can see no difference to the ReadOnly scenario.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #13 on: March 14, 2022, 07:32:06 pm »
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?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Sieben

  • Sr. Member
  • ****
  • Posts: 374
Re: Catching a KeyPress in a StringGrid-Cell
« Reply #14 on: March 14, 2022, 08:31:02 pm »
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.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018