控制台鼠标输入不起作用 [英] Console Mouse Input not working
问题描述
我正在Visual Studio中使用c ++进行控制台项目,其中鼠标负责所有输入工作。
在Windows 7上可以正常工作,但在Windows 10上却不能。
I am working on a console project in c++ with visual studio, in which the mouse is doing all the input stuff. It is working fine on windows 7, but not on windows 10.
我将向您展示一些代码,这是我的一个非常简化的版本实际代码。但这是相同的:在win7上工作,而不在win 1上工作
I'll show you some code, which is a very simplefied version, of my actual code. But here is the same: working on win7, not on win 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);
}
推荐答案
我有类似问题 。而且问题似乎是由于在Windows10的控制台Windows属性的设置中启用了快速编辑模式 选项引起的。我已经复制并粘贴了您指定的MCVE代码,但在Windows10计算机上也无法正常运行。但是,它按照 Microsoft有关禁用快速编辑模式的文档。在启用窗口&之前,我首先做了(em>禁用 这件事(!)。鼠标输入事件,方法是通过以下方式在您的代码中包含/添加快速编辑模式-禁用代码行:
I had similar problem on my Windows-10 machine. And the problem seemingly was being caused by 'Quick Edit Mode' option being enabled in settings of 'Console Windows Properties' on Windows10. I have copied and pasted your given MCVE-code, it wasn't working on my windows10 machine either. But, it started working after i disabled 'Quick Edit Mode' as per Microsoft's docs about disabling 'Quick Edit Mode'. I did this 'disabling' thing first(!), before enabling the window & mouse input events, by including/adding the 'Quick Edit Mode'-disabling-code-lines to your code in following manner:
/*
Step-1:
Disable 'Quick Edit Mode' option
*/
fdwMode = ENABLE_EXTENDED_FLAGS;
if (! SetConsoleMode(hStdin, fdwMode) )
MyErrorExit("SetConsoleMode");
/*
Step-2:
Enable the window and mouse input events,
after you have already applied that 'ENABLE_EXTENDED_FLAGS'
to disable 'Quick Edit Mode'
*/
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if (!SetConsoleMode(hStdin, fdwMode))
MyErrorExit("SetConsoleMode");
在包含上述内容之后,您的程序开始运行,并且控制台光标通过您的 gotoxy()
函数,按您提供的MCVE代码的预期,在我的 Windows10控制台上!
After this above-quoted inclusion, your program started working and the Console Cursor kept following my Mouse pointer via your gotoxy()
function, as intended by your provided MCVE-code, on my Windows10-Console!
这篇关于控制台鼠标输入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!