I'm trying to translate a Pascal mouse movement algorithm to C++ but I'm not too good at it. The mouse moves to a position but when it moves again it only goes a small increment and crashes after a few more moves or if I move the mouse a few times and interfere with it. It's supposed to mimic a human using the mouse.
Here is the page where the algorithm is and my code so far:

Pascal code:
http://villavu.com/repositories/srl/SRL/core/Mouse.scar

My code(functions I wanted):

#include "stdafx.h"

double random(double x) 
{ 
    srand((unsigned)time(0)); 
    return (double)((rand()%(int)x));
}

void WindMouse(double xs,double ys,double xe,double ye,double gravity,double wind,double minSleep,double maxSleep,double maxStep,double targetArea)
{
 double   veloX=0, veloY=0, windX=0, windY=0, veloMag, dist, randomDist, lastDist, step;
 int       lastX, lastY;
 double   sqrt2, sqrt3, sqrt5;
  
  sqrt2=  sqrt(2.0);
  sqrt3=  sqrt(3.0);
  sqrt5=  sqrt(5.0);
  while( _hypot(xs - xe,ys - ye) > 1)
   { 
    dist=  _hypot(xs - xe,ys - ye);
    wind=  min(wind, dist);
    if(dist >= targetArea )
     { 
      windX =  windX / sqrt3 + (random(floor(wind) * 2.0 + 1.0) - wind) / sqrt5;
      windY =  windY / sqrt3 + (random(floor(wind) * 2.0 + 1.0) - wind) / sqrt5;
    }   else
     { 
      windX=  windX / sqrt2;
      windY=  windY / sqrt2;
      if((maxStep < 3)   )
       { 
        maxStep= random(3) + 3.0;
      }   else
       { 
        maxStep=  maxStep / sqrt5;
       } 
     } 
    veloX=  veloX + windX;
    veloY=  veloY + windY;
    veloX=  veloX + gravity * (xe - xs) / dist;
    veloY=  veloY + gravity * (ye - ys) / dist;
    if(_hypot(veloX, veloY) > maxStep   )
     { 
      randomDist =  maxStep / 2.0 + random(floor(maxStep) / 2);
      veloMag=  sqrt(veloX * veloX + veloY * veloY);
      veloX=  (veloX / veloMag) * randomDist;
      veloY=  (veloY / veloMag) * randomDist;
     } 
    lastX=  (int)floor(xs);
    lastY=  (int)floor(ys);
    xs=  xs + veloX;
    ys=  ys + veloY;
    if((lastX != floor(xs)) || (lastY != floor(ys))   )
      SetCursorPos((int)floor(xs), (int)floor(ys));
    step=  _hypot(xs - lastX, ys - lastY);
    Sleep((int)floor((maxSleep - minSleep) * (step / maxStep) + minSleep));
    lastDist=  dist;
   } 
  if((floor(xe) != floor(xs)) || (floor(ye) != floor(ys))   )
    SetCursorPos((int)floor(xe), (int)floor(ye));
 }


void MMouse(int x,int y,int rx,int ry)
{
 int       cx, cy;
 double   randSpeed;
 POINT *p = new POINT;
  
  GetCursorPos(p);
  cx=p->x;
  cy=p->y;
    randSpeed=  (random(10.0) / 2.0 + 10.0) / 10.0;
    if(randSpeed == 0.0   )
      randSpeed =  0.1;
    x =  x + (int)random(rx);
    x =  y + (int)random(ry);
    WindMouse((double)cx,(double)cy,(double)x,(double)y,9.0,3.0,10.0/randSpeed,15.0/randSpeed,10.0*randSpeed,10.0*randSpeed);

	delete p;
 }


void Mouse(int mousex,int mousey,int ranx,int rany)
{
 int       a=0, b, c;
 POINT *p1 = new POINT;
  
  MMouse(mousex, mousey, ranx, rany);
  Sleep((int)(60 + random(30)));
  GetCursorPos(p1);
  b=p1->x;
  c=p1->y;
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  do{
    Sleep((int)(20 + random(30)));
    a =  a + 1;
  }while( (a > 4));
  GetCursorPos(p1);
  b=p1->x;
  c=p1->y;
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  Sleep((int)(100 + random(100)));
  delete p1;
}


