mirror of
https://github.com/motajs/template.git
synced 2026-11-24 23:14:32 +08:00
init: 2.B
This commit is contained in:
commit
6bf32174d8
610 files changed
+151444
No files matched your search
@@ -0,0 +1,311 @@
|
||||
# Hotkey API 文档
|
||||
|
||||
本文档由 `DeepSeek R1` 模型生成并微调。
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
Hotkey --> EventEmitter
|
||||
|
||||
click EventEmitter "https://nodejs.org/api/events.html#class-eventemitter"
|
||||
```
|
||||
|
||||
## 类描述
|
||||
|
||||
`Hotkey` 是按键系统的核心类,用于管理按键绑定、辅助键状态、按键分组及事件触发逻辑。继承自 `EventEmitter`,支持自定义事件监听。主要用于实现复杂的快捷键系统。
|
||||
|
||||
---
|
||||
|
||||
## 属性说明
|
||||
|
||||
| 属性名 | 类型 | 描述 |
|
||||
| -------------- | ---------------------------- | -------------------------------------------- |
|
||||
| `id` | `string` | 控制器的唯一标识符 |
|
||||
| `name` | `string` | 控制器的显示名称 |
|
||||
| `data` | `Record<string, HotkeyData>` | 存储所有已注册的按键配置 |
|
||||
| `keyMap` | `Map<KeyCode, HotkeyData[]>` | 按键代码到配置的映射(支持多键绑定同一操作) |
|
||||
| `enabled` | `boolean` | 当前控制器是否启用(默认 `false`) |
|
||||
| `list`(静态) | `Hotkey[]` | 静态属性,存储所有已创建的控制器实例 |
|
||||
|
||||
---
|
||||
|
||||
## 构造方法
|
||||
|
||||
```typescript
|
||||
function constructor(id: string, name: string): Hotkey;
|
||||
```
|
||||
|
||||
- **参数**
|
||||
- `id`: 控制器的唯一标识符
|
||||
- `name`: 控制器的显示名称
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
const editorHotkey = new Hotkey('editor', '编辑器快捷键');
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 方法说明
|
||||
|
||||
### `register`
|
||||
|
||||
```typescript
|
||||
function register(data: RegisterHotkeyData): this;
|
||||
```
|
||||
|
||||
注册一个按键配置。
|
||||
|
||||
- **参数**
|
||||
```typescript
|
||||
interface RegisterHotkeyData {
|
||||
id: string; // 按键唯一标识(可含数字后缀,如 "copy_1")
|
||||
name: string; // 显示名称(如 "复制")
|
||||
defaults: KeyCode; // 默认按键代码
|
||||
ctrl?: boolean; // 是否默认需要 Ctrl 辅助键
|
||||
shift?: boolean; // 是否默认需要 Shift 辅助键
|
||||
alt?: boolean; // 是否默认需要 Alt 辅助键
|
||||
}
|
||||
```
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
editorHotkey.register({
|
||||
id: 'copy',
|
||||
name: '复制',
|
||||
defaults: KeyCode.KeyC,
|
||||
ctrl: true
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `realize`
|
||||
|
||||
```typescript
|
||||
function realize(id: string, func: HotkeyFunc, config?: HotkeyEmitConfig): this;
|
||||
```
|
||||
|
||||
为按键绑定触发逻辑。
|
||||
|
||||
- **参数**
|
||||
- `id`: 目标按键 ID(无需后缀)
|
||||
- `func`: 触发时执行的函数
|
||||
- `config`: 触发类型配置(节流/超时等)
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
editorHotkey.realize(
|
||||
'copy',
|
||||
(id, key, ev) => {
|
||||
console.log('执行复制操作');
|
||||
},
|
||||
{ type: 'down-throttle', throttle: 500 }
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `group`
|
||||
|
||||
```typescript
|
||||
function group(id: string, name: string, keys?: RegisterHotkeyData[]): this;
|
||||
```
|
||||
|
||||
创建按键分组,后续注册的按键自动加入该组。
|
||||
|
||||
- **参数**
|
||||
- `id`: 分组唯一标识
|
||||
- `name`: 分组显示名称
|
||||
- `keys`: 可选,预注册的按键列表
|
||||
|
||||
---
|
||||
|
||||
### `set`
|
||||
|
||||
```typescript
|
||||
function set(id: string, key: KeyCode, assist: number, emit?: boolean): void;
|
||||
```
|
||||
|
||||
动态修改按键绑定。
|
||||
|
||||
- **参数**
|
||||
- `id`: 目标按键 ID
|
||||
- `key`: 新按键代码
|
||||
- `assist`: 辅助键状态(二进制位:Ctrl=1<<0, Shift=1<<1, Alt=1<<2)
|
||||
- `emit`: 是否触发 `set` 事件(默认 `true`)
|
||||
|
||||
---
|
||||
|
||||
### `when`
|
||||
|
||||
```typescript
|
||||
function when(fn: () => boolean): this;
|
||||
```
|
||||
|
||||
为当前作用域的按键绑定添加触发条件。
|
||||
|
||||
- **参数**
|
||||
- `fn`: 条件函数,返回 `true` 时允许触发按键逻辑
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
// 仅在游戏处于运行状态时允许触发
|
||||
controller.when(() => gameState === 'running');
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `enable`
|
||||
|
||||
```typescript
|
||||
function enable(): void;
|
||||
```
|
||||
|
||||
### `disable`
|
||||
|
||||
```typescript
|
||||
function disable(): void;
|
||||
```
|
||||
|
||||
启用/禁用整个按键控制器(禁用后所有按键事件将被忽略)。
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
// 暂停游戏时禁用按键
|
||||
controller.disable();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `use`
|
||||
|
||||
```typescript
|
||||
function use(symbol: symbol): void;
|
||||
```
|
||||
|
||||
切换当前作用域,后续 `realize` 方法绑定的逻辑将关联到该作用域。
|
||||
|
||||
- **参数**
|
||||
- `symbol`: 唯一作用域标识符
|
||||
|
||||
---
|
||||
|
||||
### `dispose`
|
||||
|
||||
```typescript
|
||||
function dispose(symbol?: symbol): void;
|
||||
```
|
||||
|
||||
释放指定作用域及其绑定的所有按键逻辑。
|
||||
|
||||
- **参数**
|
||||
- `symbol`(可选): 要释放的作用域(默认释放当前作用域)
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
const scope = Symbol();
|
||||
controller.use(scope);
|
||||
// ...绑定操作...
|
||||
controller.dispose(scope); // 释放该作用域
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `emitKey`
|
||||
|
||||
```typescript
|
||||
function emitKey(
|
||||
key: KeyCode,
|
||||
assist: number,
|
||||
type: KeyEventType,
|
||||
ev: KeyboardEvent
|
||||
): boolean;
|
||||
```
|
||||
|
||||
手动触发按键事件(可用于模拟按键操作)。
|
||||
|
||||
- **参数**
|
||||
- `key`: 按键代码
|
||||
- `assist`: 辅助键状态(二进制位:`Ctrl=1<<0`, `Shift=1<<1`, `Alt=1<<2`)
|
||||
- `type`: 事件类型(`'up'` 或 `'down'`)
|
||||
- `ev`: 原始键盘事件对象
|
||||
- **返回值**
|
||||
`true` 表示事件被成功处理,`false` 表示无匹配逻辑
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
// 模拟触发 Ctrl+S 保存操作
|
||||
controller.emitKey(
|
||||
KeyCode.KeyS,
|
||||
1 << 0, // Ctrl 激活
|
||||
'down',
|
||||
new KeyboardEvent('keydown')
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 静态方法说明
|
||||
|
||||
### `Hotkey.get`
|
||||
|
||||
```typescript
|
||||
function get(id: string): Hotkey | undefined;
|
||||
```
|
||||
|
||||
**静态方法**:根据 ID 获取控制器实例。
|
||||
|
||||
---
|
||||
|
||||
## 事件说明
|
||||
|
||||
| 事件名 | 参数类型 | 触发时机 |
|
||||
| --------- | --------------------------------------------------- | ---------------- |
|
||||
| `set` | `[id: string, key: KeyCode, assist: number]` | 按键绑定被修改时 |
|
||||
| `emit` | `[key: KeyCode, assist: number, type: KeyEmitType]` | 按键被触发时 |
|
||||
| `press` | `[key: KeyCode]` | 按键被按下时 |
|
||||
| `release` | `[key: KeyCode]` | 按键被释放时 |
|
||||
|
||||
**事件监听示例**
|
||||
|
||||
```typescript
|
||||
gameKey.on('emit', (key, assist) => {
|
||||
console.log(`按键 ${KeyCode[key]} 触发,辅助键状态:${assist}`);
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 总使用示例
|
||||
|
||||
::: code-group
|
||||
|
||||
```typescript [注册]
|
||||
import { gameKey } from '@motajs/system-action';
|
||||
|
||||
gameKey.register({
|
||||
id: 'jump',
|
||||
name: '跳跃',
|
||||
defaults: KeyCode.Space
|
||||
});
|
||||
```
|
||||
|
||||
```typescript [实现]
|
||||
import { useKey } from '@motajs/render-vue';
|
||||
|
||||
const [gameKey] = useKey();
|
||||
|
||||
// 绑定跳跃逻辑
|
||||
gameKey.realize('jump', () => {
|
||||
player.jump();
|
||||
});
|
||||
```
|
||||
|
||||
:::
|
||||
@@ -0,0 +1,221 @@
|
||||
# Keyboard API 文档
|
||||
|
||||
本文档由 `DeepSeek R1` 模型生成并微调。
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
Keyboard --> EventEmitter
|
||||
```
|
||||
|
||||
## 类描述
|
||||
|
||||
`Keyboard` 是虚拟键盘的核心类,用于管理动态按键布局、处理按键事件及辅助键状态。继承自 `EventEmitter`,支持自定义事件监听。
|
||||
|
||||
---
|
||||
|
||||
## 属性说明
|
||||
|
||||
| 属性名 | 类型 | 描述 |
|
||||
| -------------- | ---------------- | ----------------------------------------------------------- |
|
||||
| `id` | `string` | 键盘的唯一标识符 |
|
||||
| `keys` | `KeyboardItem[]` | 当前键盘包含的按键列表(响应式数组) |
|
||||
| `assist` | `number` | 辅助键状态(二进制位表示:Ctrl=1<<0, Shift=1<<1, Alt=1<<2) |
|
||||
| `fontSize` | `number` | 按键文本字体大小(默认 18) |
|
||||
| `list`(静态) | `Keyboard[]` | 静态属性,存储所有已创建的键盘实例 |
|
||||
|
||||
**KeyboardItem 结构**:
|
||||
|
||||
```typescript
|
||||
interface KeyboardItem {
|
||||
key: KeyCode; // 按键代码
|
||||
text?: string; // 显示文本(可选)
|
||||
x: number; // X 坐标
|
||||
y: number; // Y 坐标
|
||||
width: number; // 宽度
|
||||
height: number; // 高度
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 构造方法
|
||||
|
||||
```typescript
|
||||
function constructor(id: string): Keyboard;
|
||||
```
|
||||
|
||||
- **参数**
|
||||
- `id`: 键盘的唯一标识符
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
const numpad = new Keyboard('numpad');
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 方法说明
|
||||
|
||||
### `add`
|
||||
|
||||
```typescript
|
||||
function add(item: KeyboardItem): Keyboard;
|
||||
```
|
||||
|
||||
### `remove`
|
||||
|
||||
```typescript
|
||||
function remove(item: KeyboardItem): Keyboard;
|
||||
```
|
||||
|
||||
添加/移除按键,返回当前实例以便链式调用。
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
// 添加数字键 1
|
||||
numpad.add({
|
||||
key: KeyCode.Digit1,
|
||||
text: '1',
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 60,
|
||||
height: 60
|
||||
});
|
||||
|
||||
// 移除按键
|
||||
numpad.remove(existingKey);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `extend`
|
||||
|
||||
```typescript
|
||||
function extend(
|
||||
keyboard: Keyboard,
|
||||
offsetX?: number,
|
||||
offsetY?: number
|
||||
): Keyboard;
|
||||
```
|
||||
|
||||
继承其他键盘的按键布局并添加偏移量。
|
||||
|
||||
**示例**
|
||||
|
||||
```typescript
|
||||
const extendedKB = new Keyboard('extended');
|
||||
extendedKB.extend(numpad, 100, 0); // 向右偏移 100px
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `emitKey`
|
||||
|
||||
```typescript
|
||||
function emitKey(key: KeyboardItem, index: number): void;
|
||||
```
|
||||
|
||||
模拟触发按键事件(自动处理辅助键状态)。
|
||||
|
||||
---
|
||||
|
||||
### `createScope`
|
||||
|
||||
```typescript
|
||||
function createScope(): symbol;
|
||||
```
|
||||
|
||||
### `disposeScope`
|
||||
|
||||
```typescript
|
||||
function disposeScope(): void;
|
||||
```
|
||||
|
||||
管理事件监听作用域:
|
||||
|
||||
- `createScope`: 创建新作用域(返回唯一标识符)
|
||||
- `disposeScope`: 释放当前作用域
|
||||
|
||||
---
|
||||
|
||||
### `withAssist`
|
||||
|
||||
```typescript
|
||||
function withAssist(assist: number): symbol;
|
||||
```
|
||||
|
||||
创建预设辅助键状态的作用域(如 `Ctrl+Shift`)。
|
||||
|
||||
---
|
||||
|
||||
### `Keyboard.get`
|
||||
|
||||
```typescript
|
||||
function get(id: string): Keyboard | undefined;
|
||||
```
|
||||
|
||||
**静态方法**:根据 ID 获取键盘实例。
|
||||
|
||||
---
|
||||
|
||||
## 事件说明
|
||||
|
||||
| 事件名 | 参数类型 | 触发时机 |
|
||||
| -------------- | ------------------------- | ------------------ |
|
||||
| `add` | `KeyboardItem` | 新增按键时 |
|
||||
| `remove` | `KeyboardItem` | 移除按键时 |
|
||||
| `extend` | `Keyboard` | 继承其他键盘布局时 |
|
||||
| `emit` | `item, assist, index, ev` | 触发按键时 |
|
||||
| `scopeCreate` | `symbol` | 创建作用域时 |
|
||||
| `scopeDispose` | `symbol` | 释放作用域时 |
|
||||
|
||||
**事件监听示例**
|
||||
|
||||
```typescript
|
||||
numpad.on('emit', (item, assist) => {
|
||||
console.log(`按键 ${item.key} 触发,辅助键状态:${assist}`);
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 总使用示例
|
||||
|
||||
```typescript
|
||||
import { KeyCode } from '@motajs/client-base';
|
||||
import { Keyboard } from '@motajs/system-action';
|
||||
|
||||
// 创建数字键盘
|
||||
const numpad = new Keyboard('numpad');
|
||||
|
||||
// 添加基础按键
|
||||
numpad
|
||||
.add({ key: KeyCode.Digit1, x: 0, y: 0, width: 60, height: 60 })
|
||||
.add({ key: KeyCode.Digit2, x: 60, y: 0, width: 60, height: 60 });
|
||||
|
||||
// 添加功能键(带辅助状态)
|
||||
const ctrlScope = numpad.withAssist(1 << 0); // Ctrl 激活
|
||||
numpad.add({
|
||||
key: KeyCode.KeyC,
|
||||
text: '复制',
|
||||
x: 120,
|
||||
y: 0,
|
||||
width: 120,
|
||||
height: 60
|
||||
});
|
||||
|
||||
// 监听复制键
|
||||
numpad.on('emit', item => {
|
||||
if (item.key === KeyCode.KeyC) {
|
||||
console.log('执行复制操作');
|
||||
}
|
||||
});
|
||||
|
||||
// 触发按键
|
||||
numpad.emitKey(numpad.keys[0], 0); // 模拟按下数字 1
|
||||
|
||||
// 获取键盘实例
|
||||
const foundKB = Keyboard.get('numpad');
|
||||
```
|
||||
@@ -0,0 +1,3 @@
|
||||
# @motajs/system-action
|
||||
|
||||
目录:
|
||||
Reference in new issue
Block a user