【发布时间】:2018-03-21 08:56:55
【问题描述】:
我正在编写一个与鼠标交互的 Win32 控制台应用程序。我正在使用ReadConsoleInput 来像这样获得与窗口相关的鼠标移动。这是我的问题的简化版本:
int main(void)
{
HANDLE hStdin;
DWORD cNumRead;
INPUT_RECORD irInBuf[128];
hStdin = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hStdin, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_PROCESSED_INPUT);
while (1)
{
mouse_position_changed = 0;
ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead);
/* input handler here: changes the cursor position if the mouse position changed;
clears screen if mouse position changed;
sets mouse_position_changed (self-explanatory).
(this part of the code is irrelevant to the quesiton at hand) */
if (!mouse_position_changed)
putchar('0');
}
}
(我已经删除了大部分代码,包括错误检查。这是我正在做的一个简单的、淡化的版本;它比使 0 远离光标的范围要大得多。)
我希望在移动鼠标时清除屏幕并将光标设置为鼠标坐标。这部分工作。
我希望在鼠标不移动时将0打印在屏幕上。这将产生 0 远离鼠标光标的效果。这不起作用,因为ReadConsoleInput 将阻塞直到它收到输入。
在收到更多输入之前,不会打印0。除非用户不断敲击键盘,否则不会打印任何内容,因为只要移动鼠标,屏幕就会被清除。
问题
即使没有输入,我也希望循环继续。 ReadConsoleInput 等待输入被读取,这意味着循环将暂停,直到敲击键盘或移动鼠标。
我正在寻找ReadConsoleInput 的替代方案,或使其成为非阻塞的方法。
【问题讨论】:
标签: c windows winapi console mouse