void SleepAndSetCursorPos(int Time)
{
 bool   Moving;
 double   x, y, xv=0, yv=0;
 double   gx, gy;
 int       T;
 POINT *p2= new POINT;
  
  GetCursorPos(p2);
  x=(double)p2->x;
  y=(double)p2->y;
  if((random(2) == 0)   )
    Moving =  0;
  else
    Moving =  1;
  gx =  130 + random(500);
  gy =  130 + random(300);
  T =  GetTickCount();
  do{
    Sleep(10);
    if((Moving)   )
     { 
      if((gx > x)   )
        xv =  xv + 0.1;
      else
        xv =  xv - 0.1;
      if((gy > y)   )
        yv =  yv + 0.1;
      else
        yv =  yv - 0.1;
      x =  x + xv;
      y =  y + yv;
      SetCursorPos((int)floor(x), (int)floor(y));
     } 
    if((random(100) == 0)   )
      Moving =  !   Moving;
    if((random(30) == 0)   )
     { 
      gx =  130 + random(500);
      gy =  130 + random(300);
     } 
  }while( (abs((int)(GetTickCount() - T)) <= Time));

  delete p2;
}
Member Avatar for iamthwee

Just throwing ideas, but you could be trashing memory somewhere if your not familiar with pointers new/delete.

With any code porting, start with the basics.

Get it so it just moves the mouse to a random position first.
Then handle mouse clicks or whatever.

Member Avatar for iamthwee

For example, if you are not familiar with allocating and removing pointers you could quite easily do what you need with the following code . . .

Set the cursor position

#include <windows.h>

int main ( void )
{
   SetCursorPos ( 500, 50 );

   system ( "pause" );
   return 0;
}

Get the cursor position

#include <windows.h>
#include <iostream>

int main()
{
   POINT c;
   GetCursorPos ( &c );
   std::cout << c.x << ", " << c.y;
   system ( "pause" );
}
Member Avatar for iamthwee

Also I'd look very carefully at your windMouse function you could very be easily be doing a division by 0 somewhere!!!

Not good.

Member Avatar for iamthwee

After changing a few bits and bobs and not calling the windMouse function I can get it so it doesn't crash.

#include <iostream>
#include <windows.h>
#include <cmath>
#include <ctime> 
double random ( double x )
{
   srand ( ( unsigned ) time ( 0 ) );
   return ( double ) ( ( rand() % ( int ) x ) );
}

