Recent

Author Topic: How to intercept F2 Keydown in a StringGrid  (Read 4126 times)

Eusebi

  • Newbie
  • Posts: 4
How to intercept F2 Keydown in a StringGrid
« on: May 02, 2016, 11:28:25 pm »
Using the event OnKeyDown of a StringGrid a program can
react on keystrokes like F-keys, Tab, Esc etc.
Function key F2, however, escapes this mechanism.
I can neither intercept it by OnKeyDown of a StringGrid,
nor by OnKeyDown of the main Form. (With KeyPreview being true)
What has to be done to have F2 behave like the other
function keys?

wp

  • Hero Member
  • *****
  • Posts: 11911
Re: How to intercept F2 Keydown in a StringGrid
« Reply #1 on: May 02, 2016, 11:47:08 pm »
It is working for me (Win 7: Laz trunk/fpc 3.0, Laz 1.44/fpc 2.6.4; Linux Mint (qt, gtk2): Laz trunk / fpc2.6.4). Just a form with a StringGrid, no properties changed, just an event handler for OnKeyDown:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     StringGrid1: TStringGrid;
  16.     procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
  17.       Shift: TShiftState);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. uses
  32.   LCLType;
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  37.   Shift: TShiftState);
  38. begin
  39.   if Key = VK_F2 then
  40.     ShowMessage('F2');
  41. end;
  42.  
  43. end.
  44.  

Eusebi

  • Newbie
  • Posts: 4
Re: How to intercept F2 Keydown in a StringGrid
« Reply #2 on: May 03, 2016, 04:55:31 pm »
Thanks for your quick reply!
When running your example, I can't detect any function key or
other key: Setting a breakpoint at "if Key = VK_F2 then" gives no break.
The attached program logs F1 to F5 except F2, which is activating an
editor in the active cell.
Win10-64, Lazarus 1.6/FPC3.0.0   

wp

  • Hero Member
  • *****
  • Posts: 11911
Re: How to intercept F2 Keydown in a StringGrid
« Reply #3 on: May 03, 2016, 05:22:57 pm »
No, your demo activates the cell editor AND displays "F2!!!" in a cell. Could you post the full project? Maybe something is wrong somewhere else. Pack the .lpi, .lpr, .pas and .lfm files into a single zip which you can upload. As for your system: You are on Win10-64 bit; is Lazarus 64 bit as well, or is it 32 bit? (Your program did work with 64-bit Laz on Win7-64 bit, though.)

Eusebi

  • Newbie
  • Posts: 4
Re: How to intercept F2 Keydown in a StringGrid
« Reply #4 on: May 04, 2016, 06:58:11 pm »
Thanks for looking at the enclosed project.
My Lazarus installation filename is: lazarus-1.6.0-fpc-3.0.0-win64.exe
Version #:1.6, 2016-02-14, FPC Version: 3.0.0, SVN Revision 51630,
x86_64-win64-win32/win64.
I observe the same behaviour on another W10-64 laptop PC, same OS,
same Lazarus.
I wonder what setting I have missed... 

wp

  • Hero Member
  • *****
  • Posts: 11911
Re: How to intercept F2 Keydown in a StringGrid
« Reply #5 on: May 04, 2016, 07:18:44 pm »
I see the same now. But when you turn off the option goAlwaysShowEditor the OnKeyDown does react on F2. Do you need this option? Normally it is enough to have goEditing on in order to be able to edit cells.

Eusebi

  • Newbie
  • Posts: 4
Re: How to intercept F2 Keydown in a StringGrid
« Reply #6 on: May 04, 2016, 11:39:57 pm »
 With the option goAlwaysShowEditor OFF, (which I actually don't
 need) F2 is now indeed being intercepted by StringGrid's OnKeyDown.
 Many thanks for your help!
 

 

TinyPortal © 2005-2018