DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   macro building program help (http://www.daniweb.com/forums/thread102650.html)

TheBeast32 Dec 27th, 2007 11:36 pm
macro building program help
 
Hi again, i'm making a macro building pogram. I have got mouse movement down, but now i have no idea how to do mouse clicks or keyboard input. Please help! I'm also using dev-c++ 4.9.9.2.
Here's my complete code for the program.


POINT MousePoint;
int X = 0;
int Y = 0;
int MaxTime = 0;
int Counter = 0;
int NumOfResults = 0;
int Xmax, Ymax = 0;
int InputNumStr = 0;
char Action = 'a';
int TempInt, InputInt = 0;
char FileName[256] = "AAAAAAAAAAAAAAAAAAA";
char TempStr[256] = "AAAAAAAAAAAAAAAAAAA";
BOOL SetX = TRUE, SetY = FALSE;

#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include "Macro.h"

using namespace std;
int main(int argc, char* argv[])
{
cout << "MacroBuilder V. 1.0" << endl;
cout << "\nEnter the name of the macro: ";
cin >> FileName;
strcat(FileName, ".txt");

std::ofstream SaveFile(FileName);
SaveFile.close();
cout << "Type 'r' to record, 'q' to quit, 't' to test, or 'p' to play";
cin >> Action;

while (Action != 'q')
{

switch(Action)
{
case 'r':
MaxTime = 0;
Counter = 0;
NumOfResults = 0;
cout << "Enter the max time of the macro in seconds: ";
cin >> MaxTime;
MaxTime = MaxTime * 10;
cout << "\nPress escape to stop recording\n";
system("pause");
system("cls");
while (MaxTime > Counter)
{
GetCursorPos(&MousePoint);
if (MousePoint.x < 10)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "000" << MousePoint.x;
SaveFile.close();
cout << "\nX: 000" << MousePoint.x;
}
else if (MousePoint.x < 100)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "00" << MousePoint.x;
SaveFile.close();
cout << "\nX: 00" << MousePoint.x;
}
else if (MousePoint.x < 1000)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "0" << MousePoint.x;
SaveFile.close();
cout << "\nX: 0" << MousePoint.x;
}
else if (MousePoint.x > 1000)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "" << MousePoint.x;
SaveFile.close();
cout << "\nX: " << MousePoint.x;
}

if (MousePoint.y < 10)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "000" << MousePoint.y;
SaveFile.close();
cout << "  Y: 000" << MousePoint.y;
}
else if (MousePoint.y < 100)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "00" << MousePoint.y;
SaveFile.close();
cout << "  Y: 00" << MousePoint.y;
}
else if (MousePoint.y < 1000)
{
std::ofstream SaveFile(FileName, ios::app);
SaveFile << "0" << MousePoint.y;
SaveFile.close();
cout << "  Y: 0" << MousePoint.y;
}

NumOfResults++;
Sleep(10);
Counter++;
}
cout << "\n\nDisplayed " << NumOfResults << " in " << (MaxTime / 10) << " seconds, "
<< "which is " << (NumOfResults) / (MaxTime / 10) << " results per second";
break;

case 't':
X = Y = 0;
cout << "Enter the maximum X value: ";
cin >> Xmax;
cout << "\nEnter the maximum Y value: ";
cin >> Ymax;
system("pause");
system("cls");

// Move the mouse
while (X < Xmax || Y < Ymax)
{
if (X < Xmax)
{
X++;
}

if (Y < Ymax)
{
Y++;
}

SetCursorPos(X,Y);

// Get the mouse point
GetCursorPos(&MousePoint);
cout << MousePoint.x << ", ";
cout << MousePoint.y << "  ";
Sleep(10);
}
break;

case 'q':
return 0;
break;

case 'p':
SaveFile.close();
std::ifstream ReadFile(FileName);
TempInt = InputInt = 0;
X = Y = 0;
char ch;
/*
cout << "\nRaw data:\n";
while (!ReadFile.eof())
{
ReadFile.get(ch[2]);
cout << ch[2];
}
ReadFile.close();
ReadFile.open(FileName, ios::in);*/

        while(!ReadFile.eof())
        { 
            Sleep(10);
        ReadFile.get(ch);
                TempInt++;
                switch (TempInt)
                {
               
                case 1:
                InputInt = atoi(&ch) * 1000;
                break;
               
                case 2:
                InputInt += atoi(&ch) * 100;
                break;
               
                case 3:
                InputInt += atoi(&ch) * 10;
                break;
               
                case 4:
                InputInt += atoi(&ch);
               
                TempInt = 0;
                if (SetX)
                {
                SetX = FALSE;
                SetY = TRUE;
                X = InputInt;
                cout << X << endl;
                }
                else if (SetY)
                {
                Y = InputInt;
                SetX = TRUE;
                SetY = FALSE;
                InputInt = 0;
                cout << Y << endl;
                SetCursorPos(X, Y);
                }
            }
            }
        ReadFile.close();
} // End switch

/*for (TempInt = 0; TempInt < 256; TempInt++)
{
FileName[TempInt] = NULL;
}
*/
cout << "\nType 'r' to record, 'q' to quit, 't' to test, or 'p' to play";
cin >> Action;
} // End While
return 0;
}

Oh, and if you want to play a macro back, you have to do it right after you record it. I'm going to fix that.

Ancient Dragon Dec 27th, 2007 11:49 pm
Re: macro building program help
 
Its nice that you used code tags but you really should have formatted that code better before posting it. No one but a big masochist is going to read through all that stuff.

