I've been teaching myself C++ for the last two weeks. One of the things that was difficult for me to find on google was using the left click on a certain coordinate on the screen. The goal is to eventually be able to recognize images on the screen and alter the path accordingly, but until I gain more experience, I'd like to focus on the previously stated problem. Any help would be appreciated. Thank you.

Recommended Answers

All 40 Replies

Member Avatar for iamthwee

I've been teaching myself C++ for the last two weeks. One of the things that was difficult for me to find on google was using the left click on a certain coordinate on the screen. The goal is to eventually be able to recognize images on the screen and alter the path accordingly, but until I gain more experience, I'd like to focus on the previously stated problem. Any help would be appreciated. Thank you.

Clicking, will depend on what operating system you are using.

If you're using windows check out the windows api from the msdn website.

The latter is more difficult. To recognise an image, that is to have your program intelligently match an image to another by itself, would require some sort of artificial intelligence programming.

windows api will return the x,y coordinates of the mouse. If you know the coordinates of the image's bounding rectangle then you just need to find out which image rectangle contains the mouse coordinates. Using MFC will simplify that, but you can also use windows RECT structure and write your own code to make the "hit test".

If you are not using MS-Windows, I have no clue how to do it with *nix.

All of the code I've seen for it has been incomplete pieces in Microsoft Visual C++. Is there a simpler way to just return the coordinates of the cursor or move the cursor to a coordinate using a format I am more familar with?

I don't think you'll be able to do this with 2 weeks of C++ experience. Of course I have no idea about how good you are at C++, but I think it's too advanced.

You should search www.msdn.com, there are several APIs that will handle mouse-things.

There is also something to record mouse movements, I don't really remember much of it.

codeproject has a mouse-macro recorder with source, you might want to check it.

Goodluck,
Eddy

The window's message pump should be checking for WM_MOUSEMOVE messages. This message will give you the cordinates of the mouse. If you are not at all familiar with win32 api functions, then a good place to start is with this tutorial.

I've spent the last couple of days trying to put together from bits I've found online with no success. I'm not if I failed because the code doesn't compile on Dev-C++ or if I just don't understand it. Is there any chance someone could put together the whole thing for me? Output a number with teh current cursor position or move the cursor to a coordinate. I can't imagine it taking up much more than 10-15 lines. :-/

>>I can't imagine it taking up much more than 10-15 lines

HaHa you really are new to this aren't you! run through the tutorial I posted previously to get down the basic window then add a little code to check for WM_MOUSEMOVE events. You will probably know how to do that once you have completed the tutorial.

I read through the tutorial. I'm sure this is nowhere near correct, but could you help me from here?

#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
int WINAPI WinMain()
{
LRESULT CALLBACK WndProc(WPARAM wParam, LPARAM lParam)
{
WM_MOUSEMOVE fwKeys = wParam; 
int xPos = LOWORD(lParam); 
int yPos = HIWORD(lParam);
cout<<xPos<<", "<<yPos;
system("pause");
}
}

If you really did go through that tutorial -- thoroughly I might add -- you should know that what you posted is not even close to being correct. Read it thorougly again, or again, or as many times as it takes for you to know it well. If you are not willing to put in the effort to do that, then nobody else is going to do it for you.

^^I know. That's why I said it's nowhere near correct. :P

I found a MUCH easier way to do this. GetCursorPos() and SetCursorPos(x,y). ;-) One question though: what do I define x and y as? I tried point, but that didn't work.

Member Avatar for iamthwee

Just wack in some numbers?

#include <windows.h>

int main(void)
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    return 0;
}

#include <windows.h>

int main(void)
{
    SetCursorPos(10, 20);
    return 0;
}

I'd like to be able to set the value for the variables. What would I identify the variables as?

>>I can't imagine it taking up much more than 10-15 lines

HaHa you really are new to this aren't you! run through the tutorial I posted previously to get down the basic window then add a little code to check for WM_MOUSEMOVE events. You will probably know how to do that once you have completed the tutorial.

I got 10 lines. ;)

#include <windows.h>
#include <iostream>
using namespace std;
int main(){
POINT c;
GetCursorPos(&c);
int x=c.x;
int y=c.y;
cout<<x<<", "<<y;
system("PAUSE");}
#include <windows.h>
#include <iostream>
int main(){
POINT c;
GetCursorPos(&c);
std::cout<<c.x<<", "<<c.y;
}

is enough...

7 lines

You were wrong with your estimation 10-15 lines!

Some compilers might not agree that it does not have a return value. I had to include StdAfx.h because I used visual c++.


Greetz, Eddy

that seems to work ok :) next trick is how to know when the cursor moves? or when the left mouse button is pressed? How will you know when to call GetCursorPos() as stated in your original post? A console program does not get windows events so it will not know when the cursor is moved over the bitmap, or between bitmaps or anywhere else on the window.

[edit]There is a whole series of console functions that might help you. Look for consol win events where you can set up an event handler for mouse events. I haven't tried it so you might have to do some experimentation with it.

Any chance anyone could give me a line or two that tells it to leftclick at its active location? I can't find this in C++. :-/

I got 10 lines. ;)

#include <windows.h>
#include <iostream>
using namespace std;
int main(){
POINT c;
GetCursorPos(&c);
int x=c.x;
int y=c.y;
cout<<x<<", "<<y;
system("PAUSE");}

Dont say things without reading the entire post submitted by a very experienced and senior member.

What he wanted to said that if u intended on drawing a window or creating a window u need atleast 60 - 70 lines of code which is well formatted.

What u have written above is not a GUI or window application but a console application.

Any chance anyone could give me a line or two that tells it to leftclick at its active location? I can't find this in C++. :-/

catch the WM_LBUTTONDOWN event in the WinProc event handler.

I'm looking for a single command that does it not a case to alert me when it happens. Do you know of one?

For example, if this was the mouse move, I would want SetPosition() not WM_MOUSEMOVE. It is hard for me to find the command that works in C++.

Member Avatar for iamthwee

Active location. You mean when the mouse moves over a coordinate on the screen, hit the left mouse click.

Here's the code for clicking...

#include <windows.h>

int main(void)
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    return 0;
}

Instead of searching for a coordinate try and define an square or rectangular area?

Most windows messages do NOT have a corresponding function. You are treading into areas which will force you to learn Windows programming whether you like it or not, or you could always just go watch a TV show :cheesy:

[edit]The code that Jamthwee just posted will move the mouse, it won't tell you when (or even if) the left button was clicked.[/edit]

Member Avatar for iamthwee

Most windows messages do NOT have a corresponding function. You are treading into areas which will force you to learn Windows programming whether you like it or not, or you could always just go watch a TV show :cheesy:

What's your favourite tv show at your retirement home? :cheesy:

The code that iamthwee just posted will move the mouse, it won't tell you when (or even if) the left button was clicked

Huh? It just clicks it?

What's your favourite tv show at your retirement home? :cheesy:

Lawrence Welk, from the 1950s:eek: Also Dr. Who from BBC that started almost 30 years ago but was revived about a year or so ago.

>>Huh? It just clicks it?
Yup, clicks it, not moves it.

Member Avatar for iamthwee

>>Huh? It just clicks it?
Yup, clicks it, not moves it.

Have they given you your sleepie pills a little earlier today? :cheesy:

I do not know how to make something run and continue running after reboot. What is this type of program called? What would I google?

Member Avatar for iamthwee

I do not know how to make something run and continue running after reboot. What is this type of program called? What would I google?

You mean start up on every reboot. And runs in the background...

You mean start up on every reboot. And runs in the background...

That would fit.

Should someone login for the program to run (example anti-virus program)? Or "is the computer running?" the only criteria (example Telnet Server)?

Can you explain the type of program you are trying to create?

Ok, here's an example. To shutdown a computer using windows you can press ctrl+alt, u, u. If I wanted my computer to shutdown everytime I pressed u,u,u, how would I go about making a program that runs automatically at the start and waits for key inputs or some other command. A decent keylogger would be another example of the type of application I would like to make.

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.