CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: sendinput event

  1. #1
    Join Date
    Nov 2016
    Posts
    7

    sendinput event

    this is my code:

    I am trying to make it do a right mouse button down and drag and then at the end of the drag, let go of the right mouse button. For some reason it doesn't work! It's for my project to control a video game mouse look event. I am trying to make it turn right by so and so degrees.

    Did I do something wrong here?

    thx!


    Code:
    	INPUT input;
    
    	input.type = INPUT_MOUSE;
    	input.mi.dx = 0;
    	input.mi.dy = 0;
    	input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
    	input.mi.time = 10;
    	input.mi.time = 0;
    	SendInput(1, &input, sizeof(input));
    	
    	Sleep(50);
    	input.type = INPUT_MOUSE;
    	input.mi.dx = 100;
    	input.mi.dy = 0;
    	input.mi.dwFlags = MOUSEEVENTF_MOVE;
    	input.mi.time = 10;
    	SendInput(1, &input, sizeof(input));
    
    	Sleep(50);
    	input.type = INPUT_MOUSE;
    	input.mi.dx = 0;
    	input.mi.dy = 0;
    	input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    	input.mi.time = 0;
    	SendInput(1, &input, sizeof(input));

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: sendinput event

    Pass in an array of INPUT structs and make a single call to SendInput().

  3. #3
    Join Date
    Nov 2016
    Posts
    7

    Re: sendinput event

    I don't know how to do that, could you show me up please?

  4. #4
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: sendinput event

    Have you tried mouse_event instead ? Simulate click down then move pointer and simulate click up
    Code:
    					// Bring Window On Top
    					HWND window = FindWindow(NULL, "Window Title");
    					ShowWindow(window, SW_RESTORE);
    					SetForegroundWindow(window);
    
    					// Rect Location
    					RECT rect;
    					GetWindowRect(window, &rect);
    
    					// Set Cursor
    					SetCursorPos(rect.right - 50, rect.bottom - 100);
    
    					// Click once
    					mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    
    					// Set Cursor Second Location
    					SetCursorPos(rect.right - 100, rect.bottom - 200);
    
    					// Release click
    					mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    I had cases when sendinput failed but mouse_event didn't
    Last edited by eclessiastes; November 28th, 2016 at 07:28 PM.

  5. #5
    Join Date
    Nov 2016
    Posts
    7

    Re: sendinput event

    that works alright, if i give the 3 commands separately yes, but it's different from giving all 3 commands in one sendinput, i dont know why. That's why i am trying to figure it out

  6. #6
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: sendinput event

    probably because u use the same INPUT input. you could do INPUT input1 for one task, INPUT input2 for another etc. I had cases when SendInput didn't work for virtualized exe in different session id, since then i always use mouse_event
    Last edited by eclessiastes; November 28th, 2016 at 09:11 PM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: sendinput event

    When you pass the array of items, you need to set input.mi.time to an increasing value for each of the array entries.

  8. #8
    Join Date
    Nov 2016
    Posts
    7

    Re: sendinput event

    Quote Originally Posted by Arjay View Post
    When you pass the array of items, you need to set input.mi.time to an increasing value for each of the array entries.
    how long?

    This is what I have now, but it seems there is some problem with building the array. I get this error message:

    argument of type "INPUT (*)[3]" is incompatible with parameter of type "LPINPUT"


    Code:
    #include <iostream>
    #include <windows.h>
    #include <ctime>
    using namespace std;
    
    void main()
    {
    	Sleep(3000);
    
    	INPUT input[3];
    
    	input[0].type = INPUT_MOUSE;
    	input[0].mi.dx = 0;
    	input[0].mi.dy = 0;
    	input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
    	input[0].mi.time = 10;
    	
    	input[1].type = INPUT_MOUSE;
    	input[1].mi.dx = 100;
    	input[1].mi.dy = 0;
    	input[1].mi.dwFlags = MOUSEEVENTF_MOVE;
    	input[1].mi.time = 10;
    
    	input[2].type = INPUT_MOUSE;
    	input[2].mi.dx = 0;
    	input[2].mi.dy = 0;
    	input[2].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    	input[2].mi.time = 0;
    
    	SendInput(3, &input, sizeof(input));
    
    
    	return;
    }

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: sendinput event

    Try

    Code:
    	INPUT input[3] = {0};
    
    	input[0].type = INPUT_MOUSE;
    	input[0].mi.dx = 0;
    	input[0].mi.dy = 0;
    	input[1].mi.time = GetTickCount();
    	input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
    	
    	input[1].type = INPUT_MOUSE;
    	input[1].mi.dx = 100;
    	input[1].mi.dy = 0;
    	input[1].mi.time = GetTickCount();
    	input[1].mi.dwFlags = MOUSEEVENTF_MOVE;
    
    	input[2].type = INPUT_MOUSE;
    	input[2].mi.dx = 0;
    	input[2].mi.dy = 0;
    	input[1].mi.time = GetTickCount();
    	input[2].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    
    	SendInput(3, &input, sizeof(INPUT));
    }
    Also, search this forum for SendInput and bing or google for "SendInput Examples C++".

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