C++ 使用 GetKeyState()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6331868/
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
Using GetKeyState()
提问by CaptainProg
I would like to have a boolean event toggle when a key is pressed. Specifically, the 's' key. I have been pointed to the function GetKeyState(), which supposedly works under the Win32 API. I understand the ASCII code for the letter 's' is 115, and so my code is as follows:
我想在按下某个键时进行布尔事件切换。具体来说,'s' 键。我已经指出了函数 GetKeyState(),据说它在 Win32 API 下工作。我知道字母 's' 的 ASCII 码是 115,所以我的代码如下:
if (GetKeyState(115) == 1)
{
<EVENT>
}
However, this does not work. Why? Here is the MSDN reference: http://msdn.microsoft.com/en-us/library/ms646301%28v=vs.85%29.aspx... "If the low-order bit is 1, the key is toggled"
但是,这不起作用。为什么?这是 MSDN 参考:http: //msdn.microsoft.com/en-us/library/ms646301%28v=vs.85%29.aspx...“如果低位为 1,则切换键”
回答by Piotr Praszmo
From what I understand you need to do:
据我了解,您需要做的是:
if( GetKeyState(115) & 0x8000 )
{
<EVENT>
}
The highest bit tells if key is pressed. The lowest tells if key is toggled (like, if caps lock is turned on).
最高位表示是否按下了键。最低的告诉键是否被切换(例如,如果大写锁定被打开)。
回答by Simon Rozman
Since SHORT is signed, high-order bit equals sign bit.
由于 SHORT 是有符号的,因此高位等于符号位。
Therefore to test if a given key is pressed, simply test if the value returned by GetKeyState()
is negative:
因此要测试是否按下了给定的键,只需测试返回的值GetKeyState()
是否为负:
if (GetKeyState('S') < 0) {
// The S key is down.
} else {
// The S key is up.
}
Besides, 115 is ASCII code for 's'. I believe, you should use capital case 83 to test the 'S' key.
此外,115 是 's' 的 ASCII 码。我相信,您应该使用大写字母 83 来测试“S”键。
回答by T.I.Adrian
Sometimes you want to use a combination of keys.
有时您想使用组合键。
To avoid the situations when a combination of keys (eg: VK_SHIFT && VK_LEFT) satisfies two conditions:
为了避免组合键(例如:VK_SHIFT && VK_LEFT)满足两个条件的情况:
std::cout << "Shift RIGHT pressed" << std::endl;
std::cout << "LEFT key pressed" << std::endl;
just use Sleep(...);
and GetAsyncKeyState(VK_...)
只需使用Sleep(...);
和GetAsyncKeyState(VK_...)
GetKeyState() vs. GetAsyncKeyState() vs. getch()?
GetKeyState() 与 GetAsyncKeyState() 与 getch()?
#include <windows.h>
...
while (1)
{
if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_LEFT) & 0x8000))
{
Sleep(200);
std::cout << "Shift LEFT pressed" << std::endl;
}
if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_RIGHT) & 0x8000))
{
Sleep(200);
std::cout << "Shift RIGHT pressed" << std::endl;
}
if (GetAsyncKeyState(VK_RIGHT))
{
std::cout << "RIGHT key pressed" << std::endl;
}
if (GetAsyncKeyState(VK_LEFT))
{
std::cout << "LEFT key pressed" << std::endl;
}
}
回答by Adolfo Cárdenas
I use a global variable bool altgr
我使用全局变量 bool altgr
Example:
例子:
void Editor::HandleKey(char car) {
bool shift = false;
//bool altgr = false;
bool printable = false;
if (car == 27) SendMessage(hwnd, WM_DESTROY, 0, 0);
if ((GetKeyState(VK_CAPITAL) & 0x0001) == 1) shift = true;
if ((GetKeyState(VK_SHIFT) & 0x8000) == 0x8000) shift = true;
// if(GetKeyState(VK_RMENU) & 0x80000000 == 0x80000000) altgr = true;
if (car == 18) altgr = true;
回答by Laurie
Bit late for this but the high order bit is 0x80000000 not 0x8000, change this and it will work fine.
有点晚了,但高位是 0x80000000 不是 0x8000,改变它,它会正常工作。
The other bit - useful for nothing - it acts as if you pressed CAPS LOCK when you pressed LSHIFT.
另一位 - 毫无用处 - 它的作用就像你在按下 LSHIFT 时按下了 CAPS LOCK。