?
Solved

Delphi Component

Posted on 2004-04-28
4
Medium Priority
?
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10946483
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 500 total points
ID: 10946519
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 10946640
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 23

Expert Comment

by:Ferruccio Accalai
ID: 10947514
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

801 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