• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

how can I hook keyboard strokes within dynamically created component

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
Strona01
Asked:
Strona01
  • 2
1 Solution
 
esoftbgCommented:
0
 
esoftbgCommented:
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
 
Strona01Author Commented:
I already had code to that effect but when my function was called the program crashed.

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now