C/C++ IsUserAFK

Posts 1–8 of 8 · Page 1 of 1
C/C++ IsUserAFK
Hey With this function you can check, if a user is afk. It´s only a simple project, you can change many thinks to make it better, f.E: the Sleep function. But i hope people will learn from it.

Code:
// coded by Inmate
#include <windows.h>
#include <iostream>

using namespace std;

bool CheckAFK(int delay, bool isAFK = false);

int main()
{

if (CheckAFK(1000)){
                    
    //Mache irgendwas...
  
}                  

getchar();

                         
}                   
      
bool CheckAFK(int delay, bool isAFK)
{
   if ( delay < 1000 ){
        
        MessageBoxA(NULL, "Bitte größere Delay Anzahl angeben." ,"Error", NULL);
        
        return 0;
   }


   int xPoint, yPoint, xPointNew, yPointNew;
   POINT position;
   
          GetCursorPos(&position);
      
          xPoint = position.x;
          yPoint = position.y;
         
          if ( xPoint == 0 || yPoint == 0 ){
        
          MessageBoxA(NULL, "Die Mauszeiger Position konnte nicht richtig gelesen werden." ,"Error", NULL);
        
          return 0;
          
          }
         
          //cout << "Y Alt: " << yPoint << "\n" << "X Alt: " << xPoint << "\n";
         
          Sleep(delay);
             
          GetCursorPos(&position);
              
          xPointNew = position.x;
          yPointNew = position.y;
         
          //cout << "Y Neu: " << yPointNew << "\n" << "X Neu: " << xPointNew << "\n";
        
          if ( xPointNew == 0 || yPointNew == 0 ){
        
          MessageBoxA(NULL, "Die Mauszeiger Position konnte nicht richtig gelesen werden." ,"Error", NULL);
        
          return 0;
          
          }
        
          if ( xPoint == xPointNew && yPoint == yPointNew){
                                   
          //cout << "User is afk\n";
              
          isAFK == true;   
              
          }else{
                  
          //cout << "User is not afk";  
          
          isAFK == false;  
                    
   }

}
You have 1 serious error:

Code:
if ( xPoint == 0 || yPoint == 0 ){
        
          MessageBoxA(NULL, "Die Mauszeiger Position konnte nicht richtig gelesen werden." ,"Error", NULL);
        
          return 0;
          
          }
If you look at the logic above, what would happen if the mouse by accident is on x = 0 or y = 0? Your program simply exits.

If you want to catch whether an error occured while fetching the mouse position with GetCursorPos, you can do something like this:

Code:
if(!GetCursorPos(position)){
//throw error/exit
}
Quote Originally Posted by inmate View Post
Hey With this function you can check, if a user is afk. It´s only a simple project, you can change many thinks to make it better, f.E: the Sleep function. But i hope people will learn from it.

Code:
// coded by Inmate
#include <windows.h>
#include <iostream>

using namespace std;

bool CheckAFK(int delay, bool isAFK = false);

int main()
{

if (CheckAFK(1000))
{
                    
    //Mache irgendwas...
  
}
how is it possible that it is called with only one parameter?????
Quote Originally Posted by Toxic Waltz View Post
how is it possible that it is called with only one parameter?????
Default parameters.

edit:

@ OP:
isAFK==false and isAFK==true won't do anything.
Quote Originally Posted by Hell_Demon View Post
isAFK==false and isAFK==true won't do anything.
I thought that it was usefull when user can things like this:

if (CheckAFK(1000), false){

//Mache irgendwas...
//Do anything
}

but you´re right, in the end effect it is useless :-/

Regards
What would be the point of such program anyways?
Quote Originally Posted by Kallisti View Post
What would be the point of such program anyways?
To check if somebody's afk
Quote Originally Posted by Kallisti View Post
What would be the point of such program anyways?
screensaver for example... auto shutdown... something...
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us