How do I check if a Key is pressed on C++(如何检查是否在 C++ 上按下了某个键)
问题描述
如何检查 Windows 上是否按下了某个键?
How could I possibly check if a Key is pressed on Windows?
推荐答案
正如其他人所说,没有跨平台的方法可以做到这一点,但在 Windows 上你可以这样做:
As mentioned by others there's no cross platform way to do this, but on Windows you can do it like this:
下面的代码检查A"键是否按下.
The Code below checks if the key 'A' is down.
if(GetKeyState('A') & 0x8000/*Check if high-order bit is set (1 << 15)*/)
{
// Do stuff
}
如果发生轮班或类似情况,您需要通过以下其中一项:https://msdn.microsoft.com/de-de/library/windows/desktop/dd375731(v=vs.85).aspx
In case of shift or similar you will need to pass one of these: https://msdn.microsoft.com/de-de/library/windows/desktop/dd375731(v=vs.85).aspx
if(GetKeyState(VK_SHIFT) & 0x8000)
{
// Shift down
}
低位表示按键是否被切换.
The low-order bit indicates if key is toggled.
SHORT keyState = GetKeyState(VK_CAPITAL/*(caps lock)*/);
bool isToggled = keyState & 1;
bool isDown = keyState & 0x8000;
哦,别忘了
#include <Windows.h>
这篇关于如何检查是否在 C++ 上按下了某个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查是否在 C++ 上按下了某个键


- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01