interaction-system 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +377 -0
- package/USAGE.md +490 -0
- package/dist/index.cjs.js +1983 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.es.js +1983 -0
- package/dist/index.es.js.map +1 -0
- package/dist/input/KeyEvent.d.ts +315 -0
- package/dist/input/KeyEvent.d.ts.map +1 -0
- package/dist/input/KeyInputHandler.d.ts +15 -0
- package/dist/input/KeyInputHandler.d.ts.map +1 -0
- package/dist/input/KeyToCodeMap.d.ts +2 -0
- package/dist/input/KeyToCodeMap.d.ts.map +1 -0
- package/dist/input/UIEventsCode.d.ts +175 -0
- package/dist/input/UIEventsCode.d.ts.map +1 -0
- package/dist/interaction/FeaturedInteractionHandler.d.ts +26 -0
- package/dist/interaction/FeaturedInteractionHandler.d.ts.map +1 -0
- package/dist/interaction/InteractionHandler.d.ts +86 -0
- package/dist/interaction/InteractionHandler.d.ts.map +1 -0
- package/dist/interaction/SimpleInteractionHandler.d.ts +19 -0
- package/dist/interaction/SimpleInteractionHandler.d.ts.map +1 -0
- package/dist/messages/CommandControlMessage.d.ts +49 -0
- package/dist/messages/CommandControlMessage.d.ts.map +1 -0
- package/dist/messages/ControlMessage.d.ts +25 -0
- package/dist/messages/ControlMessage.d.ts.map +1 -0
- package/dist/messages/KeyCodeControlMessage.d.ts +24 -0
- package/dist/messages/KeyCodeControlMessage.d.ts.map +1 -0
- package/dist/messages/ScrollControlMessage.d.ts +23 -0
- package/dist/messages/ScrollControlMessage.d.ts.map +1 -0
- package/dist/messages/TextControlMessage.d.ts +19 -0
- package/dist/messages/TextControlMessage.d.ts.map +1 -0
- package/dist/messages/TouchControlMessage.d.ts +44 -0
- package/dist/messages/TouchControlMessage.d.ts.map +1 -0
- package/dist/models/MotionEvent.d.ts +18 -0
- package/dist/models/MotionEvent.d.ts.map +1 -0
- package/dist/models/Point.d.ts +14 -0
- package/dist/models/Point.d.ts.map +1 -0
- package/dist/models/Position.d.ts +17 -0
- package/dist/models/Position.d.ts.map +1 -0
- package/dist/models/Rect.d.ts +22 -0
- package/dist/models/Rect.d.ts.map +1 -0
- package/dist/models/ScreenInfo.d.ts +14 -0
- package/dist/models/ScreenInfo.d.ts.map +1 -0
- package/dist/models/Size.d.ts +21 -0
- package/dist/models/Size.d.ts.map +1 -0
- package/dist/types/PlayerInterface.d.ts +20 -0
- package/dist/types/PlayerInterface.d.ts.map +1 -0
- package/dist/utils/Util.d.ts +26 -0
- package/dist/utils/Util.d.ts.map +1 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
# Interaction System
|
|
2
|
+
|
|
3
|
+
独立的用户交互处理和控制消息生成库,从 ws-scrcpy 项目中提取。
|
|
4
|
+
|
|
5
|
+
## 简介
|
|
6
|
+
|
|
7
|
+
这是一个完全独立的交互处理库,包含从用户交互(触摸、鼠标、键盘)到生成控制消息的完整流程。它提供了一套完整的解决方案,用于捕获浏览器中的用户输入并将其转换为结构化的控制消息。
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- **多种交互处理器**:支持功能完整的交互处理器和简化版交互处理器
|
|
12
|
+
- **完整的控制消息**:支持触摸、滚动、键盘、文本输入、命令等多种消息类型
|
|
13
|
+
- **键盘映射**:完整的浏览器键码到 Android 键码的映射
|
|
14
|
+
- **多点触控支持**:支持模拟多点触控手势
|
|
15
|
+
- **类型安全**:完全使用 TypeScript 编写,提供完整的类型定义
|
|
16
|
+
- **零依赖核心**:核心功能无外部依赖(仅依赖 buffer 包)
|
|
17
|
+
|
|
18
|
+
## 安装
|
|
19
|
+
|
|
20
|
+
### 从 npm 安装(公开发布后)
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install interaction-system
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 本地使用
|
|
27
|
+
|
|
28
|
+
如果你想在本地项目中使用此包,有以下几种方式:
|
|
29
|
+
|
|
30
|
+
#### 方法 1: npm link(推荐用于开发)
|
|
31
|
+
|
|
32
|
+
这种方式创建全局符号链接,适合频繁修改和测试:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# 在 interaction-system 目录中
|
|
36
|
+
cd interaction-system
|
|
37
|
+
npm link
|
|
38
|
+
|
|
39
|
+
# 在你的项目中
|
|
40
|
+
cd your-project
|
|
41
|
+
npm link interaction-system
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
之后可以像正常的 npm 包一样导入使用:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { FeaturedInteractionHandler } from 'interaction-system';
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**取消链接:**
|
|
51
|
+
```bash
|
|
52
|
+
# 在你的项目中
|
|
53
|
+
npm unlink interaction-system
|
|
54
|
+
|
|
55
|
+
# 在 interaction-system 目录中(可选)
|
|
56
|
+
npm unlink
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### 方法 2: 相对路径安装
|
|
60
|
+
|
|
61
|
+
直接从本地文件系统安装:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
cd your-project
|
|
65
|
+
npm install ../path/to/ws-scrcpy/interaction-system
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### 方法 3: file: 协议
|
|
69
|
+
|
|
70
|
+
在项目的 `package.json` 中添加依赖:
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
{
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"interaction-system": "file:../ws-scrcpy/interaction-system"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
然后运行 `npm install`。
|
|
81
|
+
|
|
82
|
+
#### 方法 4: 直接引用源码
|
|
83
|
+
|
|
84
|
+
如果在同一个 monorepo 中,可以直接引用 TypeScript 源码:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { FeaturedInteractionHandler } from '../../interaction-system/src';
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**注意事项:**
|
|
91
|
+
- 使用 `npm link` 时,包的更新会立即反映到所有链接的项目中
|
|
92
|
+
- 使用 `file:` 协议时,需要重新运行 `npm install` 才能获取更新
|
|
93
|
+
- 直接引用源码需要项目的构建工具能够处理 TypeScript
|
|
94
|
+
|
|
95
|
+
## 基本使用
|
|
96
|
+
|
|
97
|
+
### 1. 实现 IPlayer 接口
|
|
98
|
+
|
|
99
|
+
首先,你需要实现 `IPlayer` 接口:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { IPlayer, ScreenInfo } from 'interaction-system';
|
|
103
|
+
|
|
104
|
+
class MyPlayer implements IPlayer {
|
|
105
|
+
private canvas: HTMLCanvasElement;
|
|
106
|
+
private screenInfo?: ScreenInfo;
|
|
107
|
+
|
|
108
|
+
constructor() {
|
|
109
|
+
this.canvas = document.createElement('canvas');
|
|
110
|
+
document.body.appendChild(this.canvas);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getTouchableElement(): HTMLCanvasElement {
|
|
114
|
+
return this.canvas;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
getScreenInfo(): ScreenInfo | undefined {
|
|
118
|
+
return this.screenInfo;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
setScreenInfo(info: ScreenInfo) {
|
|
122
|
+
this.screenInfo = info;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### 2. 使用 FeaturedInteractionHandler
|
|
128
|
+
|
|
129
|
+
完整功能的交互处理器,支持触摸、鼠标、滚动和键盘输入:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import {
|
|
133
|
+
FeaturedInteractionHandler,
|
|
134
|
+
InteractionHandlerListener,
|
|
135
|
+
ControlMessage,
|
|
136
|
+
ScreenInfo,
|
|
137
|
+
Rect,
|
|
138
|
+
Size
|
|
139
|
+
} from 'interaction-system';
|
|
140
|
+
|
|
141
|
+
// 创建播放器实例
|
|
142
|
+
const player = new MyPlayer();
|
|
143
|
+
|
|
144
|
+
// 设置屏幕信息
|
|
145
|
+
const screenInfo = new ScreenInfo(
|
|
146
|
+
new Rect(0, 0, 1080, 1920),
|
|
147
|
+
new Size(1080, 1920),
|
|
148
|
+
0
|
|
149
|
+
);
|
|
150
|
+
player.setScreenInfo(screenInfo);
|
|
151
|
+
|
|
152
|
+
// 实现监听器
|
|
153
|
+
const listener: InteractionHandlerListener = {
|
|
154
|
+
sendMessage: (message: ControlMessage) => {
|
|
155
|
+
// 处理控制消息
|
|
156
|
+
console.log('Received message:', message);
|
|
157
|
+
const buffer = message.toBuffer();
|
|
158
|
+
// 发送 buffer 到服务器
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// 创建交互处理器
|
|
163
|
+
const handler = new FeaturedInteractionHandler(player, listener);
|
|
164
|
+
|
|
165
|
+
// 当不再需要时,释放资源
|
|
166
|
+
// handler.release();
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 3. 使用 SimpleInteractionHandler
|
|
170
|
+
|
|
171
|
+
简化版交互处理器,仅支持点击和滚动:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import {
|
|
175
|
+
SimpleInteractionHandler,
|
|
176
|
+
TouchHandlerListener,
|
|
177
|
+
Position
|
|
178
|
+
} from 'interaction-system';
|
|
179
|
+
|
|
180
|
+
const listener: TouchHandlerListener = {
|
|
181
|
+
performClick: (position: Position) => {
|
|
182
|
+
console.log('Click at:', position);
|
|
183
|
+
},
|
|
184
|
+
performScroll: (from: Position, to: Position) => {
|
|
185
|
+
console.log('Scroll from:', from, 'to:', to);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const simpleHandler = new SimpleInteractionHandler(player, listener);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 4. 键盘输入处理
|
|
193
|
+
|
|
194
|
+
独立的键盘输入处理器:
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { KeyInputHandler, KeyEventListener, KeyCodeControlMessage } from 'interaction-system';
|
|
198
|
+
|
|
199
|
+
const keyListener: KeyEventListener = {
|
|
200
|
+
onKeyEvent: (event: KeyCodeControlMessage) => {
|
|
201
|
+
console.log('Key event:', event);
|
|
202
|
+
const buffer = event.toBuffer();
|
|
203
|
+
// 发送到服务器
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// 添加监听器
|
|
208
|
+
KeyInputHandler.addEventListener(keyListener);
|
|
209
|
+
|
|
210
|
+
// 移除监听器
|
|
211
|
+
// KeyInputHandler.removeEventListener(keyListener);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### 5. 手动创建控制消息
|
|
215
|
+
|
|
216
|
+
你也可以手动创建和发送控制消息:
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import {
|
|
220
|
+
TouchControlMessage,
|
|
221
|
+
ScrollControlMessage,
|
|
222
|
+
TextControlMessage,
|
|
223
|
+
CommandControlMessage,
|
|
224
|
+
Position,
|
|
225
|
+
Point,
|
|
226
|
+
Size,
|
|
227
|
+
MotionEvent
|
|
228
|
+
} from 'interaction-system';
|
|
229
|
+
|
|
230
|
+
// 触摸消息
|
|
231
|
+
const touchMsg = new TouchControlMessage(
|
|
232
|
+
MotionEvent.ACTION_DOWN, // 动作类型
|
|
233
|
+
0, // 指针 ID
|
|
234
|
+
new Position(
|
|
235
|
+
new Point(100, 200), // 触摸点
|
|
236
|
+
new Size(1080, 1920) // 屏幕尺寸
|
|
237
|
+
),
|
|
238
|
+
1.0, // 压力值
|
|
239
|
+
MotionEvent.BUTTON_PRIMARY // 按钮
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
// 滚动消息
|
|
243
|
+
const scrollMsg = new ScrollControlMessage(
|
|
244
|
+
new Position(new Point(540, 960), new Size(1080, 1920)),
|
|
245
|
+
0, // 水平滚动
|
|
246
|
+
-1 // 垂直滚动
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// 文本消息
|
|
250
|
+
const textMsg = new TextControlMessage('Hello World');
|
|
251
|
+
|
|
252
|
+
// 命令消息
|
|
253
|
+
const clipboardMsg = CommandControlMessage.createSetClipboardCommand('复制的文本', false);
|
|
254
|
+
|
|
255
|
+
// 转换为 Buffer
|
|
256
|
+
const buffer = touchMsg.toBuffer();
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## API 文档
|
|
260
|
+
|
|
261
|
+
### 核心接口
|
|
262
|
+
|
|
263
|
+
#### IPlayer
|
|
264
|
+
|
|
265
|
+
播放器接口,需要由使用方实现:
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
interface IPlayer {
|
|
269
|
+
getTouchableElement(): HTMLCanvasElement;
|
|
270
|
+
getScreenInfo(): ScreenInfo | undefined;
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 交互处理器
|
|
275
|
+
|
|
276
|
+
#### InteractionHandler (抽象类)
|
|
277
|
+
|
|
278
|
+
基础交互处理器,提供了通用的交互处理逻辑。
|
|
279
|
+
|
|
280
|
+
#### FeaturedInteractionHandler
|
|
281
|
+
|
|
282
|
+
完整功能的交互处理器,支持:
|
|
283
|
+
- 触摸事件
|
|
284
|
+
- 鼠标事件
|
|
285
|
+
- 滚轮滚动
|
|
286
|
+
- 键盘输入
|
|
287
|
+
- 多点触控(Ctrl + 鼠标)
|
|
288
|
+
|
|
289
|
+
#### SimpleInteractionHandler
|
|
290
|
+
|
|
291
|
+
简化版交互处理器,仅支持:
|
|
292
|
+
- 点击
|
|
293
|
+
- 拖动滚动
|
|
294
|
+
|
|
295
|
+
### 控制消息
|
|
296
|
+
|
|
297
|
+
所有控制消息都继承自 `ControlMessage` 基类,并实现了 `toBuffer()` 方法用于序列化。
|
|
298
|
+
|
|
299
|
+
- `TouchControlMessage` - 触摸事件
|
|
300
|
+
- `ScrollControlMessage` - 滚动事件
|
|
301
|
+
- `KeyCodeControlMessage` - 键盘按键
|
|
302
|
+
- `TextControlMessage` - 文本输入
|
|
303
|
+
- `CommandControlMessage` - 系统命令(剪贴板、电源等)
|
|
304
|
+
|
|
305
|
+
### 数据模型
|
|
306
|
+
|
|
307
|
+
- `Point` - 二维点坐标
|
|
308
|
+
- `Size` - 尺寸(宽度和高度)
|
|
309
|
+
- `Position` - 位置(点 + 屏幕尺寸)
|
|
310
|
+
- `Rect` - 矩形区域
|
|
311
|
+
- `ScreenInfo` - 屏幕信息
|
|
312
|
+
- `MotionEvent` - 运动事件常量
|
|
313
|
+
|
|
314
|
+
## 高级用法
|
|
315
|
+
|
|
316
|
+
### 多点触控
|
|
317
|
+
|
|
318
|
+
使用 `FeaturedInteractionHandler` 时,按住 `Ctrl` 键可以激活多点触控模式:
|
|
319
|
+
|
|
320
|
+
- `Ctrl + 鼠标移动` - 创建对称的两个触摸点
|
|
321
|
+
- `Ctrl + Shift + 鼠标` - 以第一个点为中心创建镜像触摸点
|
|
322
|
+
|
|
323
|
+
### 自定义命令
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { CommandControlMessage, ControlMessage } from 'interaction-system';
|
|
327
|
+
|
|
328
|
+
// 设置剪贴板
|
|
329
|
+
const clipboardCmd = CommandControlMessage.createSetClipboardCommand('文本内容', false);
|
|
330
|
+
|
|
331
|
+
// 设置屏幕电源模式
|
|
332
|
+
const powerCmd = CommandControlMessage.createSetScreenPowerModeCommand(true);
|
|
333
|
+
|
|
334
|
+
// 创建简单命令
|
|
335
|
+
const expandNotifications = new CommandControlMessage(ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL);
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## 架构说明
|
|
339
|
+
|
|
340
|
+
```
|
|
341
|
+
interaction-system/
|
|
342
|
+
├── src/
|
|
343
|
+
│ ├── interaction/ # 交互处理器
|
|
344
|
+
│ ├── messages/ # 控制消息
|
|
345
|
+
│ ├── input/ # 键盘输入处理
|
|
346
|
+
│ ├── models/ # 数据模型
|
|
347
|
+
│ ├── utils/ # 工具函数
|
|
348
|
+
│ ├── types/ # TypeScript 接口
|
|
349
|
+
│ └── assets/ # 静态资源(图片等)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## 注意事项
|
|
353
|
+
|
|
354
|
+
1. **ScreenInfo 是必需的**:大多数交互功能都需要正确的 `ScreenInfo`,确保在使用前设置。
|
|
355
|
+
|
|
356
|
+
2. **内存管理**:使用完交互处理器后调用 `release()` 方法释放资源。
|
|
357
|
+
|
|
358
|
+
3. **Buffer 依赖**:控制消息序列化依赖 Node.js 的 `Buffer` API(通过 buffer 包提供浏览器支持)。
|
|
359
|
+
|
|
360
|
+
4. **图片资源**:多点触控可视化需要加载图片资源,确保构建工具正确处理 PNG 导入。
|
|
361
|
+
|
|
362
|
+
## TypeScript 支持
|
|
363
|
+
|
|
364
|
+
本库完全使用 TypeScript 编写,提供完整的类型定义。无需额外安装 @types 包。
|
|
365
|
+
|
|
366
|
+
## 许可证
|
|
367
|
+
|
|
368
|
+
本项目采用 Apache-2.0 许可证。
|
|
369
|
+
|
|
370
|
+
## 贡献
|
|
371
|
+
|
|
372
|
+
欢迎提交 Issue 和 Pull Request!
|
|
373
|
+
|
|
374
|
+
## 相关项目
|
|
375
|
+
|
|
376
|
+
- [ws-scrcpy](https://github.com/NetrisTV/ws-scrcpy) - 原始项目
|
|
377
|
+
|