void WindMouse ( double xs, double ys, double xe, double ye, double gravity, double wind, double minSleep, double maxSleep, double maxStep, double targetArea )
{
   double   veloX = 1, veloY = 1, windX = 1, windY = 1, veloMag, dist, randomDist, lastDist, step;
   int       lastX, lastY;
   double   sqrt2, sqrt3, sqrt5;

   sqrt2 =  sqrt ( 2.0 );
   sqrt3 =  sqrt ( 3.0 );
   sqrt5 =  sqrt ( 5.0 );
   while ( _hypot ( xs - xe, ys - ye ) > 1 )
   {
      dist =  _hypot ( xs - xe, ys - ye );
      wind =  4;
      if ( dist >= targetArea )
      {
         windX =  windX / sqrt3 + ( random ( floor ( wind ) * 2.0 + 1.0 ) - wind ) / sqrt5;
         windY =  windY / sqrt3 + ( random ( floor ( wind ) * 2.0 + 1.0 ) - wind ) / sqrt5;
      }
      else
      {
         windX =  windX / sqrt2;
         windY =  windY / sqrt2;
         if ( ( maxStep < 3 )   )
         {
            maxStep = random ( 3 ) + 3.0;
         }
         else
         {
            maxStep =  maxStep / sqrt5;
         }
      }
      veloX =  veloX + windX;
      veloY =  veloY + windY;
      veloX =  veloX + gravity * ( xe - xs ) / dist;
      veloY =  veloY + gravity * ( ye - ys ) / dist;
      if ( _hypot ( veloX, veloY ) > maxStep   )
      {
         randomDist =  maxStep / 2.0 + random ( floor ( maxStep ) / 2 );
         veloMag =  sqrt ( veloX * veloX + veloY * veloY );
         veloX =  ( veloX / veloMag ) * randomDist;
         veloY =  ( veloY / veloMag ) * randomDist;
      }

   }

} 
void MMouse ( int x, int y, int rx, int ry )
{
   int       cx, cy;
   double   randSpeed;
   POINT p;

   GetCursorPos ( &p );
   cx = p.x;
   cy = p.y;
   randSpeed =  ( random ( 10.0 ) / 2.0 + 10.0 ) / 10.0;
   if ( randSpeed == 0.0   )
      randSpeed =  0.1;
   x =  x + ( int ) random ( rx );
   y =  y + ( int ) random ( ry );

} 
void Mouse ( int mousex, int mousey, int ranx, int rany )
{
   int       a = 0, b, c;
   POINT p1;

   MMouse ( mousex, mousey, ranx, rany );
   Sleep ( ( int ) ( 60 + random ( 30 ) ) );
   GetCursorPos ( &p1 );
   b = p1.x;
   c = p1.y; 
   mouse_event ( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
   do
   {
      Sleep ( ( int ) ( 20 + random ( 30 ) ) );
      a =  a + 1;
   }
   while ( ( a > 4 ) ); 
   POINT t1;
   GetCursorPos ( &t1 );
   b = t1.x;
   c = t1.y;
   mouse_event ( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
   Sleep ( ( int ) ( 100 + random ( 100 ) ) );

} 
void SleepAndSetCursorPos ( int Time )
{
   bool   Moving;
   double   x, y, xv = 0, yv = 0;
   double   gx, gy;
   int       T;
   POINT p2;

   GetCursorPos ( &p2 );
   x = p2.x;
   y = p2.y;
   if ( ( random ( 2 ) == 0 )   )
      Moving =  0;
   else
      Moving =  1;
   gx =  130 + random ( 500 );
   gy =  130 + random ( 300 );
   T =  GetTickCount();
   do
   {
      Sleep ( 10 );
      if ( ( Moving )   )
      {
         if ( ( gx > x )   )
            xv =  xv + 0.1;
         else
            xv =  xv - 0.1;
         if ( ( gy > y )   )
            yv =  yv + 0.1;
         else
            yv =  yv - 0.1;
         x =  x + xv;
         y =  y + yv;
         SetCursorPos ( ( int ) floor ( x ), ( int ) floor ( y ) );
      }
      if ( ( random ( 100 ) == 0 )   )
         Moving =  !   Moving;
      if ( ( random ( 30 ) == 0 )   )
      {
         gx =  130 + random ( 500 );
         gy =  130 + random ( 300 );
      }
   }
   while ( ( abs ( ( int ) ( GetTickCount() - T ) ) <= Time ) );

}

int main()
{
   SleepAndSetCursorPos ( 3000 );

}

Hmm, the SleepAndSetCursorPos was working even before, but I did change that pointer and used the address instead. I think the WindMouse function is the big problem here.

When I wrote it from pascal to c++ some variables were used without being defined so I set them to 0, seems even at 1 it crashes.

Could there be some operation that leads to a crash ? I can't really find it.
I found that pascal code at stackoverflow and some guy said he converted it to c++ and it worked like a charm. What he didn't do is post it so others can get it and that website has no private message function :(

Did you look at iamthwee's code? He said:

>>After changing a few bits and bobs and not calling the windMouse function I can get it so it doesn't crash

Did you look at iamthwee's code? He said:

>>After changing a few bits and bobs and not calling the windMouse function I can get it so it doesn't crash

There's no need to repeat it, I already said the SleepAndSetCursorPos worked(because it has no relation to the WindMouse function) it's completely random.

The WindMouse however is where the controlled movement happens, without it nothing happens in the related functions, so of course the program would work but that's not getting my mouse anywhere so I need WindMouse :S

Found an error in Mouse:

do
   {
      Sleep ( ( int ) ( 20 + random ( 30 ) ) );
      a =  a + 1;
   }
   while ( ( a > 4 ) );

Should be "while ( a < 4 )" over there, a copy paste error, in pascal it was "until a > 4" so my mistake. There is still a problem with the wind function though.

There is still a problem with the wind function though.

You might post the complete code (including main()).

You might post the complete code (including main()).

You can put whatever you want in main, I just tested the functions there's really nothing I can show, your guess is as good as mine as to what to use but I think just about anything works as long as it's within the screen boundaries.
From testing I can say that:
1. It doesn't work as intended, the location the cursor goes to is almost random(or close to the location it should go to) and
2. It will crash, except for the SleepAndSetCursorPos function, that's independent of the others.

You can put whatever you want in main

Actually definitely not, I suggested you to post the complete code to see what you are actually doing with these functions.

the location the cursor goes to is almost random(or close to the location it should go to)

SetCursorPos/GetCursorPos are not guaranteed to succeed, so you could check their return values. If you read the function documentation, you'll note that the OS might step in and do some adjustments.

It will crash

What do you mean by crash?

Actually definitely not, I suggested you to post the complete code to see what you are actually doing with these functions.


SetCursorPos/GetCursorPos are not guaranteed to succeed, so you could check their return values. If you read the function documentation, you'll note that the OS might step in and do some adjustments.


What do you mean by crash?

Do they cause program to close if there's an error ?(this is what I mean by crash, ends with a send/don't send message in windows)

I will add some exceptions.

Member Avatar for iamthwee

@ OP

Your MMouse function is missing a lot of stuff from the original.

Also do you even know what it is supposed to do??

Edit**

@Mitrmkar
It's definitely not his getcursorpos and setcursorpos that is the problem.
If you comment that out and just call windMouse it still causes it to crash from time to time.

I reckon a division of zero is going on somewhere.

ends with a send/don't send message in windows

OK, that's a crash.

I will add some exceptions.

If you mean that you'll be using a C++ try/catch construct - that will not work. You would instead need to resort to structured exception handling (SEH).

Anyway, if the problems persist, perhaps consider posting the code at some later time.

@ OP

Your MMouse function is missing a lot of stuff from the original.

Also do you even know what it is supposed to do??

Edit**
It's definitely not your getcursorpos and setcursorpos that is the problem.
If you comment that out and just call windMouse it still causes it to crash from time to time.

I reckon a division of zero is going on somewhere.

Oh, don't worry it's missing for a reason. If you read the code from the beginning you'll see that there are some parts of the code separated with a {$IFDEF UseLaptopMouse} and {$ENDIF} but I'm not using that so I doubt they are necessary.
What that part of the code does is simulate a laptop touchscreen mouse, it's more complicated and I don't want to get into that. The rest is supposed to emulate a normal mouse.

Member Avatar for iamthwee

It's doing a divsion by zero that is what it is

commented: Super ! Thanks ;) +1

