CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2005
    Posts
    4,954

    Windows SDK User Interface: How can I emulate mouse events in an application?

    Q: How can I emulate mouse events in an application?

    A:
    • There are two API fucntions that you can use:






    • Which of the two API functions should I use?

      The 'mouse_event()' function has been superseded by 'SendInput()' on Window NT/2000/XP. Thus, on these operating systems you should use 'SendInput()' (unless you need to provide backward compatibility with Windows 98 etc.). This FAQ is based on 'SendInput()'.



    • Can I see some example on how to use 'SendInput()' to emulate a click with the left mouse button?


      Code:
      void LeftClick ( )
      {  
        INPUT    Input={0};
        // left down 
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
        ::SendInput(1,&Input,sizeof(INPUT));
      
        // left up
        ::ZeroMemory(&Input,sizeof(INPUT));
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
        ::SendInput(1,&Input,sizeof(INPUT));
      }

    • How to use the function?


      Code:
      LeftClick();
      The left click will be performed on the current position of the mouse cursor.



    • Can I see some example on how to use 'SendInput()'
      to emulate a click with the right mouse button?


      Code:
      void RightClick ( )
      {  
        INPUT    Input={0};
        // right down 
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_RIGHTDOWN;
        ::SendInput(1,&Input,sizeof(INPUT));
      
        // right up
        ::ZeroMemory(&Input,sizeof(INPUT));
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_RIGHTUP;
        ::SendInput(1,&Input,sizeof(INPUT));
      }

    • How to use the function?


      Code:
      RightClick();
      The right click will be performed on the current position of the mouse cursor.



    • Can I see some example on how to use 'SendInput()' for emulating mouse movement?


      Code:
      void MouseMove (int x, int y )
      {  
        double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
        double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
        double fx = x*(65535.0f/fScreenWidth);
        double fy = y*(65535.0f/fScreenHeight);
        INPUT  Input={0};
        Input.type      = INPUT_MOUSE;
        Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
        Input.mi.dx = fx;
        Input.mi.dy = fy;
        ::SendInput(1,&Input,sizeof(INPUT));
      }

    • How to use the function?


      Code:
      MouseMove(100,100);
      This call will move the mouse cursor to the position 100/100 on the screen.



    Thanks to cilu for helping writing this FAQ.



    Last edited by Andreas Masur; May 6th, 2006 at 10:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured