c++ - GetAsyncKeyState() defn 中的 "Shouldn' t be relied upon 是什么意思

标签 c++ winapi

From MSDN :

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. However, you should not rely on this last behavior; [..]

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.

在我的程序中,我只对键当前按下位置的返回值感兴趣。即,如 msdn 所定义,当最低有效位和最高有效位都已设置时 (-32767)(这就是它现在的工作方式。)。但是,我是否还必须检查 32767(没有最小信号位的值)在这种情况下,就像它说的那样,最低有效位被另一个程序截获,或者是“不可靠的”,无论这意味着什么。

最佳答案

嗯,是的,基本上。不过,只检查最高有效位而完全忽略最低有效位更容易。

must I also check for 32767 (the value without the least sig. bit)

这是不对的 - 值将为 -32768(假设二进制补码):

1000 0000 0000 0001   -32767
1000 0000 0000 0000   -32768

另见 here ;我很奇怪这个值被打包到一个 short 中,但我们开始了。

关于c++ - GetAsyncKeyState() defn 中的 "Shouldn' t be relied upon 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547377/

相关文章:

c++ - Dll 只能通过 LoadLibrary 正确加载吗?

c++ - 错误 : unitialized member with const type c++

成员值从指针更改为非指针时的 C++ const 问题

C++-gsoap : Parameter passing memory management issues

c++ - 为什么 GetProcessImageFileName 返回 null 而不是进程的地址?

c++ - 如何在将输出打印到cmd.exe后显示cmd提示符?

windows - 创建虚拟文件夹并将它们挂接到文件系统

c# - 用于 usb 设备通知的 ServiceControlHandler,OnStop() 不可实现

c++ - *buf++ = *buf + 10 - 解释

winapi - 有谁知道advapi代表什么?