Link to home
Start Free TrialLog in
Avatar of Michael C
Michael C

asked on

C++ mouse_event mouse look

I have the following code for c++

I am trying to make a "mouse look" function so I can use it to control a video game I play, now, the mouse clicks work fine. but I can't get holding down right mouse button and move mouse to look around to work. Did I miss something?

thx



#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;

void main()
{
	Sleep(3000);

	INPUT input;
	input.type = INPUT_MOUSE;
	input.mi.dx = 0;
	input.mi.dy = 0;
	input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
	input.mi.time = 0;
	SendInput(1, &input, sizeof(input));

	input.mi.dx = 10;
	input.mi.dy = 0;
	input.mi.dwFlags = MOUSEEVENTF_MOVE;
	input.mi.time = 0;
	SendInput(1, &input, sizeof(input));

	input.mi.dx = 0;
	input.mi.dy = 0;
	input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
	input.mi.time = 0;
	SendInput(1, &input, sizeof(input));
		
	return;
}

Open in new window

Avatar of Michael C
Michael C

ASKER

Is there a way to combine mouse movement with clicking? As in, use one line of this  
SendInput(1, &input, sizeof(input));

but with moving the mouse while clicking it or holding it down?

thx
Hmm, there must a tiny thing i got wrong here:

I think LPINPUT is the proper type! but it says the expression must include class type.


#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[0].mi.time = 0;
	// SendInput(1, &input, sizeof(input));
	
	Sleep(50);
	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;
	//SendInput(1, &input, sizeof(input));

	Sleep(50);
	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(1, &input, sizeof(input));

	SendInput(3, &input, sizeof(input));

	//Sleep(50);
	/*
	input.type = INPUT_MOUSE;
	input.mi.dx = 0;
	input.mi.dy = 0;
	input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
	input.mi.time = 0;
	SendInput(1, &input, sizeof(input));

	input.type = INPUT_MOUSE;
	input.mi.dx = 0;
	input.mi.dy = 0;
	input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
	input.mi.time = 10;
	input.mi.time = 0;
	SendInput(1, &input, sizeof(input));
	*/
	return;
}

Open in new window

Avatar of Subrat (C++ windows/Linux)
Alter the sequence of left mouse click

      input.type = INPUT_MOUSE;
      input.mi.dx = 0;
      input.mi.dy = 0;
      input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
      input.mi.time = 10;
      input.mi.time = 0;
      SendInput(1, &input, sizeof(input));

input.type = INPUT_MOUSE;
      input.mi.dx = 0;
      input.mi.dy = 0;
      input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
      input.mi.time = 0;
      SendInput(1, &input, sizeof(input));
Moreover in code small change is needed for right mouse button, as follows.


      input.mi.dx = 10; <= CHANGE FROM 0 to 10 as per you code shown in first place.
      input.mi.dy = 0;
      input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
      input.mi.time = 0;
      SendInput(1, &input, sizeof(input));
Sorry I should have clarified this:

Instead of sending 3 events of this in 3 separate sendinput() , I am trying to create an array of INPUT and use sendinput only once, as described here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
UINT WINAPI SendInput(
  _In_ UINT    nInputs,
  _In_ LPINPUT pInputs,
  _In_ int     cbSize
);

Type: LPINPUT
An array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream.

Open in new window



So, here is what I have:

#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[0].mi.time = 0;
	// SendInput(1, &input, sizeof(input));
	
	Sleep(50);
	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;
	//SendInput(1, &input, sizeof(input));

	Sleep(50);
	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(1, &input, sizeof(input));

	SendInput(3, &input, sizeof(input));


	return;
}

Open in new window


Yet, the compiler gave me this error message:

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

Open in new window



So I didn't formulate the array properly.
What's the right way to do it?
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
awesome!