Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

trapping key strokes when not current app

Posted on 1998-06-30
4
Medium Priority
?
233 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

610 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