2
我正在使用Visual Studio在C++中進行控制檯項目,其中鼠標正在執行所有輸入內容。 它在Windows 7上工作正常,但不在Windows 10上。控制檯鼠標輸入不起作用
我會告訴你一些代碼,這是一個非常簡單的版本,我的實際代碼。但這裏是相同的:工作Win7上,而不是贏1
#include <stdio.h>
#include <windows.h>
void MouseEventProc(MOUSE_EVENT_RECORD);
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
VOID KeyEventProc(KEY_EVENT_RECORD);
VOID GetInputEvents(VOID);
void gotoxy(int, int);
void MyErrorExit(char *s)
{
printf("Fatal: %s\n", s);
exit(1);
}
int main(void)
{
HANDLE hStdin;
DWORD cNumRead, fdwMode, fdwSaveOldMode, i;
INPUT_RECORD irInBuf[128];
// Get the standard input handle.
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
MyErrorExit("GetStdHandle");
// Save the current input mode, to be restored on exit.
if (!GetConsoleMode(hStdin, &fdwSaveOldMode))
MyErrorExit("GetConsoleMode");
// Enable the window and mouse input events.
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (!SetConsoleMode(hStdin, fdwMode))
MyErrorExit("SetConsoleMode");
// Loop to read and handle the input events.
while (1)
{
// Wait for the events.
if (!ReadConsoleInput(
hStdin, // input buffer handle
irInBuf, // buffer to read into
128, // size of read buffer
&cNumRead)) // number of records read
MyErrorExit("ReadConsoleInput");
// Dispatch the events to the appropriate handler.
for (i = 0; i < cNumRead; i++)
{
switch (irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input
KeyEventProc(irInBuf[i].Event.KeyEvent);
break;
case MOUSE_EVENT: // mouse input
MouseEventProc(irInBuf[i].Event.MouseEvent);
break;
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
ResizeEventProc(
irInBuf[i].Event.WindowBufferSizeEvent);
break;
case FOCUS_EVENT: // disregard focus events
case MENU_EVENT: // disregard menu events
break;
default:
MyErrorExit("unknown event type");
break;
}
}
}
return 0;
}
VOID MouseEventProc(MOUSE_EVENT_RECORD ir)
{
if (ir.dwEventFlags == MOUSE_MOVED)
gotoxy(ir.dwMousePosition.X, ir.dwMousePosition.Y);
}
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD dummy) { return; };
VOID KeyEventProc(KEY_EVENT_RECORD dummy) { return; };
VOID GetInputEvents(VOID) { return; };
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
由於Linux子系統的原因,Win10發生了很大的變化。您禁用了ENABLE_PROCESSED_INPUT,出於某種原因並不高興。 –
那麼我如何啓用它?是否有任何文件可以閱讀如何處理更改?我真的試圖解決它。但迄今爲止感謝! –
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT;現在它可以工作。 –