Solved

Use a Background Application to Perform Mouse Operations in a Foreground Application

Posted on 2004-09-15
4
196 Views
Last Modified: 2010-08-05
I have a vendor supplied program which allows me to export data to a file by doing
a series of mouse operations.  I'd like to have a background  program which would
perform the export mouse operations periodically without my intervention.  The
mouse targets are in predictable positions.  

I've researched many documents on writing code to perform various mouse operations
and they seem relatively straightforward.  However, I'm not sure about how to structure
the background program so that it does what I want without interfering with the
foreground application.  Also, I have a feeling that such a program may have been written before, maybe many times.

Do you know of any source code that would do what I want?  And if not, can you give me some guidance on how to structure my background program?  

0
Comment
Question by:RalphSCoffin
4 Comments
 
LVL 6

Expert Comment

by:Amir Azhdari
Comment Utility
the easiest way to get all the systems mouse button Downs and Ups and ... is with the Journal hook.  This code uses a Form with 2 buttons on it, one button starts the journal hook and the other button stops it. There is One List box which will show all the mouse ups and downs and their screen coordinates.
There is also one TApplicationEvents to get the OnMessage event, so you can restart the hook if there's a WM_CANCELJOURNAL message.


unit MouseButton;

interface

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

type
 TForm1 = class(TForm)
   Button_StartJour: TButton;
   Button_StopJour: TButton;
   ListBox1: TListBox;
   ApplicationEvents1: TApplicationEvents;
   procedure Button_StartJourClick(Sender: TObject);
   procedure Button_StopJourClick(Sender: TObject);
   procedure ApplicationEvents1Message(var Msg: tagMSG;
     var Handled: Boolean);
   procedure FormClose(Sender: TObject; var Action: TCloseAction);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;
 JHook: THandle;
 Track: Boolean;

implementation

{$R *.DFM}

function JournalProc(Code, wParam: Integer; var EventStrut: TEVENTMSG): Integer; stdcall;
var
Char1: PChar;
begin
{this is the JournalRecordProc}
Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut));
{the CallNextHookEX is not really needed for journal hook since it it not
really in a hook chain, but it's standard for a Hook}
if Code < 0 then Exit;

{you should cancel operation if you get HC_SYSMODALON}
if Code = HC_SYSMODALON then Exit;
if Code = HC_ACTION then
 begin
{the EventStrut record has the Information about the mouse or keyboard
event. You said you just wanted the mouse button events so I get the
mouse down and mouse up event messages}
   if EventStrut.message = WM_LBUTTONUP then
   Form1.ListBox1.Items.Add('Left Mouse UP at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
   if EventStrut.message = WM_LBUTTONDOWN then
   Form1.ListBox1.Items.Add('Left Mouse Down at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
   if EventStrut.message = WM_RBUTTONDOWN then
   Form1.ListBox1.Items.Add('Right Mouse Down at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
   if (EventStrut.message = WM_RBUTTONUP) then
   Form1.ListBox1.Items.Add('Right Mouse Up at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
 end;
end;

procedure TForm1.Button_StartJourClick(Sender: TObject);
begin
if Track then
 begin
 ShowMessage('Mouse is already being Journaled, can not restart');
 Exit;
 end;

JHook := SetWindowsHookEx(WH_JOURNALRECORD , @JournalProc, 0, 0);
{SetWindowsHookEx starts the Hook}
if JHook > 0 then
 begin
 Track := True;
 end else
 ShowMessage('No Journal Hook availible');
end;

procedure TForm1.Button_StopJourClick(Sender: TObject);
begin
Track := False;
UnhookWindowsHookEx(JHook);
JHook := 0;
end;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
 var Handled: Boolean);
begin
{the journal hook is automaticly camceled if the Task manager
(Ctrl-Alt-Del) or the Ctrl-Esc keys are pressed, you restart it
when the WM_CANCELJOURNAL is sent to the parent window, Application}
Handled := False;
if (Msg.message = WM_CANCELJOURNAL) and Track then
 JHook := SetWindowsHookEx(WH_JOURNALRECORD , @JournalProc, 0, 0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{make sure you UN hook it if the app closes}
UnhookWindowsHookEx(JHook);
end;

end.



- - - - - - - - - - - - - - - - -  - - - - - - - - - - - -

Hope you can use it, ask questions if you need more.
0
 

Author Comment

by:RalphSCoffin
Comment Utility
I want my background program to position the mouse over specific targets
on the screen and click or double click to cause the foreground program
(on the screen) to react just as though I had manually positioned the mouse
and clicked it myself.  Collecting data on mouse activity from the journal
is not of use to me in what I'm trying to do.  
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
hello  RalphSCoffin, , what would be of use to you? ? ?  I am not sure what to do to help you? If you already know the positions of the controls you need to click , then just use the  mouse_event( ) function to "CLICK" that control, the mouse_event( ) function can do a "CLICK" (mouse down ans mouse Up) at any position , so you will need to know the postion of the control. I can not see any reason to position the cursor over the control, the postion of the cursor has nothing, that I know of, ,  to do with the programatic mouse down and mouse up  mouse_event( ) funtion. . . .  if you know the window's system handle for the controls, then you can also send them system messageses for the "CLICK" events. . . so you might say what you can do or have knowledge about the "Foreground"  app.
the jounaling hook is an excelent way to record and then PLAYBACK a series of mouse or keyboard events, although  AmirAzhdari  did not show how to store and PLAYBACK the recorded journaling. . .
0
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
Comment Utility
Hi,

The following example comes from one of my working apps. It has a timer and stays in the background.

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  IsWorking: boolean;

implementation

{$R *.DFM}

procedure MouseMoveAndClick(x, y: Integer);
var
  OldCoord: TPoint;
begin
  // save current position
  GetCursorPos(OldCoord);
  // move to new position
  SetCursorPos(x, y);
  // click
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  // restore position
  SetCursorPos(OldCoord.x, OldCoord.y);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  h: HWND;
begin
 if not IsWorking then begin
  IsWorking := true;
  try
  // check if the time has come, i.e. your app has to perform mouse clicks
  if <something> then begin
    // find, show and maximize the target app
    h := FindWindow(nil, 'TheFormTitle');
    if IsWindow(h) then begin
      ShowWindow(h, SW_RESTORE);
      // maximaze it
      if not IsZoomed(h) then begin
        ShowWindow(h, SW_MAXIMIZE);
      end;

      // do your mouse clicks here - in absolute screen coordinates
      MouseMoveAndClick(20, 50);
      MouseMoveAndClick(100, 200);
      // etc.
    end;
  end;
  finally
    IsWorking := false;
  end;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IsWorking := false;
  Timer1.Enabled := true;
end;

Regards, Geo
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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…
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: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now