?
Solved

how can I hook keyboard strokes within dynamically created component

Posted on 2004-04-23
3
Medium Priority
?
387 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 400 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

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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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