Files
XCEngine/docs/api/XCEngine/Input/InputManager/IsKeyPressed.md

54 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# InputManager::IsKeyPressed
查询一个键是否在本帧刚被按下。
```cpp
bool IsKeyPressed(KeyCode key) const;
```
## 行为说明
当前实现判断条件是:
```cpp
m_keyDownThisFrame[index] && !m_keyDownLastFrame[index]
```
因此它表达的是“当前帧出现了按下消息,而且上一帧没有同一个按下标记”。
这有两个直接含义:
- 它是帧级边沿接口,不是持续按住态;按住请使用 [IsKeyDown](IsKeyDown.md)
- 它依赖 [Update](Update.md) 推进帧边界;如果 `Update()` 调用时机不稳定,边沿判断就会失真
还要特别注意当前实现不会过滤 `repeat`
- [ProcessKeyDown](ProcessKeyDown.md) 无论 `repeat``true` 还是 `false`,都会把 `m_keyDownThisFrame[index]` 设为 `true`
- 因此平台如果在后续帧继续发送重复 `KeyDown``IsKeyPressed()` 仍可能再次返回 `true`
所以它更准确的定义是“这一帧收到按键按下消息”,而不是“严格意义上的首次物理按下”。
## 参数
- `key` - 要查询的键。
## 返回值
- `bool` - 当前帧是否刚按下。
## 示例
```cpp
if (XCEngine::Input::InputManager::Get().IsKeyPressed(XCEngine::Input::KeyCode::Space)) {
// ...
}
```
## 相关文档
- [返回类型总览](InputManager.md)
- [Update](Update.md)
- [IsKeyDown](IsKeyDown.md)
- [ProcessKeyDown](ProcessKeyDown.md)
- [IsKeyReleased](IsKeyReleased.md)