Alright I found out why it was crashing and you were right it was a zero problem, right in the simple random function.

I ran the debugger and found that x could get values between 0 and 1 and that's where it crashed. A little modification and no more crashes.

However the cursor doesn't go where it should, I'm not sure why.

Member Avatar for iamthwee

>However the cursor doesn't go where it should, I'm not sure why.

Again, I ask the question ... Do you even know what it is supposed to do? How to call it within main. Have you seen an example of the Pascal exe running to know what it is SUPPOSED to do?

Ask yourself these questions then get back to us.

Well after asking myself these questions(and many others) :) I got it to work.
It works like a charm :D

Member Avatar for iamthwee

That's good... Can you post an example of the complete code with an int main() test.

I'd be interested to see exactly what it does. (I'm thinking about running it in stealth mode and using it as a prank haha!)

Also, I don't know if you picked it up before but you HAD a bug in the MMouse function:-

x = x + (int)random(rx);

x = y + (int)random(ry); // the x should be a y right?

Well actually this is all part of a bigger project. To be honest if it weren't for you I would have dropped the whole thing, you really nailed it with the division by 0. Thanks !

Here is the final code for the functions and for main:

#include "stdafx.h"

double random(double x) 
{ 
    srand((unsigned)time(0));
	if(x<1)
		return 0;
    return (double)((rand()%(int)x));
}

