0

i have the following code. when i execute the code my mouse pointer move to 0 0 coordinate. i need to move the cursor to x1 y1 position. the value of x1 y1 is integer.

int x1,y1;
                for(int i=0; i<nomdef; i++)  
                {
                    if(defectArray[i].depth > 40 )
                    {
                        con=con+1;
                        if(con==1)
                        {
                            x1=(defectArray[i].depth_point)->x;
                y1=(defectArray[i].depth_point)->y;
                        }
                        cvLine(src, *(defectArray[i].start), *(defectArray[i].depth_point),CV_RGB(255,255,0),1, CV_AA, 0 );  
                        cvCircle( src, *(defectArray[i].depth_point), 5, CV_RGB(0,0,255), 2, 8,0);                              cvDrawContours(src,defects,CV_RGB(0,0,0),CV_RGB(255,0,0),-1,CV_FILLED,8);

                    }
                }system("xdotool mousemove x1 y1");

1 Answers1

0

This is a C++ program (not bash or any similar high level language). There is no variable invocation/replacement in C/C++ string constants.

Thus, the system call does what you wrote: calling "xdotool mousemove x1 y1" (without replacing x1 and y1 as you might expect).

Instead you have to format the string e.g. using std::string, std::ostringstream.

Add these includes to your file start:

#include <string>
#include <sstream>

Change the last line of your code to:

std::ostringstream ossCmd;
ossCmd << "xdotool mousemove " << x1 << ' ' << y1;
#if 1 // EXPLICIT:
std::string cmd = ossCmd.str();
system(cmd.c_str());
#else // COMBINED:
system(ossCmd.str().c_str());
#endif // 1

This should work.

Note:

The #if 1 thing may look strange but it is a usual way in C/C++ to have active and inactive code alternatives between which the developer may change as necessary.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56