Solved

Delphi Component

Posted on 2004-04-28
4
391 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
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 125 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 22

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 88
delphi exception 7 61
LAN or WAN ? 11 81
TEMBEDDEDWB how can i change its user agent ? 8 47
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

17 Experts available now in Live!

Get 1:1 Help Now