Solved

trapping key strokes when not current app

Posted on 1998-06-30
4
225 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now