C Win32控制台ReadConsoleInput() mingw – 鼠标滚轮输入的结果与预期不符

huangapple go评论75阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;windows.h&gt;

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, &amp;fdwSaveOldMode);

	
	SetConsoleMode(hStdInput,ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS);
	FlushConsoleInputBuffer(hStdInput);

	ReadConsoleInput(hStdInput,ir,128,&amp;nRead);
	
	for(i=0;i&lt;nRead;i++)
	{
		if(ir[i].EventType == MOUSE_EVENT)
		{
			if(ir[i].Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
			{
				printf(&quot;HIWORD(dwButtonState): %d\n&quot;, 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

huangapple
  • 本文由 发表于 2023年5月15日 05:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定