Solved

Hotkey component for form onkeydown event?

Posted on 2001-08-30
7
369 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now