Solved

trapping key strokes when not current app

Posted on 1998-06-30
4
228 Views
Last Modified: 2010-04-04
I want to be able to process keystrokes when users are not in my form. Specifically I want to know when the user presses the escape key at any time, but I would imagine that the same procedure applies to trapping all keys.

I have found some info on this message board about trapping keys when your app has focus, but none about general windows keyboard trapping.
0
Comment
Question by:dob1
4 Comments
 

Expert Comment

by:lobraco
ID: 1356674
I think that you can stand infront of the user and when they hit the keys that you want to trap, push them out of the way to initiate your event. How does this sound?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1356675
see Intercepting keystrokes in:
http://www.web-span.com/pjohnson/
0
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
ID: 1356676
Here is a sample on how to do it.

It doesn't identify the window the keys come from but there is a hwnd included in the TEventMsg so you can do it.

I didn't translate all the keys but here is a start.

I tried using the WH_KEYBOARD but the messages were already away then. This method retrieves also mouse messages etc. It is designed to record macro's with.

*** start of code ***

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
var
  Hook : HHook;

function Catch(Code : Integer; WParam : WPARAM; LParam : LPARAM) : LRESULT; stdcall;
var
  VirtKey : Word;
  ShiftState : TShiftState;
  pre : String;
  aft : String;
begin
  if Code<0 then begin
    Result:=CallNextHookEx(Hook,code,WPARAM,LPARAM);
  end else begin
    case code of
      HC_ACTION : begin
        with PEventMsg(LPARAM)^ do begin
          if message = WM_KEYDOWN then begin
             ShiftState:=KeyDataToShiftState(ParamH);
             pre:='';
             if ssShift in ShiftState then pre:='Shift';
             if ssCtrl in ShiftState then  pre:=pre+'+Ctrl';
             if ssAlt in ShiftState then pre:=pre+'+Alt';
             if Length(pre)>0 then
               if pre[1]='+' then pre:=Copy(pre,2,100);
             VirtKey := Lo(ParamL);
             aft:='';
             if (VirtKey>=Ord('A')) and (VirtKey<=Ord('Z')) then
               aft:=Chr(VirtKey);
             if (VirtKey>=Ord('0')) and (VirtKey<=Ord('9')) then
               aft:=Chr(VirtKey);
             if VirtKey = VK_BACK then aft:='BackSpace';
             if VirtKey = VK_DELETE then aft:='Delete';
             if VirtKey = VK_TAB then aft:='Tab';
             if VirtKey = VK_RETURN then aft:='Return';
             if VirtKey = VK_ESCAPE then aft:='Escape';
             if aft<>'' then
               Form1.Memo1.Lines.Add(pre+' '+aft);
          end;
        end;
      end;
      HC_SYSMODALOFF : begin
      end;
      HC_SYSMODALON : begin
      end;
    end;
    Result:=0;              // TMessage TEventMessage
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Hook:=SetWindowsHookEx(WH_JOURNALRECORD,Catch,HINSTANCE,0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if not UnhookWindowsHookEx(Hook) then ShowMessage('UnHook failed');;
end;

end.

*** end of code ***

Regards Jacco
0
 

Expert Comment

by:djadja
ID: 3449153
Doesn't catch keys from other windows; only form1.
0

Featured Post

Industry Leaders: 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

Suggested Solutions

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…
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…

713 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