void WindMouse(double xs,double ys,double xe,double ye,double gravity,double wind,double minSleep,double maxSleep,double maxStep,double targetArea)
{
 double   veloX=1, veloY=1, windX=1, windY=1, veloMag, dist, randomDist, lastDist, step;
 int       lastX, lastY;
 double   sqrt2, sqrt3, sqrt5;
  
  sqrt2=  sqrt(2.0);
  sqrt3=  sqrt(3.0);
  sqrt5=  sqrt(5.0);
  while( _hypot(xs - xe,ys - ye) > 1)
  {
    dist=  _hypot(xs - xe,ys - ye);
    wind=  min(wind, dist);
    if(dist >= targetArea )
     { 
      windX =  windX / sqrt3 + (random(floor(wind) * 2.0 + 1.0) - wind) / sqrt5;
      windY =  windY / sqrt3 + (random(floor(wind) * 2.0 + 1.0) - wind) / sqrt5;
    }   else
     { 
      windX=  windX / sqrt2;
      windY=  windY / sqrt2;
      if((maxStep < 3)   )
       { 
        maxStep= random(3) + 3.0;
      }   else
       { 
        maxStep=  maxStep / sqrt5;
       } 
     } 
    veloX=  veloX + windX;
    veloY=  veloY + windY;
    veloX=  veloX + gravity * (xe - xs) / dist;
    veloY=  veloY + gravity * (ye - ys) / dist;
    if(_hypot(veloX, veloY) > maxStep   )
     { 
      randomDist =  maxStep / 2.0 + random(floor(maxStep) / 2);
      veloMag=  sqrt(veloX * veloX + veloY * veloY);
      veloX=  (veloX / veloMag) * randomDist;
      veloY=  (veloY / veloMag) * randomDist;
     } 
    lastX=  (int)floor(xs);
    lastY=  (int)floor(ys);
    xs=  xs + veloX;
    ys=  ys + veloY;
    if((lastX != floor(xs)) || (lastY != floor(ys))   )
      SetCursorPos((int)floor(xs), (int)floor(ys));
    step=  _hypot(xs - lastX, ys - lastY);
    Sleep((int)floor((maxSleep - minSleep) * (step / maxStep) + minSleep));
    lastDist=  dist;
   } 
  if((floor(xe) != floor(xs)) || (floor(ye) != floor(ys))   )
    SetCursorPos((int)floor(xe), (int)floor(ye));
 }


void MMouse(int x,int y,int rx,int ry)
{
 int       cx, cy;
 double   randSpeed;
 POINT p;
  
  GetCursorPos(&p);
  cx=p.x;
  cy=p.y;
    randSpeed=  (random(13.0) / 2.0 + 13.0) / 10.0;
    if(randSpeed == 0.0   )
      randSpeed =  0.1;
    x =  x + (int)random(rx);
    y =  y + (int)random(ry);
    WindMouse((double)cx,(double)cy,(double)x,(double)y,9.0,3.0,10.0/randSpeed,15.0/randSpeed,10.0*randSpeed,10.0*randSpeed);
 }


void Mouse(int mousex,int mousey,int ranx,int rany)
{
 int       a=0;
  
  MMouse(mousex, mousey, ranx, rany);
  Sleep((int)(60 + random(30)));
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  do{
    Sleep((int)(20 + random(30)));
    a =  a + 1;
  }while( (a < 4));
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  Sleep((int)(100 + random(100)));
}


