Solved

how can I hook keyboard strokes within dynamically created component

Posted on 2004-04-23
3
386 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
[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
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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