windows 提到键盘控件时,0x80 代码是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2746817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the 0x80 code mean when referring to keyboard controls
提问by Paz
what does the 0x80 code mean when referring to the keyboard controls in C++ Windows environment?
在 C++ Windows 环境中提到键盘控件时,0x80 代码是什么意思?
For example,
例如,
if(GetKeyState('K') & 0x80) {
//do something
}
Thanks everyone!
谢谢大家!
回答by Jacob
Update
更新
A flurry of downvotes propelled me into investigating this further. Here's how the return values (in hex) of GetKeyState
works. I don't quite get the toggle property of a key like k
but I'm assuming there's some default state it toggles from.
一连串的反对票促使我进一步调查此事。这是返回值(以十六进制表示)的GetKeyState
工作方式。我不太明白像这样的键的切换属性,k
但我假设它有一些默认状态可以切换。
0 Default State, key up
ff80 Default state, key down
1 Toggled, key up
ff81 Toggled, key down
So 0xff80
is added whenever the high-order bit needs to be set and the low-order bit makes sense. So now we know why the 0x80
approach works --- since the high-order bit of the lower byte is set as well!
So0xff80
是在需要设置高位且低位有意义时添加的。所以现在我们知道为什么这种0x80
方法有效——因为低字节的高位也被设置了!
Old Answer
旧答案
GetKeyStatereturns a SHORT
where if the high-order bit is 1
it means the key is up. The bitwise ANDoperation with 0x80
just checks if that bit is 1
since in binary 0x80
is 10000000
.
GetKeyState返回一个SHORT
,如果高位是,1
则表示密钥已启动。该位与与操作0x80
只是检查,如果该位是1
因为二进制0x80
是10000000
。
Therefore the statement GetKeyState('K') & 0x80
would return 0x80
if the high-order bit of the value returned by GetKeyState('K')
is 1
and 0
if the high-order bit is 0
.
因此,GetKeyState('K') & 0x80
如果返回0x80
值的高位GetKeyState('K')
为1
并且0
高位为,则语句将返回0
。
回答by shoosh
The MSDN documentation of the functionstates:
If the high-order bit is 1, the key is down; otherwise, it is up.
如果高位为1,则key为down;否则,它是了。
bit-wise and with 0x80 gives you the high order bit, the if
checks if the result is zero or non-zero and in essence checks the value of that bit.
按位和 0x80 为您提供高位,if
检查结果是零还是非零,本质上检查该位的值。
This check however looks like a mistake since GetKeyState()
returns a SHORT
and to check the high order bit of a short you need to bit-wise and with 0x8000.
So I suggest you check the return value with a debugger and verify how this works in reality.
但是,此检查看起来像一个错误,因为GetKeyState()
返回 aSHORT
并检查短路的高位,您需要按位并使用 0x8000。
所以我建议你用调试器检查返回值并验证它在现实中是如何工作的。
回答by Stewart
I think you mean 0x8000, not 0x80. If that is the case, you should consult the documentation (http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx) which has the following to say on the return value of GetKeyState:-
我想你的意思是 0x8000,而不是 0x80。如果是这种情况,您应该查阅文档(http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx),其中对 GetKeyState 的返回值有以下说明: ——
The return value specifies the status of the specified virtual key, as follows:
返回值指定了指定虚拟键的状态,如下:
?If the high-order bit is 1, the key is down; otherwise, it is up. ?If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
?如果高位为1,则key为down;否则,它是了。?如果低位为1,则键被切换。如果某个键(例如 CAPS LOCK 键)打开,则会切换该键。如果低位为 0,则该键关闭且未切换。当该键被切换时,键盘上的切换键的指示灯(如果有)将亮起,当该键被取消切换时熄灭。
0x80 doesn't mean anything as far as I know though
据我所知,0x80 并不意味着什么
回答by BlueRaja - Danny Pflughoeft
According to the documentation
根据文档
The return value specifies the status of the specified virtual key:
If the high-order bit is 1, the key is down; otherwise, it is up.
If the low-order bit is 1, the key is toggled.A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
返回值指定了指定虚拟按键的状态:
如果高位为1,则按键为down;否则,它是了。
如果低位为 1,则键被切换。如果某个键(例如 CAPS LOCK 键)打开,则会切换该键。如果低位为 0,则该键关闭且未切换。当该键被切换时,键盘上的切换键的指示灯(如果有)将亮起,当该键被取消切换时熄灭。
Perhaps with a non-toggleable key (such a 'K'), the low-order (ambiguous term - perhaps they mean 0x0080 ?)and high-order (0x8000) bits do the same thing?
也许使用不可切换的密钥(例如“K”),低位(含糊不清的术语 - 也许它们的意思是 0x0080 ?)和高位 (0x8000) 位做同样的事情?