944,204 Members | Top Members by Rank

Ad:
Nov 5th, 2009
0

KeyPress, KeyDown not triggering event

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
CanYouHandstand is offline Offline
27 posts
since Nov 2009
Nov 6th, 2009
0
Re: KeyPress, KeyDown not triggering event
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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. type
  2. TForm1 = class(TForm)
  3. GroupBox1: TGroupBox;
  4. Edit1: TEdit;
  5. procedure FormKeyDown(Sender: TObject; var Key: Word;
  6. Shift: TShiftState);
  7. procedure FormCreate(Sender: TObject);
  8. private
  9. { Private declarations }
  10. public
  11. { Public declarations }
  12. end;
  13.  
  14. var
  15. Form1: TForm1;
  16.  
  17. implementation
  18.  
  19. {$R *.DFM}
  20.  
  21. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  22. Shift: TShiftState);
  23. begin
  24. if (key=ord('1')) and (ssCtrl in Shift) then
  25. begin
  26. Edit1.Text:='Pressed';
  27. { Add your code here }
  28. Key:=0; // if required
  29. end;
  30.  
  31. end;
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35. KeyPreview:=True;
  36. end;
  37.  
  38. end.
cao
Reputation Points: 11
Solved Threads: 5
Newbie Poster
cao is offline Offline
12 posts
since Oct 2009
Nov 6th, 2009
0
Re: KeyPress, KeyDown not triggering event
TRY This:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TMain.FormKeyDown(Sender: TObject; var Key: Word;
  2. Shift: TShiftState);
  3. begin
  4. CASE Key OF
  5. Ord('1') : IF Shift = [SSCTRL] THEN
  6. Edit1.Text := 'CTRL 1 - Pressed'
  7. ELSE IF Shift = [SSAlt] THEN
  8. Edit1.Text := 'Alt 1 - Pressed'
  9. End; {CASE}
  10.  
  11. end;
Click to Expand / Collapse  Quote originally posted by cao ...
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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. type
  2. TForm1 = class(TForm)
  3. GroupBox1: TGroupBox;
  4. Edit1: TEdit;
  5. procedure FormKeyDown(Sender: TObject; var Key: Word;
  6. Shift: TShiftState);
  7. procedure FormCreate(Sender: TObject);
  8. private
  9. { Private declarations }
  10. public
  11. { Public declarations }
  12. end;
  13.  
  14. var
  15. Form1: TForm1;
  16.  
  17. implementation
  18.  
  19. {$R *.DFM}
  20.  
  21. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  22. Shift: TShiftState);
  23. begin
  24. if (key=ord('1')) and (ssCtrl in Shift) then
  25. begin
  26. Edit1.Text:='Pressed';
  27. { Add your code here }
  28. Key:=0; // if required
  29. end;
  30.  
  31. end;
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35. KeyPreview:=True;
  36. end;
  37.  
  38. end.
Reputation Points: 11
Solved Threads: 3
Newbie Poster
quaifp1 is offline Offline
10 posts
since Oct 2009
Nov 9th, 2009
0
Re: KeyPress, KeyDown not triggering event
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.
Reputation Points: 10
Solved Threads: 2
Light Poster
CanYouHandstand is offline Offline
27 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: runtime error as IOresult<>0
Next Thread in Pascal and Delphi Forum Timeline: Exporting table data to CSV.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC