Thread: sendinput()

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    10

    sendinput()

    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:
    SendInput function (Windows)

    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;
    }


    But I am getting this error:

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

    How do I construct the array properly?

    thx!

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Code:
    SendInput(3, input, sizeof(INPUT));
    ?
    If that doesn't work, perhaps this will...
    Code:
    SendInput(3, (LPINPUT)input, sizeof(INPUT));

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    for some reason the screen goes dark when I run this, and I can't figure out what it is!
    Is it a screensaver?


    Code:
    #include <iostream>#include <windows.h>
    #include <ctime>
    using namespace std;
    
    
    void main()
    {
    	Sleep(1000);
    
    
    	INPUT input;
    
    
    	input.mi.dx = 1000;
    	input.mi.dy = 500;
    	input.mi.dwFlags = MOUSEEVENTF_MOVE;
    	input.type = INPUT_MOUSE;
    
    
    	SendInput(1, &input, sizeof(input));
    
    
    
    
    	cout << "done" << endl;
    
    
    	Sleep(1000);
    
    
    
    
    
    
    	return;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    MOUSEINPUT structure (Windows)
    The MOUSEINPUT structure contains more members than you're assigning.
    The manual makes it quite clear some of these fields need to be zero (or otherwise valid data), but you're leaving them with garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    I got mouse move to work but not mouse click

    Code:
    #include <iostream>#include <windows.h>
    #include <ctime>
    using namespace std;
    
    
    void main()
    {
    	Sleep(4000);
    
    
    	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();
    	
    	SendInput(2, input, sizeof(input));
    
    
    	cout << "done" << endl;
    	Sleep(1000);
    	return;
    }

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    Quote Originally Posted by Salem View Post
    MOUSEINPUT structure (Windows)
    The MOUSEINPUT structure contains more members than you're assigning.
    The manual makes it quite clear some of these fields need to be zero (or otherwise valid data), but you're leaving them with garbage.
    thanks

  7. #7
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    This code runs fine, but it's because I pass only one mouse event to sendinput? There is a & needed for &input to sendinput()

    Here, I passed only one INPUT structure to it, I think that's how it works when there is no array.

    Code:
    #include <iostream>#include <windows.h>
    #include <ctime>
    using namespace std;
    
    
    void main()
    {
        Sleep(4000);
    
    
        INPUT input;
    
    
        input.type = INPUT_MOUSE;
        input.mi.dx = 200;
        input.mi.dy = 0;
        input.mi.mouseData = 0;
        input.mi.dwFlags = MOUSEEVENTF_MOVE;
        input.mi.time = 0;
        input.mi.dwExtraInfo = GetMessageExtraInfo();
    
    
        SendInput(1, &input, sizeof(input));
    
    
        cout << "done" << endl;
        Sleep(1000);
        return;
    }


    However, when I declare a LPINPUT , this code compiles, but would crash when its run. The error message says input is used without being declared first. I have no clue how what that means.


    Code:
    #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;
    }
    All I am trying to do is to pack multiple mouse events into one sendinput call.

    thx all

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read the specification for the function again.
    > SendInput(2, input, sizeof(input));

    SendInput function (Windows)
    cbSize [in]

    Type: int

    The size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function fails.
    You should be using sizeof(INPUT), not sizeof(input).
    They're only the same value when you have only one element in your array.

    Also this
    Return value

    Type: UINT

    The function returns the number of events that it successfully inserted into the keyboard or mouse input stream. If the function returns zero, the input was already blocked by another thread. To get extended error information, call GetLastError.
    Don't blindly assume success.
    If something isn't working, your first debug action would be to inspect the return result to see if that sheds any light on the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    i see i see, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SendInput
    By Ducky in forum Windows Programming
    Replies: 12
    Last Post: 07-24-2011, 07:18 AM
  2. SendInput()
    By borre_mosch in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2004, 11:36 AM
  3. SendInput()?
    By src in forum Windows Programming
    Replies: 11
    Last Post: 02-21-2003, 07:43 PM
  4. tagINPUT and SendInput
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-08-2002, 03:08 PM

Tags for this Thread