Solved

how can I hook keyboard strokes within dynamically created component

Posted on 2004-04-23
3
383 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

789 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