| | |
KeyPress, KeyDown not triggering event
![]() |
•
•
Join Date: Nov 2009
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 4
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
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)
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.
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 3
0
#3 18 Days Ago
TRY This:
Pascal and Delphi Syntax (Toggle Plain Text)
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
Pascal and Delphi Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Javascript event stuff (JavaScript / DHTML / AJAX)
- Keypress of a textbox in asp.net with c#.net (JavaScript / DHTML / AJAX)
- events in counting vowels (C#)
- backspace and delete key (Visual Basic 4 / 5 / 6)
- about keypress in keyascii (Visual Basic 4 / 5 / 6)
- Minimize Media Player on Escape Button (VB.NET)
- Vb6 office 2000 (Visual Basic 4 / 5 / 6)
- fun, but hard work (C++)
- system cpu spikes to 50% every 2 min for 5 sec (Windows NT / 2000 / XP)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: runtime error as IOresult<>0
- Next Thread: Exporting table data to CSV.
| Thread Tools | Search this Thread |





