英文:
C Win32 console ReadConsoleInput() mingw - Unexpected results from mousewheel input
问题
From: https://learn.microsoft.com/en-us/windows/console/mouse-event-record-str
MOUSE_WHEELED 0x0004 鼠标垂直滚轮已移动。
如果dwButtonState
成员的高字包含正值,表示滚轮向远离用户方向旋转。否则,滚轮向用户方向旋转。
#include <stdio.h>
#include <windows.h>
int main(void);
int main()
{
INPUT_RECORD ir[128];
HANDLE hStdInput = NULL;
HANDLE hStdOutput = NULL;
DWORD nRead;
COORD xy;
DWORD fdwSaveOldMode;
int i;
hStdInput=GetStdHandle(STD_INPUT_HANDLE);
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hStdInput, &fdwSaveOldMode);
SetConsoleMode(hStdInput,ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS);
FlushConsoleInputBuffer(hStdInput);
ReadConsoleInput(hStdInput,ir,128,&nRead);
for(i=0;i<nRead;i++)
{
if(ir[i].EventType == MOUSE_EVENT)
{
if(ir[i].Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
{
printf("HIWORD(dwButtonState): %d\n", HIWORD(ir[i].Event.MouseEvent.dwButtonState));
}
}
}
}
我正在使用mingw编译器。根据Microsoft文档,当我向后旋转鼠标滚轮时,应该获得一个非正值。但事实并非如此。
我获得一致的值,但它们都是正值。dwButtonState
的值被定义为DWORD。
当我运行上面的代码并向前旋转鼠标滚轮时,我得到以下响应:
HIWORD(dwButtonState): 120
当我向后旋转鼠标滚轮时,我得到:
HIWORD(dwButtonState): 65416
我已经花了太多时间寻找答案,为什么我没有得到预期的结果。如果有答案,那么我可能错过了它。非常感谢任何帮助。
英文:
From: https://learn.microsoft.com/en-us/windows/console/mouse-event-record-str
> MOUSE_WHEELED 0x0004 The vertical mouse wheel was moved.
If the high word of the dwButtonState
member contains a positive value, the wheel was rotated forward, away from the user. Otherwise, the wheel was rotated backward, toward the user.
#include <stdio.h>
#include <windows.h>
int main(void);
int main()
{
INPUT_RECORD ir[128];
HANDLE hStdInput = NULL;
HANDLE hStdOutput = NULL;
DWORD nRead;
COORD xy;
DWORD fdwSaveOldMode;
int i;
hStdInput=GetStdHandle(STD_INPUT_HANDLE);
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hStdInput, &fdwSaveOldMode);
SetConsoleMode(hStdInput,ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS);
FlushConsoleInputBuffer(hStdInput);
ReadConsoleInput(hStdInput,ir,128,&nRead);
for(i=0;i<nRead;i++)
{
if(ir[i].EventType == MOUSE_EVENT)
{
if(ir[i].Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
{
printf("HIWORD(dwButtonState): %d\n", HIWORD(ir[i].Event.MouseEvent.dwButtonState));
}
}
}
}
I'm using the mingw compiler. According to Microsoft documentation I should be getting a non-positive value whenever I rotate the mouse wheel backward. That's not what is happening.
I get consistent values but they are both positive. The value for dwButtonState
is defined as a DWORD.
When I run the above & rotate the mouse wheel forward I get the following response:
> HIWORD(dwButtonState): 120
When I rotate the mouse wheel backward I get:
> HIWORD(dwButtonState): 65416
I've spent way too much time looking for an answer as to why I'm not getting the expected results. If it's out there then I've missed it. Any help is greatly appreciated.
答案1
得分: 0
A 16位值在传递给%d时被提升为int。 将其首先转换为(short)(HIWORD(ir[i].Event.MouseEvent.dwButtonState)),显示为-120。 - Weather Vane
英文:
A 16-bit value is promoted to int when passed for %d. Casting first as (short)(HIWORD(ir[i].Event.MouseEvent.dwButtonState)) shows -120. –
Weather Vane
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论