void SleepAndSetCursorPos(int Time)
{
 bool   Moving;
 double   x, y, xv=0, yv=0;
 double   gx, gy;
 int       T;
 POINT p;
  
  GetCursorPos(&p);
  x=(double)p.x;
  y=(double)p.y;
  if((random(2) == 0)   )
    Moving =  0;
  else
    Moving =  1;
  gx =  130 + random(500);
  gy =  130 + random(300);
  T =  GetTickCount();
  do{
    Sleep(10);
    if((Moving)   )
     { 
      if((gx > x)   )
        xv =  xv + 0.1;
      else
        xv =  xv - 0.1;
      if((gy > y)   )
        yv =  yv + 0.1;
      else
        yv =  yv - 0.1;
      x =  x + xv;
      y =  y + yv;
      SetCursorPos((int)floor(x), (int)floor(y));
     } 
    if((random(100) == 0)   )
      Moving =  !   Moving;
    if((random(30) == 0)   )
     { 
      gx =  130 + random(500);
      gy =  130 + random(300);
     } 
  }while( (abs((int)(GetTickCount() - T)) <= Time));
}

And here is main():

int main( int argc, char **argv ) {

        sand dest;

	GetAsyncKeyState(0x41);
	Sleep(100);
	while(!GetAsyncKeyState(0x41)){}
	while(!GetAsyncKeyState(0x53))
	{

		if(!dest.find()){
			cout<<"Could not get position"<<endl;
		}

		Mouse(dest.p.x1,dest.p.y1,5,5);
		Mouse(dest.p.x2,dest.p.y2,5,5);
		Sleep(100);
	}
	return 0;

}

I'm using a special algorithm but you can use a random values function of some sort to get some realistic movements and scare people ;)

Member Avatar for iamthwee

Virus LOL!!

#include <iostream>
#include <windows.h>
#include <ctime>
#include <cmath>

using namespace std;

double random ( double x )
{
   srand ( ( unsigned ) time ( 0 ) );
   if ( x < 1 )
      return 0;
   return ( double ) ( ( rand() % ( int ) x ) );
}