Dave Sinkula Dec 28th, 2007 3:14 am
Re: macro building program help
 
Voila:
POINT MousePoint;
int X = 0;
int Y = 0;
int MaxTime = 0;
int Counter = 0;
int NumOfResults = 0;
int Xmax, Ymax = 0;
int InputNumStr = 0;
char Action = 'a';
int TempInt, InputInt = 0;
char FileName[256] = "AAAAAAAAAAAAAAAAAAA";
char TempStr[256] = "AAAAAAAAAAAAAAAAAAA";
BOOL SetX = TRUE, SetY = FALSE;

#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include "Macro.h"

using namespace std;
int main(int argc, char* argv[])
{
  cout << "MacroBuilder V. 1.0" << endl;
  cout << "\nEnter the name of the macro: ";
  cin >> FileName;
  strcat(FileName, ".txt");

  std::ofstream SaveFile(FileName);
  SaveFile.close();
  cout << "Type 'r' to record, 'q' to quit, 't' to test, or 'p' to play";
  cin >> Action;

  while ( Action != 'q' )
  {

      switch ( Action )
      {
      case 'r':
        MaxTime = 0;
        Counter = 0;
        NumOfResults = 0;
        cout << "Enter the max time of the macro in seconds: ";
        cin >> MaxTime;
        MaxTime = MaxTime * 10;
        cout << "\nPress escape to stop recording\n";
        system("pause");
        system("cls");
        while ( MaxTime > Counter )
        {
            GetCursorPos(&MousePoint);
            if ( MousePoint.x < 10 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "000" << MousePoint.x;
              SaveFile.close();
              cout << "\nX: 000" << MousePoint.x;
            }
            else if ( MousePoint.x < 100 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "00" << MousePoint.x;
              SaveFile.close();
              cout << "\nX: 00" << MousePoint.x;
            }
            else if ( MousePoint.x < 1000 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "0" << MousePoint.x;
              SaveFile.close();
              cout << "\nX: 0" << MousePoint.x;
            }
            else if ( MousePoint.x > 1000 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "" << MousePoint.x;
              SaveFile.close();
              cout << "\nX: " << MousePoint.x;
            }

            if ( MousePoint.y < 10 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "000" << MousePoint.y;
              SaveFile.close();
              cout << " Y: 000" << MousePoint.y;
            }
            else if ( MousePoint.y < 100 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "00" << MousePoint.y;
              SaveFile.close();
              cout << " Y: 00" << MousePoint.y;
            }
            else if ( MousePoint.y < 1000 )
            {
              std::ofstream SaveFile(FileName, ios::app);
              SaveFile << "0" << MousePoint.y;
              SaveFile.close();
              cout << " Y: 0" << MousePoint.y;
            }

            NumOfResults++;
            Sleep(10);
            Counter++;
        }
        cout << "\n\nDisplayed " << NumOfResults << " in " << (MaxTime / 10) << " seconds, "
        << "which is " << (NumOfResults) / (MaxTime / 10) << " results per second";
        break;

      case 't':
        X = Y = 0;
        cout << "Enter the maximum X value: ";
        cin >> Xmax;
        cout << "\nEnter the maximum Y value: ";
        cin >> Ymax;
        system("pause");
        system("cls");

// Move the mouse
        while ( X < Xmax || Y < Ymax )
        {
            if ( X < Xmax )
            {
              X++;
            }

            if ( Y < Ymax )
            {
              Y++;
            }

            SetCursorPos(X,Y);

// Get the mouse point
            GetCursorPos(&MousePoint);
            cout << MousePoint.x << ", ";
            cout << MousePoint.y << " ";
            Sleep(10);
        }
        break;

      case 'q':
        return 0;
        break;

      case 'p':
        SaveFile.close();
        std::ifstream ReadFile(FileName);
        TempInt = InputInt = 0;
        X = Y = 0;
        char ch;
/*
cout << "\nRaw data:\n";
while (!ReadFile.eof())
{
ReadFile.get(ch[2]);
cout << ch[2];
}
ReadFile.close();
ReadFile.open(FileName, ios::in);*/

        while ( !ReadFile.eof() )
        {
            Sleep(10);
            ReadFile.get(ch);
            TempInt++;
            switch ( TempInt )
            {
           
            case 1:
              InputInt = atoi(&ch) * 1000;
              break;

            case 2:
              InputInt += atoi(&ch) * 100;
              break;

            case 3:
              InputInt += atoi(&ch) * 10;
              break;

            case 4:
              InputInt += atoi(&ch);

              TempInt = 0;
              if ( SetX )
              {
                  SetX = FALSE;
                  SetY = TRUE;
                  X = InputInt;
                  cout << X << endl;
              }
              else if ( SetY )
              {
                  Y = InputInt;
                  SetX = TRUE;
                  SetY = FALSE;
                  InputInt = 0;
                  cout << Y << endl;
                  SetCursorPos(X, Y);
              }
            }
        }
        ReadFile.close();
      } // End switch

/*for (TempInt = 0; TempInt < 256; TempInt++)
{
FileName[TempInt] = NULL;
}
*/
      cout << "\nType 'r' to record, 'q' to quit, 't' to test, or 'p' to play";
      cin >> Action;
  } // End While
  return 0;
}
Behold the power of copy, paste, a decent code editor, copy, paste, and post.

iamthwee Dec 28th, 2007 10:20 am
Re: macro building program help
 
>but now i have no idea how to do mouse clicks or keyboard input.
Go the msdn website, it should be well documented.


All times are GMT -4. The time now is 7:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC