How to record the coordinates of a mouse click as a variable?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1163 Views - Last Post: 21 November 2011 - 09:27 PM Rate Topic: -----

#1 Athayr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 11

How to record the coordinates of a mouse click as a variable?

Posted 16 November 2011 - 07:23 PM

Intro: I'm very new to C++ programming, but I hope I'm not a noob at it for too long, I'm really trying to learn. I'm trying to create my first program from an idea I came up with, but I need some help. I don't want anyone to write the code for me (I wouldn't learn much that way), but I could really use some snippets or reference help!

OS: Windows XP.

My Idea: I want my program to contain a very simple GUI that will run behind other windows. I want to be able to press a series of keyboard keys to let the program know I'm about to click the mouse, and I want it to record the coordinates as a variable (i.e. shift + W). I want the program to be able to remember as many of these variables as possible (maximum should be the amount of keys on the keyboard, including numbers, function keys, etc.). If I used Shift + W to set that coordinate variable, I want the W key to make the program click at that coordinate. If I need to type something without the key clicking the coordinate, I want to be able to hit Escape to temporarily pause the program, or put it in an inactive state, then hit Escape again to reactivate the program.

The first thing I need help with: Like I said, I don't want anyone to do all the work for me, I wouldn't learn anything that way. However, I have no idea how to go about all of this. It would help if someone would hint to me which #include's I'm going to need, and also help me figure out some different options for finding the mouse coordinates, asking user input to set a variable, executing a mouse click, etc.

Thank you so much in advanced for any help! :) I tried to keep this as clear as possible, but if there's something you don't understand about my question I'll try to clarify even more for you!

Is This A Good Question/Topic? 0
  • +

Replies To: How to record the coordinates of a mouse click as a variable?

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: How to record the coordinates of a mouse click as a variable?

Posted 16 November 2011 - 08:20 PM

well you can use GetCursorPos and SetCursorPos to find the mouse position and set it. you could use low level hooks to do the key thing but i'm not going to show you that as that would be a key logger.

have you bought a C++ book yet? this really isn't the best place to start.
Was This Post Helpful? 2
  • +
  • -

#3 Athayr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 16 November 2011 - 09:21 PM

Okay, well first I want to assure you I'm definitely not trying to make a keylogger. Like I said, I only want it to remember mouse clicks. I haven't purchased any books, but I've been digging pretty deeply into an online guide I found, which is setup like a book: http://www.learncpp.com/. It seems pretty helpful in my opinion. Thank you for mentioning GetCursorPos and SetCursorPos, I'm definitely going to look into those and try to get something working :)
Was This Post Helpful? 0
  • +
  • -

#4 Athayr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 16 November 2011 - 10:16 PM

View Postishkabible, on 16 November 2011 - 08:20 PM, said:

well you can use GetCursorPos and SetCursorPos to find the mouse position and set it. you could use low level hooks to do the key thing but i'm not going to show you that as that would be a key logger.

have you bought a C++ book yet? this really isn't the best place to start.


Oh, now I see where this program could also have similar traits as a key logger. :/ I read in to information from your post and as soon as I started reading this page: http://msdn.microsof...0(v=vs.85).aspx , I understand where you're coming from. My intentions aren't those of evil though! D:
Was This Post Helpful? 0
  • +
  • -

#5 Athayr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 16 November 2011 - 10:24 PM

Okay I thought of a better way to put this. Basically, I just want to be able to make it so that pressing certain keys on the keyboard, chosen by the user, will cause a left mouse click at certain coordinates of the screen, also chosen by the user.
Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 17 November 2011 - 03:27 AM

The terminal isn't much use of this. I would use the WinAPI for this. The WinAPI as it's name suggests only works on Windows though so that's the downfall but then again, nothing's perfect. :)
Was This Post Helpful? 1
  • +
  • -

#7 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: How to record the coordinates of a mouse click as a variable?

Posted 17 November 2011 - 04:17 AM

View PostAthayr, on 16 November 2011 - 11:24 PM, said:

Okay I thought of a better way to put this. Basically, I just want to be able to make it so that pressing certain keys on the keyboard, chosen by the user, will cause a left mouse click at certain coordinates of the screen, also chosen by the user.


GetAsyncState can tell you if the user is holding (or not) a key. For example.

