Hey All

I've recently picked up a VCL delphi project and am quite new to the language. The software so far consists of a main form, with several GroupBoxes and panels covering the entire form. I'm trying to implement some user short cuts, for example when "Ctrl+1" is pressed I want all the ComboBoxs to be set to a certain position. Correct me if I'm wrong but I have added an event that triggers when a button is pressed i.e KeyDown, KeyPress, etc.. However the event does not seem to be triggering when a keyboard button is pressed. Does anyone have an idea why this could be? My only guess is that the GroupBoxes are intefering. Any thoughts / hints would be greatly appreciated!

Cheers

Cameron

Dani AI

Generated

Nice catch by : if the form’s KeyPreview is False, the focused control gets key events first, so your form-level handlers never run. Also note that KeyPress won’t fire for Ctrl+1 because it is character-based; use OnKeyDown (or actions) for modifier combos. And no, your GroupBoxes/Panels are not the culprit here; they are not focusable by default, so they do not consume keys.

If you plan to add more shortcuts, consider using actions instead of hand-rolling FormKeyDown logic. Actions centralize shortcut handling and work regardless of which control has focus.

uses ActnList, Menus;

type
  TMain = class(TForm)
    ActionList1: TActionList;
    actCtrl1: TAction;
    procedure FormCreate(Sender: TObject);
    procedure actCtrl1Execute(Sender: TObject);
  end;

procedure TMain.FormCreate(Sender: TObject);
begin
  KeyPreview := True; // keeps your existing handlers working too
  actCtrl1 := TAction.Create(Self);
  actCtrl1.ActionList := ActionList1;
  actCtrl1.ShortCut := TextToShortCut('Ctrl+1'); // Menus unit
  actCtrl1.OnExecute := actCtrl1Execute;
end;

procedure TMain.actCtrl1Execute(Sender: TObject);
begin
  // Set all ComboBoxes to the desired items here
end;

Quick checks if it still does not fire:

  • Verify nothing else already uses Ctrl+1 (a menu item or another action). Change the shortcut or disable the conflicting one.
  • If you stick with OnKeyDown, handle both the top-row key and the numpad: test for Ord('1') and VK_NUMPAD1, and check ssCtrl in Shift as showed.
  • For components with their own key handling (e.g., when a ComboBox drop-down is open), actions are more reliable than raw form key events.

This keeps your shortcuts consistent and easy to expand.

Recommended Answers

All 3 Replies

You are attempting to trap the key stroke on the form instead of a
specific component.

Set the KeyPreview property to true or in the OnCreate Method as below
Add a FormKeyDown Event to process your requirements

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key=ord('1')) and (ssCtrl in Shift) then
  begin
    Edit1.Text:='Pressed';
  { Add your code here }
    Key:=0;  // if required
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  KeyPreview:=True;
end;

end.

TRY This:

procedure TMain.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    CASE Key OF
      Ord('1') : IF Shift = [SSCTRL] THEN
                 Edit1.Text := 'CTRL 1 - Pressed'
                 ELSE IF Shift = [SSAlt] THEN
                 Edit1.Text := 'Alt 1 - Pressed'
      End;  {CASE}

end;

You are attempting to trap the key stroke on the form instead of a
specific component.

Set the KeyPreview property to true or in the OnCreate Method as below
Add a FormKeyDown Event to process your requirements

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key=ord('1')) and (ssCtrl in Shift) then
  begin
    Edit1.Text:='Pressed';
  { Add your code here }
    Key:=0;  // if required
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  KeyPreview:=True;
end;

end.

Thank you for your replies! I think what I was missing was KeyPreview := true. Your case statements saved me time as well. Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.