Solved

Delphi Component

Posted on 2004-04-28
4
390 Views
Last Modified: 2013-11-23
Hi Experts!
               I am writing a components. One of the feature of compnents is to respond to Escape key even the component does not have the focus. I am wondering is there is any way i can achieve this functionality for my component. Please if any body can help me explain how i can achieve this functionality. The most important feature is if an Esc key is detected is for the component to run its new method.

Thank You

Zahid.
0
Comment
Question by:David_m43
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
possibilities
- you could lookup in a small intervall the keyboardstate
  - GetAsyncKeyBoardState
  - GetKeyBoardState

- you could hook the keyboard (dll needed)
  - search for "hook keyboard" here
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 125 total points
Comment Utility
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
The real question is if your component should do that at all.
It is either a windowed control which means that it can focus and should only react to keys when focused
or it is a non-windowed control which should not react to keys at all.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
Comment Utility
Another way is to register a HotKey

Here's my version....

unit MyComponent;

interface
uses
 Windows, Messages, SysUtils, Classes,Controls;

type
 TMyComponent = class(TComponent)
 private
   FEnabled: Boolean;
   FRegistered: Boolean;
   prOldWndProc: TWndMethod;
   FHotkey: TSHortcut;
   FOnHotKeyPressed: TNotifyEvent;
   prWindow: TWinControl;
   prHandle: HWND;
   procedure RegisterKey;
   procedure UnRegisterKey;
   { Private declarations }
 protected
   { Protected declarations }
   procedure SetEnabled(const Value: Boolean);
   procedure SetHotKey(const Value: TShortcut);
   procedure NewWindowProc(var Message: TMessage);
   procedure Loaded; override;
 public
   { Public declarations }
   constructor Create(aOwner: TComponent); override;
   destructor Destroy; override;
   property Registered: boolean read FRegistered;
 published
   { Published declarations }
   property Enabled: Boolean read FEnabled write SetEnabled default true;
   property Key: TShortcut read FHotkey write SetHotkey;
   property OnHotKey: TNotifyEvent read FOnHotKeyPressed write FOnHotKeyPressed;
 end;

procedure Register;

implementation


{ TMyComponent }

constructor TMyComponent.Create(aOwner: TComponent);
begin
 inherited;
 FEnabled := True;
 FHotKey := 0;
 FRegistered := false;
 if not (csDesigning in ComponentState) then
 begin
   prWindow := TWinControl(Self.Owner);
   prOldWndProc := prWindow.WindowProc;
   prWindow.WindowProc := NewWindowProc;
   prHandle := prWindow.Handle;
 end;
end;

destructor TMyComponent.Destroy;
begin
 if not (csDesigning in ComponentState) then
 begin
   UnRegisterKey;
   prWindow.WindowProc := prOldWndProc;
 end;
 inherited;
end;

procedure TMyComponent.Loaded;
begin
 inherited;
 if (FEnabled) and (not (csDesigning in ComponentState)) then RegisterKey;
end;

procedure TMyComponent.NewWindowProc(var Message: TMessage);
begin
 case Message.Msg of
   WM_HOTKEY:
   begin
     if assigned(FOnHotkeyPressed) then
       FOnHotKeyPressed(self);
   end;
 end;
 prOldWndProc(Message);
end;

procedure TMyComponent.RegisterKey;
procedure ShortCutToKey(ShortCut: TShortCut; var Key: Word; var Shift: TShiftState);
begin
  Key := ShortCut and not (scShift + scCtrl + scAlt);
  Shift := [];
  if ShortCut and scShift <> 0 then Include(Shift, ssShift);
  if ShortCut and scCtrl <> 0 then Include(Shift, ssCtrl);
  if ShortCut and scAlt <> 0 then Include(Shift, ssAlt);
end;
var
 lVKey: word;
 lvShift: TShiftstate;
 lModifier: UINT;
begin
 if not FRegistered then
 begin
   lModifier := 0;
   ShortcutToKey(fHotKey, lvKey, lvShift);

   if ssShift in lvShift  then
     lModifier := lModifier or MOD_SHIFT;

   if ssAlt in lvShift then
     lModifier := lModifier or MOD_ALT;

   if ssCtrl in lvShift then
     lModifier := lModifier or MOD_CONTROL;

   FRegistered := RegisterHotKey(prWindow.Handle, 0, lModifier, LOBYTE(lVKey));
 end;
end;

procedure TMyComponent.SetEnabled(const Value: Boolean);
begin
 FEnabled := Value;
 if FEnabled then
   RegisterKey
 else
   UnregisterKey;
end;

procedure TMyComponent.UnRegisterKey;
begin
 if FRegistered then
 begin
   UnregisterHotKey(prHandle, 0);
   FRegistered := False;
 end;
end;

procedure TMyComponent.SetHotKey(const Value: TShortcut);
begin
 if Value <> FHotkey then
 begin
   FHotkey := Value;
   if not (csDesigning in ComponentState) then
   begin
       UnRegisterKey;
       if FEnabled then
         RegisterKey;
   end;
 end;
end;

procedure Register;
begin
 RegisterComponents('Samples', [TMyComponent]);
end;

end.

the Key Shortcut value must be integer (in your case for Escape Key 27) but if you want to show it in object inspector like for the MenuItem Shorcut property you need a property editor...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
code issue 8 84
how to center only a line in richedit? 4 45
Reconfigure Delphi Install? 2 34
Multiple image collision 13 42
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

16 Experts available now in Live!

Get 1:1 Help Now