Solved

How do I trap an event  in another application?

Posted on 2001-08-01
6
229 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:bjornborg
6 Comments
 

Accepted Solution

by:
GoodTimes earned 100 total points
ID: 6342597
What you need to do is set a Hook (look into Win32.hlp under SetWindowsHookEx and using the WH_CBT and WH_KEYBOARD Hooks.

Omri
0
 

Expert Comment

by:GoodTimes
ID: 6342605
oops, sorry, forget the WH_CBT. Only use WH_KEYBOARD.

Omri
0
 
LVL 3

Expert Comment

by:rondi
ID: 6343648
Don't forget to put all your hookin code in a DLL
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6345194
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
0
 

Author Comment

by:bjornborg
ID: 6355994
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
0
 

Author Comment

by:bjornborg
ID: 6356044
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)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Communication Between RC4 Delphi <-> PHP 3 115
how to center only a line in richedit? 4 57
Error E2158 compiling with Delphi XE10 Seattle 2 100
Delphi Yen format 3 35
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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