Solved

how can I hook keyboard strokes within dynamically created component

Posted on 2004-04-23
3
376 Views
Last Modified: 2010-04-05
I have a component of type TCustomPanel that I have created. I want to include the ability to capture a keystroke (escape key) when the program that uses my component is not in focus.  I am trying to insert a SetWindowsHookEx Function to capture an escape keypress within the component without luck.  The escape key should refocus the program and call a function within the component.

thanks
0
Comment
Question by:Strona01
  • 2
3 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 10906782
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 100 total points
ID: 10909274
The code below is 99% based on the above link

unit eCustomPanel;

interface

uses
  Windows, SysUtils, Classes, Controls, StdCtrls, ExtCtrls, Graphics;

type
  TeCustomPanel = class(TCustomPanel)
    private     { Private declarations }
      FdoEsp:     Boolean;
      FColor:     TColor;
    protected   { Protected declarations }
    public      { Public declarations }
      NumPress:   Integer;
      constructor Create(AOwner: TComponent); override;
      procedure   EspKey;
      destructor  Destroy; override;
    published   { Published declarations }
  end;

procedure Register;

var
  FKeyHook:       THandle;
  Hooked:         Boolean;
  eCustomArray:   array of TeCustomPanel;

implementation

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

function HookFunc(Code, VirtualKey, KeyStroke: Integer): Integer; stdcall;
var
  I: Integer;
begin
  Result := 0;
  if (Code<>HC_NOREMOVE) then
  begin
    // Since do not blocked or changed keys, do CallNextHookEx first
    Result := CallNextHookEx(FKeyHook, Code, VirtualKey, KeyStroke);
    if (Code>=0) then
    begin
      if (Code=HC_ACTION) then
        if ((KeyStroke and (1 shl 30)) = 0) then
          if VirtualKey = VK_ESCAPE then
            for I := 0 to High(eCustomArray) do
              eCustomArray[I].EspKey;
    end;
  end;
end;

constructor TeCustomPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := [csAcceptsControls, csOpaque, csReplicatable];
  if not Hooked then
  begin
    if not (csDesigning in ComponentState) then
      FKeyHook := SetWindowsHookEx(WH_KEYBOARD, @HookFunc, 0, GetCurrentThreadID)
    else
      Hooked := True;
    if (FKeyHook<>0) then
      Hooked := True;
  end;
  FColor := clRed;
  Width := 100;
  Height := 100;

  NumPress := 0;
  FdoEsp := True;

  SetLength(eCustomArray, Length(eCustomArray)+1);
  eCustomArray[High(eCustomArray)] := Self;
end;

procedure TeCustomPanel.EspKey;
begin
  // this is just for this demo
  Canvas.Brush.Color := clBtnFace;
  Canvas.TextOut(4,32, 'Escape Key is Pressed '+IntToStr(NumPress));
  if FdoEsp then
    Inc(NumPress)
  else
    Dec(NumPress);
end;

destructor TeCustomPanel.Destroy;
var
  I, J:    Integer;
begin
  for I := 0 to High(eCustomArray) do
  begin
    if (eCustomArray[I]=Self) then
    if (I=High(eCustomArray)) then
    begin
      SetLength(eCustomArray, Length(eCustomArray)-1);
      Break;
    end
    else
    begin
      for J := I to High(eCustomArray) do
        eCustomArray[J] := eCustomArray[J+1];
      SetLength(eCustomArray, Length(eCustomArray)-1);
      Break;
    end;
  end;
  inherited Destroy;
end;

initialization
  Hooked := False;
  FKeyHook := 0;

finalization
  UnhookWindowsHookEx(FKeyHook);

end.
0
 

Author Comment

by:Strona01
ID: 10923221
I already had code to that effect but when my function was called the program crashed.

Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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