4
votes

I'm using the following library: http://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library?fid=1518257&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&fr=51#xx0xx

To help me work with low level mouse hooks in Windows 7. I create a timer to check the last time a mouse move event was fired, and if it's longer than a given time, I move the mouse to the top left corner of the screen using SetCursorPos(0,0)

Before moving the mouse, I took it's old coordinates, and saved them. So that when I receive the next MouseMove event I can replace the mouse in it's original location. However, upon called SetCursorPos(oldPos.x, oldPos.y), the mouse does not move.

I'm sure that the oldPos values are correct, however the cursor refuses to move. Could this be due to the library I'm using? Please help.

[DllImport("user32.dll", SetLastError = true)]
    public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out POINT lpPoint);
void mouseHook_MouseMove(object sender, MouseEventArgs e)
    {
        //If the mouse was not visible, move it back to it's original position
        if (!mouseVisible)
        {
            mouseVisible = true;

            SetCursorPos(cursorPosition.x, cursorPosition.y);
        }

        //Update the last moved time.
        lastMoved = DateTime.Now;
    }

private void hideMouse(object sender, EventArgs e)
    {
        if (mouseVisible && (DateTime.Now - lastMoved) > new TimeSpan(0, 0, 0, mouseControl.timeTrackBar.Value))
        {
            log.Debug("Hiding mouse.");

            //Store the current mouse position.
            GetCursorPos(out cursorPosition);

            //Hide the mouse.
            SetCursorPos(0, 0);
            log.Debug("Moving cursor to 0,0");

            mouseVisible = false;
        }
1
Try to post some code. For example, how you get mouse coordinates? - Adriano Repetti
Added code. :) I'm using the WinAPI for most things, so I can't really understand why it's not working, especially if it works to set the mouse to 0,0, and I've gone through debug and can see that cursorPosition.x, and cursorPosition.y are in fact the right values. IT just never gets moved. - DTI-Matt
My guess is. Maybe it will be set, but directly overwritten by another mouse move event, because you actually moved the mouse. This event is fired a lot, depending on the OS and the speed of the mouse. Can you try to display something else, like a dot instead and use the mouse position you expect to use as the position of this dot? And check the return value or the GetLastError. - dowhilefor
@dowhilefor Any idea how I can draw a system-wide dot? Or how I can cancel further mouse move events after this one? I looked for a "Handled" property, but MouseEventArgs has none. - DTI-Matt
If you need to cancel (or to change) mouse event you better to use the hook itself. The structure you get in the hook procedure can be modified to change the event other applications will receive. - Adriano Repetti

1 Answers

1
votes

My guess is that this is what happens:

  1. You move the mouse to 0,0 with SetCursor.
  2. That act of calling SetCursor generates a mouse move event from your hook.
  3. You respond to the mouse move by showing the cursor again and putting it back where it was before.