问题相关代码,请勿粘贴截图
#include<windows.h>
#include<cstdio>
class Event{
HANDLE hin,hout;
DWORD oldMode;
void ErrorExit (char* Error){
fprintf(stderr,"%s\n",Error);
SetConsoleMode(hin,oldMode);
Sleep(2000);
ExitProcess(0);
}
public:
Event(){
hin=GetStdHandle(STD_INPUT_HANDLE);
hout=GetStdHandle(STD_OUTPUT_HANDLE);
if(hin==INVALID_HANDLE_VALUE)this->ErrorExit("GetStdInputHandleError!");
if(hout==INVALID_HANDLE_VALUE)this->ErrorExit("GetStdOnputHandleError!");
if(!GetConsoleMode(hin,&oldMode))ErrorExit("GetConsoleModeError!");
if(!SetConsoleMode(hin,ENABLE_MOUSE_INPUT))ErrorExit("SetConsoleModeError!");
}
COORD GetMouseLeftPressPosition(){
INPUT_RECORD Buf[128];
DWORD inputSize;
while(1){
if(!ReadConsoleInput(hin,Buf,128,&inputSize))
ErrorExit("ReadConsoleInputError!");
for(int i=0;i<inputSize;i++)
if(Buf[i].EventType==MOUSE_EVENT&&!Buf[i].Event.MouseEvent.dwEventFlags&&Buf[i].Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED)
return Buf[i].Event.MouseEvent.dwMousePosition;
}
}
};
int main(){
Event e;
COORD pos;
while(1){
pos=e.GetMouseLeftPressPosition();
printf("x:%d y:%d",pos.X,pos.Y);
Sleep(1000);
system("cls");
}
}
运行结果
明明是循环,却只能有一次输出
我想要达到的结果
每点一次控制台,都输出鼠标点击的位置