Files
engine/docs/zh/input/keyboard.mdx
鹅叔 dc1a4793ed Docs: remove playgrounds (#2689)
* fix: doc typo
2025-05-23 17:24:53 +08:00

61 lines
2.2 KiB
Plaintext
Raw Permalink 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.
---
order: 2
title: 键盘
type: 交互
label: Interact
---
Galacean 支持开发者随时查询当前的键盘交互实况,且调用接口十分简单。
## 方法
| 方法名称 | 方法释义 |
| ------------------------------------------------------ | -------------------------- |
| [isKeyHeldDown](/apis/core/#InputManager-isKeyHeldDown) | 返回这个按键是否被持续按住 |
| [isKeyDown](/apis/core/#InputManager-isKeyDown) | 返回当前帧是否按下过此按键 |
| [isKeyUp](/apis/core/#InputManager-isKeyUp) | 返回当前帧是否抬起过此按键 |
## 快速上手
下方枚举了检测按键状态的简单示例。
```typescript
class KeyScript extends Script {
onUpdate() {
const { inputManager } = this.engine;
if (inputManager.isKeyHeldDown(Keys.Space)) {
// 现在还按着空格键
}
if (inputManager.isKeyDown(Keys.Space)) {
// 这帧按下过空格键
}
if (inputManager.isKeyUp(Keys.Space)) {
// 这帧抬起过空格键
}
}
}
```
## 状态字典
| 按键状态 | isKeyHeldDown | isKeyDown | isKeyUp |
| -------------------------- | ------------- | --------- | ------- |
| 该键从上帧开始就一直按着 | true | false | false |
| 该键当前帧按下后就没有松开 | true | true | false |
| 该键在当前帧松开后又按下 | true | true | true |
| 该键在当前帧按下后又松开 | false | true | true |
| 该键在当前帧被抬起 | false | false | true |
| 该键没按下且没交互 | false | false | false |
| 不会出现这种情况 | true | false | true |
| 不会出现这种情况 | false | true | false |
## Keys
Galacean 所枚举的键盘 Keys 与实体键盘一一对应,参考 W3C 标准,且兼容各种不同硬件的特制按键。
Keys 枚举https://github.com/galacean/engine/blob/main/packages/core/src/input/enums/Keys.ts
W3C 标准https://www.w3.org/TR/2017/CR-uievents-code-20170601/
键盘输入设计思路https://github.com/galacean/engine/wiki/Keyboard-Input-design