KeyPress, KeyDown not triggering event

Reply

Join Date: Nov 2009
Posts: 7
Reputation: CanYouHandstand is an unknown quantity at this point 
Solved Threads: 0
CanYouHandstand CanYouHandstand is offline Offline
Newbie Poster

KeyPress, KeyDown not triggering event

 
0
  #1
19 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: cao is an unknown quantity at this point 
Solved Threads: 4
cao cao is offline Offline
Newbie Poster
 
0
  #2
18 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: quaifp1 is an unknown quantity at this point 
Solved Threads: 3
quaifp1 quaifp1 is offline Offline
Newbie Poster
 
0
  #3
18 Days Ago
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;
Originally Posted by cao View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 7
Reputation: CanYouHandstand is an unknown quantity at this point 
Solved Threads: 0
CanYouHandstand CanYouHandstand is offline Offline
Newbie Poster
 
0
  #4
15 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC