Back

Here is a way to create your own "Hot Keys".
This example uses 'Alt+O.

if (ssAlt in Shift) and (chr(Key) in ['O']) then
  begin
      MainForm.Editor.SelText := 'Yes it works!';
  end;


Using HotKeys

You can let users define their own hot keys in your programs with the THotKey component.

Put a TMainMenu component onto your form.
Double-click on MainMenu1 to bring up the Main Menu designer.
Click on the empty item and make it's Caption 'Form Properties'.
Click on your 'Form Properties' item, then click on the blank
item underneath 'Form Properties'.
Make this item's caption 'Form Color'.

Go back to Form1.
Drop a THotKey component onto your Form, and to the right of that put a TButton.

Click on your THotKey component and go to the Object Inspector, and then
double-click on the Modifiers property, and make hkAlt = False and hkCtrl = True.

Double-click on Button1 and put in this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FormColor1.Shortcut := HotKey1.HotKey;
end;

On Form1's MainMenu click Form Properties | Form Color and put in this code:

procedure TForm1.FormColor1Click(Sender: TObject);
begin
   if Form1.Color = clBlue then
  begin
    Form1.Color := clRed;
  end
  else
  begin
    Form1.Color := clBlue;
  end;
end;
------------------------
Run your program.

Click into the THotKey component and press any letter key.
You will see the text in the HotKey component change.
Click on Button1, and your key combination in the THoyKey component
will be the shortcut for your 'Form Color' item in MainMenu1.
Click the key combination and see how it works.


Back