Solved

trapping key strokes when not current app

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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