• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

Hotkey component for form onkeydown event?

Is it possible for a hotkey component to be used to configure the key combinations pressed in a form's onkeydown event?

I would like to create two forms where the first form has the hotkey event that configures the hotkey combination. When the second window is displayed, a hidden component is visible when the user presses the combination keys on the form that's configured in the first form.

I would appreciate source code implemented in the answers, thanks.
0
NitroBlaze
Asked:
NitroBlaze
  • 4
  • 3
1 Solution
 
JaccoCommented:
Hi NitroBlaze,

I am not sure I understand completely what you mean.

Just let me know if this is it.

Is it something like this:

  class TForm1(TForm)
    procedure FormCreate(Sender: TObject);
    HotKeyController: THotKeyController; (* your component *)
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  HotKeyController.Key := VK_ENTER;
  HotKeyController.Shift := [ssCtrl, ssAlt];
end;

  class TForm2(TForm)
    Form2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  end;


procedure Form2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Form1.HotKeyController.Test(Key, Shift) then
  begin
    MyHiddenControl.Visible := True;
  end;
end;

Regards Jacco
0
 
NitroBlazeAuthor Commented:
is it possible to use the pre-installed Hotkey component located in the Advanced components tab in Delphi? It handles different properties like Modifiers for the Shift and Shortcut for the Key property.
0
 
JaccoCommented:
Delphi 6 right? I don't have it at home.

Regards Jacco
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
NitroBlazeAuthor Commented:
Yes. In that case, what components do you recommend me to check out on this matter? Thanks.
0
 
JaccoCommented:
Hi NitroBlaze,

In the office I have Delphi six so I've tried to make you a sample. Here is a sample on how to do it.

Good luck and regards Jacco

*** start of code ***
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    HotKey1: THotKey;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TForm2.Create(Self) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

end.
*** end of code ***
*** start of code ***
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses
  Unit1, QMenus;

{$R *.dfm}

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if ShortCut(Key, Shift) = Form1.HotKey1.HotKey then
    Memo1.Visible := True;
end;

end.
*** end of code ***
0
 
NitroBlazeAuthor Commented:
The code does not accept common windows keyboard shortcuts like (Ctrl-A : Select All), that's workable by registering the combination beforehand, other than that, it works perfectly.

Thanks.
0
 
JaccoCommented:
Try setting KeyPreview of the form to True

If you do that the form will see the key first. If you set Key to 0 in FormKeyDown the active control of the form will not handle the key. (It is tricky sometimes though).

And don't forget the HotKey component has a property for not allowing some key combinations.

Regards Jacco

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now