问题
the issue I'm having is that upon executing this code, it clicks, and then my monitor turns blue and says "HDMI no cable connected", and either returns back to normal after 1 second, or stays on that screen until I perform an action (click, alt etc).
Anybody with any idea on why this is happening would be appreciated.
#include "stdafx.h"
#include <Windows.h>
using namespace std;
void function() {
INPUT Input;
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(true, &Input, sizeof(Input));
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(true, &Input, sizeof(Input));
};
int main()
{
Sleep(3000);
function();
return 0;
}
Also, I'm using Visual Studio 2017 Community. This problem occurs when debugging the code, and when running the executable. There are no errors or warnings with my code.
回答1:
You are not initializing the INPUT structure properly thus (probably) invoking undefined behavior. Initialize the structure fields to zeros using:
INPUT Input = {};
You can also go with an array of two elements:
INPUT Input[2] = {0};
followed by a:
Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Input[1].type = INPUT_MOUSE;
Input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
and one call to SendInput:
SendInput(2, Input, sizeof(INPUT));
回答2:
You are not initializing most of INPUT
fields sending some garbage and potentially triggering Undefined Behavior. You should at least fill them with zeros:
Input.type = INPUT_MOUSE;
Input.mi.dx = 0;
Input.mi.dy = 0;
Input.mi.mouseData = 0;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Input.mi.time = 0;
Input.mi.dwExtraInfo = 0;
来源:https://stackoverflow.com/questions/46742660/c-execution-causes-monitor-to-disconnect