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

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.

This article has been dead for over six months. Start a new discussion instead.