GetAsyncKeyState的返回值似乎不与描述相符 [英] GetAsyncKeyState's return values don't seem to correspond with the description
问题描述
我调查 GetAsyncKeyState 看到它在如何通过WASD键使屏幕上的一个虚构人物招一个code示例后。
我读到这MSDN其页面上:
返回值
类型:短的
如果函数成功,返回值指定是否的关键
因为对GetAsyncKeyState最后一次通话是pressed,以及是否
关键是目前向上或向下。
<青霉>如果最显著位被设置,则
键是向下,并且如果至少显著位被设置的,关键是
在previous调用GetAsyncKeyState后pressed。的
块引用>我无法理解的返回值的结构:
报告说,类型为
短
,所以通过它,我想你可以重新present数为零,二进制,如0000000000000000
,或十六进制为0×0000
(因为据我知道有一个十六进制数字再presents四个二进制位)。描述说,的最显著位的,应在最关键的是pressed设置。这让我觉得,如果说有点设置,数量将显示如下:
千兆
要更好地利用它在我的计划,我通过将其转换成十六进制,导致缩短到它:
为0x8000
由于
8
十六进制应该以二进制对应1000
。当我看到说机制并没有在我的计划工作(做
GetAsyncKeyState(CHR)==为0x8000
总是会产生错误,不管,关键是pressed与否)我花了一多看看我看到在第一次使用了功能最初的例子。还有,返回值正在同号比较-32767
,其中,当快译使用Windows计算器为十六进制,导致入值0xFFFFFFFFFFFF8001
。现在,不介意最后一个十六进制数字(1),因为那是用来检查至少显著位是否也是激活了一点,但我不希望在我的情况。这我不明白这里的事情如下:
- 为什么,如果一个十六进制数字再presents四个二进制数字和功能的产值是短(两个字节),这里的数字是重新$ P $由16个16进制psented?不是那些八个字节?
- 返回值的描述说,的最显著位的是,如果密钥是pressed设置为1,这使我相信这个数字看起来像
千兆
,而不是0000000000001000
。缺少什么我在这里?解决方案GetAsyncKeyState(CHR)==为0x8000
这是不正确的检查。用途:
!!(GetAsyncKeyState(CHR)及为0x8000)
,查看是否最显著位被设置。
值
0xFFFFFFFFFFFF8001
不符合从GetAsyncKeyState实际返回的值。 Windows计算器不知道从GetAsyncKeyState返回值的大小,所以它假设你用64位数字的工作。您可以覆盖在其设置。I investigated GetAsyncKeyState after seeing it in a code example on how to make a fictional character move on the screen through the WASD keys.
I read this on its MSDN page:
Return value
Type: SHORT
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down.
If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.
I'm having trouble understanding the structure of the return value:
It says the type is
short
, so through it I suppose you could represent the number zero, in binary, as0000000000000000
, or in hex as0x0000
(since as far as I know one hex digit represents four binary digits).The description says that the most significant bit should be set when the key is pressed. That makes me think that, if said bit was set, the number would appear like this:
1000000000000000
To make better use of it in my program, I shortened it by translating it to hex, resulting into:
0x8000
Because
8
in hex should correspond to1000
in binary.When I saw that said mechanism was not working in my program (doing
GetAsyncKeyState(chr) == 0x8000
would always yield false, no matter if the key was pressed or not) I took one more look to the original example I saw the function employed in first. There, the return value was being compared with the number-32767
, which, when quickly translated to hex using the Windows calculator, results into the value0xFFFFFFFFFFFF8001
. Now, don't mind the last hex digit (1) because that's the bit used to check whether the least significant bit is also active, but I don't want that in my case.The things that I can't understand here are the following:
- Why, if one hex digit represents four binary digits, and the output value of the function is a short (two bytes), here the number is represented by sixteen hex digits? Aren't those eight bytes?
- The description of the return value said that the most significant bit would be set to 1 if the key was being pressed, and that led me to believe that the number would look like
1000000000000000
, not0000000000001000
. What am I missing here?
解决方案GetAsyncKeyState(chr) == 0x8000
This is incorrect check. Use:
!!(GetAsyncKeyState(chr) & 0x8000)
to see if the most-significant bit is set.
The value
0xFFFFFFFFFFFF8001
doesn't correspond to the actual returned value from GetAsyncKeyState. Windows calculator doesn't know the size of the value returned from GetAsyncKeyState, so it assumes you're working with 64-bit number. You can override it in its settings.这篇关于GetAsyncKeyState的返回值似乎不与描述相符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!