Recent

Author Topic: Is there a TPanel substitute that responds to key events?  (Read 563 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
Is there a TPanel substitute that responds to key events?
« on: October 03, 2024, 04:16:39 pm »
Is there a TPanel substitute that responds to key events, or do such container components have to avoid reacting to keystrokes as a matter of design?

I assume responding to key events might stop the controls contained from receiving them.

If not can a similar panel be subclassed from TPanel's ancestors which can be designed intercept a specified set of keyboard events design time or runtime?

Lazarus 3.0/FPC 3.2.2

mas steindorff

  • Hero Member
  • *****
  • Posts: 550
Re: Is there a TPanel substitute that responds to key events?
« Reply #1 on: October 03, 2024, 09:59:59 pm »
there is not much a panel can do with a key. it can't display it unless you are changing it's caption.  therefore the keypress are pass on to it's parent. Tabs and enter key operations that move from component to component and global key operation are handled at the form/frame level where you can modify or add stuff and/or menus hooks.
what is your goal? 
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
Re: Is there a TPanel substitute that responds to key events?
« Reply #2 on: October 04, 2024, 12:48:10 am »
there is not much a panel can do with a key. it can't display it unless you are changing it's caption.  therefore the keypress are pass on to it's parent. Tabs and enter key operations that move from component to component and global key operation are handled at the form/frame level where you can modify or add stuff and/or menus hooks.
what is your goal?

This post gives the general idea

https://forum.lazarus.freepascal.org/index.php/topic,68642.0.html

I want to design a fully keystroke driven window navigation and control environment, with an intent to make it all tiled eventually, and I need container components like panels which are designed to respond to certain key combinations automatically.

The container panels are supposed to follow standardized resizing (width and height), repositioning, hiding and restoring keystrokes. To implement the key stroke event handling at development time in code will be too onerous. It should all be built-in with properties to disable or enable some of these events when required.

I find using the mouse when it comes to navigating and resizing containers in programs rather awkward and inefficient and unpredictable.


Speaking as an Emacs and EXWM user who has seen the light.

Another thing I'd like to do for instance would be to use the keyboard to hide, widen and move around the columns of a grid. Being able to do that with a keyboard would be a boon.
Lazarus 3.0/FPC 3.2.2

mas steindorff

  • Hero Member
  • *****
  • Posts: 550
Re: Is there a TPanel substitute that responds to key events?
« Reply #3 on: October 04, 2024, 01:06:36 am »
that sounds great!  I too prefer the keyboard over the mouse.  the TStringgrid and other grid objects do have OnKey... properties you may be able to use to this end.

IIRC there are some grid examples included with the standard install under lazarus/examples/...
I seem to remember 10+ years ago it had a keyboard Colum resize example but I could not tell you it's name. it also has number and text cell editing examples. resizing via the keyboard strike me as a good thing as excel forces resizing with a mouse drag at the time.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Is there a TPanel substitute that responds to key events?
« Reply #4 on: October 04, 2024, 03:20:00 pm »
Making a StringGrid column larger or smaller using keys is not that difficult. See below. Click in a cell and then ALT+E makes the column larger. ALT + C makes the column smaller.

Code: Pascal  [Select][+][-]
  1. unit FormMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids;
  9.  
  10. type
  11.  
  12.   { TFrmMain }
  13.  
  14.   TFrmMain = class(TForm)
  15.     sgFrmMain: TStringGrid;
  16.     procedure sgFrmMainKeyDown(Sender: TObject; var Key: Word;
  17.       Shift: TShiftState);
  18.   private
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   FrmMain: TFrmMain;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TFrmMain }
  31.  
  32. procedure TFrmMain.sgFrmMainKeyDown(Sender: TObject; var Key: Word;
  33.   Shift: TShiftState);
  34. var
  35.   aCol: Integer;
  36. begin
  37.   aCol:= TStringGrid(Sender).Col;
  38.  
  39.   if Shift = [ssAlt] then begin
  40.     case Key of
  41.       $45: begin // $45 = VK_E --> (E)xpand cell
  42.         sgFrmMain.ColWidths[aCol] := sgFrmMain.ColWidths[aCol] + 5;
  43.       end;
  44.       $43: begin  // $43 - VK_C --> (C)ollapse cell
  45.         if sgFrmMain.ColWidths[aCol] > 5 then
  46.           sgFrmMain.ColWidths[aCol] := sgFrmMain.ColWidths[aCol] - 5;
  47.       end;
  48.     end;
  49.   end;
  50. end;
  51.  
  52. end.
  53.  

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
Re: Is there a TPanel substitute that responds to key events?
« Reply #5 on: October 04, 2024, 11:24:57 pm »
Making a StringGrid column larger or smaller using keys is not that difficult. See below. Click in a cell and then ALT+E makes the column larger. ALT + C makes the column smaller.
8<snip>8

I'd prefer to build that kind of functionality into the component itself or else I would have to set it for every instance of the component.
Lazarus 3.0/FPC 3.2.2

Sieben

  • Sr. Member
  • ****
  • Posts: 365
Re: Is there a TPanel substitute that responds to key events?
« Reply #6 on: October 04, 2024, 11:47:42 pm »
TPanel is a descendant of TWinControl and should have all it's keyboard events available in a descendant of your own. It's just that the standard TPanel doesn't expose them.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018