Solved

trapping key strokes when not current app

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

16 Experts available now in Live!

Get 1:1 Help Now