tags:

views:

1338

answers:

3

I'm currently writing a c++ console application that grabs the mouse position at regular intervals and sends it to another visual application where it is used to drive some 3d graphics in real time. The visual app is closed source and cannot be altered outside it's limited plug-in functionality.

Currently I'm using the GetCursorPos() function which is easy and fast enough, but I'm running into the issue that all of the data is clipped based on the current screen resolution of 1920x1600 so that all x values are between 0 and 1920 and all y values are between 0 and 1600 no matter how far the mouse is physically moved.

I need to get the mouse position before it's clipped at the edge of the screen, or possibly the deltas which I could use to calculate the current position.

I've seen some references to the windows MouseMove event but I would really not want to implement a window to make it work or especially have it as the active to receive those events.

I'm working in a windows environment and a language change is not feasible.

+2  A: 

Just get the position, and move it to the center and return the delta yourself

This is how FPS games do it

Pyrolistical
+4  A: 

I might be wrong, but in Win32 land you don't get mouse move messages when the mouse is at the edge of the screen because, well, the mouse isn't moving. The usual way to get an infinite mouse area is to do the following:

  1. Hide the mouse, get exclusive access and record position
  2. Centre mouse to window
  3. When mouse moves, get delta from centre of screen to current position
  4. Centre mouse to window again
  5. The next mouse move should have a delta of (0,0), so ignore it
  6. Go to 3 until end of mouse move operation
  7. Reset position, show the mouse and release exclusive access

If you didn't hide the mouse, then you'd see the mouse moving a small distance and then snapping back to the centre position, which looks nasty.

This method does require a message pump for the mouse move messages so the console application idea probably won't work with this. Can you create a full screen invisible window for grabbing the mouse?

Skizz
There's nothing that prevents a console program from having a message pump or from creating windows. But having a console itself would be rather pointless since it sounds like this program isn't supposed to have any text input or output.
Rob Kennedy
You can just use `GetCursorPos`/`SetCursorPos` in a loop (with `Sleep`s in between) to get the same effect. Not a seamless solution but will work probably.
Nick
+1  A: 

I don't have any direct experience with raw input, which is probably what you need to tap into. According to MSDN, you have to register the device, then setup your winproc to accept the WM_INPUT messages and then do your calculations based on the raw data.

Here's another relevant link.

amado
Back in the bad old days of DOS and serial mice, I'd just open the serial port to the mouse and receive the protocol for tasks like that. But the re-centering the mouse (other answers) is probably the better method.
Matthias Wandel