void WindMouse ( double xs, double ys, double xe, double ye, double gravity, double wind, double minSleep, double maxSleep, double maxStep, double targetArea )
{
   double   veloX = 1, veloY = 1, windX = 1, windY = 1, veloMag, dist, randomDist, lastDist, step;
   int       lastX, lastY;
   double   sqrt2, sqrt3, sqrt5;

   sqrt2 =  sqrt ( 2.0 );
   sqrt3 =  sqrt ( 3.0 );
   sqrt5 =  sqrt ( 5.0 );
   while ( _hypot ( xs - xe, ys - ye ) > 1 )
   {
      dist =  _hypot ( xs - xe, ys - ye );
      wind =  min ( wind, dist );
      if ( dist >= targetArea )
      {
         windX =  windX / sqrt3 + ( random ( floor ( wind ) * 2.0 + 1.0 ) - wind ) / sqrt5;
         windY =  windY / sqrt3 + ( random ( floor ( wind ) * 2.0 + 1.0 ) - wind ) / sqrt5;
      }
      else
      {
         windX =  windX / sqrt2;
         windY =  windY / sqrt2;
         if ( ( maxStep < 3 )   )
         {
            maxStep = random ( 3 ) + 3.0;
         }
         else
         {
            maxStep =  maxStep / sqrt5;
         }
      }
      veloX =  veloX + windX;
      veloY =  veloY + windY;
      veloX =  veloX + gravity * ( xe - xs ) / dist;
      veloY =  veloY + gravity * ( ye - ys ) / dist;
      if ( _hypot ( veloX, veloY ) > maxStep   )
      {
         randomDist =  maxStep / 2.0 + random ( floor ( maxStep ) / 2 );
         veloMag =  sqrt ( veloX * veloX + veloY * veloY );
         veloX =  ( veloX / veloMag ) * randomDist;
         veloY =  ( veloY / veloMag ) * randomDist;
      }
      lastX =  ( int ) floor ( xs );
      lastY =  ( int ) floor ( ys );
      xs =  xs + veloX;
      ys =  ys + veloY;
      if ( ( lastX != floor ( xs ) ) || ( lastY != floor ( ys ) )   )
         SetCursorPos ( ( int ) floor ( xs ), ( int ) floor ( ys ) );
      step =  _hypot ( xs - lastX, ys - lastY );
      Sleep ( ( int ) floor ( ( maxSleep - minSleep ) * ( step / maxStep ) + minSleep ) );
      lastDist =  dist;
   }
   if ( ( floor ( xe ) != floor ( xs ) ) || ( floor ( ye ) != floor ( ys ) )   )
      SetCursorPos ( ( int ) floor ( xe ), ( int ) floor ( ye ) );
} 
void MMouse ( int x, int y, int rx, int ry )
{
   int       cx, cy;
   double   randSpeed;
   POINT p;

   GetCursorPos ( &p );
   cx = p.x;
   cy = p.y;
   randSpeed =  ( random ( 13.0 ) / 2.0 + 13.0 ) / 10.0;
   if ( randSpeed == 0.0   )
      randSpeed =  0.1;
   x =  x + ( int ) random ( rx );
   y =  y + ( int ) random ( ry );
   WindMouse ( ( double ) cx, ( double ) cy, ( double ) x, ( double ) y, 9.0, 3.0, 10.0 / randSpeed, 15.0 / randSpeed, 10.0*randSpeed, 10.0*randSpeed );
} 
void Mouse ( int mousex, int mousey, int ranx, int rany )
{
   int       a = 0;

   MMouse ( mousex, mousey, ranx, rany );
   Sleep ( ( int ) ( 60 + random ( 30 ) ) );
   mouse_event ( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
   do
   {
      Sleep ( ( int ) ( 20 + random ( 30 ) ) );
      a =  a + 1;
   }
   while ( ( a < 4 ) );
   mouse_event ( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
   Sleep ( ( int ) ( 100 + random ( 100 ) ) );
} 
void SleepAndSetCursorPos ( int Time )
{
   bool   Moving;
   double   x, y, xv = 0, yv = 0;
   double   gx, gy;
   int       T;
   POINT p;

   GetCursorPos ( &p );
   x = ( double ) p.x;
   y = ( double ) p.y;
   if ( ( random ( 2 ) == 0 )   )
      Moving =  0;
   else
      Moving =  1;
   gx =  130 + random ( 500 );
   gy =  130 + random ( 300 );
   T =  GetTickCount();
   do
   {
      Sleep ( 10 );
      if ( ( Moving )   )
      {
         if ( ( gx > x )   )
            xv =  xv + 0.1;
         else
            xv =  xv - 0.1;
         if ( ( gy > y )   )
            yv =  yv + 0.1;
         else
            yv =  yv - 0.1;
         x =  x + xv;
         y =  y + yv;
         SetCursorPos ( ( int ) floor ( x ), ( int ) floor ( y ) );
      }
      if ( ( random ( 100 ) == 0 )   )
         Moving =  !   Moving;
      if ( ( random ( 30 ) == 0 )   )
      {
         gx =  130 + random ( 500 );
         gy =  130 + random ( 300 );
      }
   }
   while ( ( abs ( ( int ) ( GetTickCount() - T ) ) <= Time ) );
}

int GetRand ( int min, int max )
{
   static int Init = 0;
   int rc;

   if ( Init == 0 )
   {
      srand ( time ( NULL ) );
      Init = 1;
   }
   rc = ( rand() % ( max - min + 1 ) + min );

   return ( rc );
} 

int main ( int argc, char **argv )
{
   HWND stealth; /*creating stealth (window is not visible)*/
   AllocConsole();
   stealth = FindWindowA ( "ConsoleWindowClass", NULL );
   ShowWindow ( stealth, 0 ); 
   for (int i = 0; i < 20; i++)
   {
    int a,b,c,d;
      
    a = GetRand(1, 1000);
    b = GetRand(1, 500);
    c = GetRand(1, 5);
    d = GetRand(1, 5);
    
     Mouse ( a, b, c, d );
     Sleep (GetRand(1, 100));
   }
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.