//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "KeyPreview.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
/*
KeyPreview, OnKeyDown, OnKeyUp example
This cross-platform 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.
*/

TColor FormColor;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  KeyPreview = true;
  FormColor = Form1->Color;
}
//---------------------------------------------------------------------------
// BeginExample: KeyPreview
// Property: Forms.TCustomForm.KeyPreview
// Event: Controls.TWinControl.OnKeyDown
// Event: Controls.TWinControl.OnKeyUp

// BeginCode
/*
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.
Behavior is dependent on keyboard driver(USB, serial, etc).
*/
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);
  }
}
// EndCode
// EndExample: KeyPreview
//---------------------------------------------------------------------------
