Pass multiple keyboard and mouse event in a array to sendinput

Hi all:
I have this, and I am trying to combine it altogether and send the array of INPUT to Sendinput, but I don't know how!

How do I do it?

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 = 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));

return;
}
Last edited on
this is my latest code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
}



But I get this error message:
argument of type "INPUT (*)[3]" is incompatible with parameter of type "LPINPUT"

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

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx

Thus, SendInput(3, input, sizeof(input));
I got a weird problem:

this code, although it looks very similar, it crashes lol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;

void main()
{
	Sleep(4000);

	LPINPUT input;

	input[0].type = INPUT_MOUSE;
	input[0].mi.dx = 0;
	input[0].mi.dy = 0;
	input[0].mi.mouseData = 0;
	input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
	input[0].mi.time = 0;
	input[0].mi.dwExtraInfo = GetMessageExtraInfo();


	input[1].type = INPUT_MOUSE;
	input[1].mi.dx = 0;
	input[1].mi.dy = 0;
	input[1].mi.mouseData = 0;
	input[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
	input[1].mi.time = 0;
	input[1].mi.dwExtraInfo = GetMessageExtraInfo();

	SendInput(2, input, sizeof(input));

	cout << "done" << endl;
	Sleep(1000);
	return;
}



I think maybe input[1] isn't the way to access this array LPINPUT?

thx
Last edited on
I think maybe input[1] isn't the way to access this array LPINPUT?

Well it would be if you allocate some memory for your pointer or easier use a static array.
Also it is always a good idea to check the return values.

This worked for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;

int main()
{
  INPUT input[2];

  input[0].type = INPUT_MOUSE;
  input[0].mi.dx = 0;
  input[0].mi.dy = 0;
  input[0].mi.mouseData = 0;
  input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
  input[0].mi.time = 0;
  input[0].mi.dwExtraInfo = GetMessageExtraInfo();


  input[1].type = INPUT_MOUSE;
  input[1].mi.dx = 0;
  input[1].mi.dy = 0;
  input[1].mi.mouseData = 0;
  input[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
  input[1].mi.time = 0;
  input[1].mi.dwExtraInfo = GetMessageExtraInfo();

  UINT result = SendInput(2, input, sizeof(INPUT));
  if (result != 0)
    cout << result << " events inserted into the keyboard or mouse input stream\n\n";
  else
    cerr << "\aWindows error: " << GetLastError() << "\n\n";

  system("pause");
  return 0;
}
Topic archived. No new replies allowed.