RAD Studio VCL Reference
|
Specifies whether the form should receive keyboard events before the active control.
property KeyPreview: Boolean;
__property Boolean KeyPreview;
If KeyPreview is true, keyboard events occur on the form before they occur on the active control. (The active control is specified by the ActiveControl property.)
If KeyPreview is false, keyboard events occur only on the active control.
Navigation keys (Tab, BackTab, the arrow keys, and so on) are unaffected by KeyPreview because they do not generate keyboard events. Similarly, when a button has focus or when its Default property is true, the Enter key is unaffected by KeyPreview because it does not generate a keyboard events.
KeyPreview is false by default.
C++ Examples:
/* This example changes a form’s color to aqua when the user presses the F1 key, even when a control on the form has the focus. When the user releases the key, the form returns to its original color. Set the KeyPreview on the form to True. Set the OnKeyDown event to FormKeyDown and set the OnKeyUp event to FormKeyUp. Set the KeyPreview on the form to True. */ void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == VK_F1) { FormColor = Form1->Color; Form1->Color = clAqua; if (ComboBox1->Text == "KeyDown") ComboBox1->Text = "KeyDown2"; else ComboBox1->Text = "KeyDown"; Form1->Refresh(); Memo1->Lines->Add(ComboBox1->Text); } } void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == VK_F1) { Form1->Color = FormColor; if (ComboBox1->Text == "KeyUp") ComboBox1->Text = "KeyUp2"; else ComboBox1->Text = "KeyUp"; Form1->Refresh(); Memo1->Lines->Add(ComboBox1->Text); } }
Delphi Examples:
{ This example changes a form’s color to aqua when the user presses the F1 key, even when a control on the form has the focus. When the user releases the key, the form returns to its original color. Set the KeyPreview on the form to True. Set the OnKeyDown event to FormKeyDown and set the OnKeyUp event to FormKeyUp. Set the KeyPreview on the form to True. } var FormColor: TColor; procedure TForm1.FormCreate(Sender: TObject); begin KeyPreview := True; FormColor := Form1.Color; end; procedure TForm1.FormKeyDown( Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_F1 then begin Form1.Color := clAqua; if (ComboBox1.Text = 'KeyDown') then ComboBox1.Text := 'KeyDown2' else ComboBox1.Text := 'KeyDown'; WriteLn(ComboBox1.Text); end; end; procedure TForm1.FormKeyUp( Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_F1 then begin Form1.Color := FormColor; if (ComboBox1.Text = 'KeyUp') then ComboBox1.Text := 'KeyUp2' else ComboBox1.Text := 'KeyUp'; WriteLn(ComboBox1.Text); end; end;
{ The following code aborts a print job if the user presses Esc. Note that you should set KeyPreview to true to ensure that the OnKeyDown event handler of Form1 is called. } uses Printers; procedure TForm1.Button1Click(Sender: TObject); var I, X, Y: Integer; Memo1 : TMemo; r: TRect; begin Memo1 := TMemo.Create(Form1); Memo1.Parent := Form1; Memo1.Visible := True; Memo1.Width := 700; if (OpenDialog1.Execute) then begin Memo1.Lines.LoadFromFile(OpenDialog1.FileName); Printer.BeginDoc; X := 200; Y := 200; for I := 0 to 140 do begin Printer.Canvas.TextOut(X, Y, Memo1.Lines[I]); Y := Y + 80; if (Y > (Printer.PageHeight - 300)) then begin Y := 200; Printer.NewPage; end; end; Printer.EndDoc; end; end; procedure TForm1.FormCreate(Sender: TObject); begin KeyPreview := True; end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (Key=VK_ESCAPE) and Printer.Printing then begin Printer.Abort; MessageDlg('Printing aborted', mtInformation, [mbOK],0); end; end;
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|