Link to home
Start Free TrialLog in
Avatar of bjornborg
bjornborg

asked on

How do I trap an event in another application?

I would like to detect when the user executes e.g. a CTRL-C (Copy) command in another application.

When this happens I would like my Delphi application to take control and allow the user to Paste the
information into a Delphi Edit field.
ASKER CERTIFIED SOLUTION
Avatar of GoodTimes
GoodTimes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GoodTimes
GoodTimes

oops, sorry, forget the WH_CBT. Only use WH_KEYBOARD.

Omri
Don't forget to put all your hookin code in a DLL
Avatar of Mohammed Nasman
Hello

  do you want to hook the events or the clipboard, if you want to spy the clipboard, so if any one press ctrl+c or press copy from the menu, you will get that in ur delphi application, here's a program


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure WMDrawClipboard(var Msg: TMessage);
      message WM_DRAWCLIPBOARD;
 procedure WMChangeCBChain(var Msg: TMessage);
      message WM_CHANGECBCHAIN;

  public
  end;

var
  Form1: TForm1;
  NextInChain : THandle;

implementation

uses ClipBrd;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
NextInChain := SetClipboardViewer(Handle);
end;

procedure TForm1.WMDrawClipboard(var Msg:TMessage);
begin
 if Clipboard.HasFormat(cf_text) then begin
  Memo1.Lines.Clear;
  Memo1.PasteFromClipboard
 end else begin
  //do something with other clipboard formats
 end;
 //pass the message on to the next window
 if NextInChain <> 0 then
  SendMessage(NextInChain, WM_DrawClipboard, 0, 0)
end;

procedure TForm1.WMChangeCBChain(var Msg: TMessage);
var
  Remove, Next: THandle;
begin
  Remove := Msg.WParam;
  Next := Msg.LParam;
 with Msg do
  if NextInChain = Remove then
   NextInChain := Next
  else if NextInChain <> 0 then
   SendMessage(NextInChain, WM_ChangeCBChain,
               Remove, Next)
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ChangeClipboardChain(Handle, NextInChain);
end;

end.


I hope that what u need

Mohammed
Avatar of bjornborg

ASKER

Thank you Mohammed,
Your example works fine, but I guess I have to use the SetWindowsHookEx within a DLL as Omri suggested in order to "stop" the current application and transfer the control to the Delphi application.

After having seen the comment/answer from Omri, I have searched for more information about "SetWindowsHookEx" and have found the answer to "Global hooks" which has a detailed and long answer with example application and DLL.

Thanks
Bj?rn
The answer points to the routine that must be used, but does not provide solution example (and/or could have pointed to existing example in database)