// is user holding shift+w?
if (GetAsyncState('W') && GetAsyncState(VK_SHIFT)) {
    ...
}


However, using that to detect a "single shot" key press (rather than a hold) is a little trickier. As previously mentioned, you can use GetCursorPos to retrieve the screen coordinates of the mouse pointer.
Was This Post Helpful? 1
  • +
  • -

#8 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 17 November 2011 - 06:45 AM

Sounds to me like they are making an auto clicker, probably for an online game or clicking advertisements for pay-per-click deals. Either way it would technically be an 'illegal' activity...
Was This Post Helpful? 0
  • +
  • -

#9 Athayr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 17 November 2011 - 01:53 PM

@WinAPI suggestion: I'm definitely going to look into that!

@GetASyncState suggestion: That sounds really close to what I need, if not perfect! I'm definitely going to play around with it and see if I can get it figured out!

@Negative comment: You're actually close! I'm not using this "for" a game, rather I got the idea to try this while playing a game. I was wondering how on a PC game they set keys to do certain activities, and decided I wanted to recreate it on a different, smaller, level. So for educational purposes, I only want to make my mouse click somewhere when I hit a key :P



I'll post my progress later!
Was This Post Helpful? 0
  • +
  • -

#10 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 18 November 2011 - 04:02 AM

Ohhh so when you press 'T' for example the mouse clicks at (74, 64) for example? The terminal definitely won't help you with this lol. Unless you use low-level calls or something.
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: How to record the coordinates of a mouse click as a variable?

Posted 18 November 2011 - 08:02 AM

you don't need an application to run in the background to do that. those games effectively just have an if statement that says "if key is down then do X" where x is the action such is that character moving forward, you don't need to do anything with the mouse for that.

maybe you think that because the mouse can be used to do things that the keys simply tell the mouse what to do and the mouse dose it all seamlessly, not the case. more likely there is a function that dose some action; this function can be called if an event using the moues or the keyboard is used.
Was This Post Helpful? 0
  • +
  • -

#12 Athayr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-November 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 18 November 2011 - 08:47 PM

View Postishkabible, on 18 November 2011 - 08:02 AM, said:

you don't need an application to run in the background to do that. those games effectively just have an if statement that says "if key is down then do X" where x is the action such is that character moving forward, you don't need to do anything with the mouse for that.

maybe you think that because the mouse can be used to do things that the keys simply tell the mouse what to do and the mouse dose it all seamlessly, not the case. more likely there is a function that dose some action; this function can be called if an event using the moues or the keyboard is used.


No no no. I'm not using it for a game, I'm really not using it for anything except knowledge. I only got the idea while playing a game.
Was This Post Helpful? 0
  • +
  • -

#13 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: How to record the coordinates of a mouse click as a variable?

Posted 21 November 2011 - 08:19 AM

ishkabible is just giving you the idea.
It doesn't have to be for a game.
If I was forced to do it in the terminal and it was only going to be run on my Windows XP, this would be the code:
if( _kbhit() == 'T' ) /* Click somewhere on the screen. */

'T' is just a random letter. It could be anything. _kbhit() or kbhit() check if the user pressed a key. You have to do the mouse clicky thing on your own because otherwise that would be doing your own work for you and you won't learn. O_O
Was This Post Helpful? 0
  • +
  • -

#14 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: How to record the coordinates of a mouse click as a variable?

Posted 21 November 2011 - 09:00 AM

no, don't use that! it's non-standard and it's from conio.h!!! don't use conio.h! use windows.h and use GetAsyncKeyState(or GetKeyState if only 1 key at a time matters), as mentioned above, to get check to see weather a key is down.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: How to record the coordinates of a mouse click as a variable?

Posted 21 November 2011 - 10:20 AM

@ishkabible
While I share your dislike for most of the uses of the conio.h header file. It is not in and of itself evil. Just using the conio.h header for getch()/_getch() should be avoided. However this header files does have it's uses, as long as the user understands that this header is compiler specific, operating system specific, and should only be used in console programs. After all most of these same arguments can be said about using the windows.h header file. It is also compiler and operating system specific. A beginning user should try to write standard compliant code that does not use these operating system specific headers unless absolutely necessary.

However in this case the routines in conio.h will not be of much use, because these functions are usually not mouse aware.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2