Solved

Hotkey component for form onkeydown event?

Posted on 2001-08-30
7
391 Views
Last Modified: 2010-04-06
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
Comment
Question by:NitroBlaze
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:Jacco
ID: 6443665
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
 

Author Comment

by:NitroBlaze
ID: 6447203
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
 
LVL 10

Expert Comment

by:Jacco
ID: 6447208
Delphi 6 right? I don't have it at home.

Regards Jacco
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:NitroBlaze
ID: 6447219
Yes. In that case, what components do you recommend me to check out on this matter? Thanks.
0
 
LVL 10

Accepted Solution

by:
Jacco earned 100 total points
ID: 6450622
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
 

Author Comment

by:NitroBlaze
ID: 6451930
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
 
LVL 10

Expert Comment

by:Jacco
ID: 6452287
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 67
Connection between libmysql.dll and MySQL Versions 7 112
Downloading email attachments 2 79
Performance of SQL statement 37 127
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question