CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2000
    Posts
    16

    Key Press or Mouse Click/Movement

    Is there any command to move/click the mouse?
    And to simulate a key press?

    Any other coding methods would be appreciated also.

    E. Chabot


  2. #2
    Join Date
    Aug 2005
    Posts
    25

    Re: Key Press or Mouse Click/Movement

    BOOL SetCursorPos(x,y)
    to move the mouse.

    I don't know how to simulate a key press or click the mouse.

  3. #3
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Key Press or Mouse Click/Movement

    See SendInput in MSDN.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    53

    Re: Key Press or Mouse Click/Movement

    Hi,
    use SendMessage or PostMessage API.

    Regards
    MJV

  5. #5
    Join Date
    Aug 2005
    Posts
    25

    Re: Key Press or Mouse Click/Movement

    I figured out how to send mouse clicks and keyboard presses with SendInput():
    Code:
     //mouse input
    INPUT input;
    input.type=INPUT_MOUSE;
    MOUSEINPUT mouseInput;
    mouseInput.dx=0;
    mouseInput.dy=0;
    mouseInput.mouseData=NULL;
    mouseInput.dwFlags=MOUSEEVENTF_LEFTDOWN;
    mouseInput.time=0;
    mouseInput.dwExtraInfo=0;
    input.mi=mouseInput;
    if(!::SendInput(1,&input,sizeof(input)))
    {
    AfxMessageBox(_T("Error sending input."));
    }
    //(Unicode) keyboard input
    INPUT inputKey;
    inputKey.type=INPUT_KEYBOARD;
    KEYBDINPUT keyInput;
    keyInput.wVk=0;
    keyInput.wScan=_T('a');
    keyInput.dwFlags=KEYEVENTF_UNICODE;
    keyInput.time=0;
    keyInput.dwExtraInfo=0;
    inputKey.ki=keyInput;
    if(!::SendInput(1,&inputKey,sizeof(inputKey)))
    {
    AfxMessageBox(_T("Error sending input."));
    }
    but this code will only compile if you have (at least) these defined:
    Code:
     #define _WIN32_WINNT 0x0500 //define windows version to "Windows 2000" 
    #define WINVER 0x0500 //define windows version to "Windows 2000"
    Looks like we took about 5 years to respond!

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Key Press or Mouse Click/Movement

    Yep, SendInput is an answer.
    Quote Originally Posted by me earlier
    See SendInput in MSDN.
    How did you get to this post and answer it in the first place?
    I have not noticed OP date.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Key Press or Mouse Click/Movement

    Oh man.. This sounds like a message in a bottle arriving after eons

  8. #8
    Join Date
    Aug 2005
    Posts
    25

    Re: Key Press or Mouse Click/Movement

    I did a search for this question because I didn't know the answer. Then I decided to post because I knew the answer to one of the questions. Yeah, the bottle has finally been found!

    I know that you posted SendInput earlier JohnCz. I just think that it will be easier for someone who finds this post from a search engine to just copy and paste my code, rather than go to MSDN, learn everything about INPUT, MOUSEINPUT, and KEYBDINPUT and learn about all of their parameters and write all of that code. Next time I will write "As suggested by JohnCz, I've written the following code that employs SendInput..." Oops I looked at my post and sorry about the way I started off.

  9. #9
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Key Press or Mouse Click/Movement

    Quote Originally Posted by MartyP
    Next time I will write "As suggested by JohnCz, I've written the following code that employs SendInput..." Oops I looked at my post and sorry about the way I started off.
    I really do not care about that. I quoted it to show that I fully agree with your post.
    If I wrote a code as you did I would probably care more.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

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