feat(sender): 串流窗口补齐键盘/右键/滚轮与双向剪贴板同步
- video_window 处理右/中键(WM_RBUTTON/MBUTTON)与滚轮(WM_MOUSEWHEEL), 并通过 ttstd/video 的 mouse / wheel 方法通道交给 Dart - 键盘走 WM_KEYDOWN/UP 报 Windows 虚拟键码,Dart 侧映射成 Android keycode; Esc 仍保留为关窗,Alt 组合键旁路转发不吞掉 - 右键→返回、中键→多任务(buttons 位 bit1/bit2),滚轮按「deltaY 正=向下滚」取反 - 本机剪贴板每 700ms 轮询比较后下发 msg_type=4,被控端上行内容写入本机剪贴板; 两端都记住最近一次同步的文本,避免来回回送 - 控制栏「发送剪贴板」改为读取本机剪贴板实时内容(原先发的是固定占位字符串)
This commit is contained in:
1 parent
5f9fe88e37
commit
85e53f64ca
6 files changed
+345
-2
No files matched your search
@@ -37,6 +37,15 @@ class SenderBridge {
|
||||
/// x/y 是**画面像素坐标**(编码分辨率),由被控端换算成真实屏幕坐标。
|
||||
void Function(int action, int x, int y)? onVideoPointer;
|
||||
|
||||
/// 串流窗口里的右键/中键:(button, action, x, y),button 1=右键 2=中键。
|
||||
void Function(int button, int action, int x, int y)? onVideoMouseButton;
|
||||
|
||||
/// 串流窗口里的滚轮:(deltaX, deltaY),正数=向上/向右。
|
||||
void Function(int deltaX, int deltaY)? onVideoWheel;
|
||||
|
||||
/// 串流窗口里的键盘:(action, vk, modifiers),vk 是 Windows 虚拟键码。
|
||||
void Function(int action, int vk, int modifiers)? onVideoKey;
|
||||
|
||||
/// 取一条被控端上行的控制事件(投屏应答 / 剪贴板),没有则返回 null。
|
||||
///
|
||||
/// 事件形如 `{"type":8,"action":2,"text":""}`,`type` 是 msgType。
|
||||
@@ -54,6 +63,29 @@ class SenderBridge {
|
||||
final y = (args['y'] as num?)?.toInt();
|
||||
if (action == null || x == null || y == null) return;
|
||||
onVideoPointer?.call(action, x, y);
|
||||
case 'mouse':
|
||||
final args = call.arguments;
|
||||
if (args is! Map) return;
|
||||
final button = (args['button'] as num?)?.toInt();
|
||||
final action = (args['action'] as num?)?.toInt();
|
||||
final x = (args['x'] as num?)?.toInt();
|
||||
final y = (args['y'] as num?)?.toInt();
|
||||
if (button == null || action == null || x == null || y == null) return;
|
||||
onVideoMouseButton?.call(button, action, x, y);
|
||||
case 'wheel':
|
||||
final args = call.arguments;
|
||||
if (args is! Map) return;
|
||||
onVideoWheel?.call(
|
||||
(args['deltaX'] as num?)?.toInt() ?? 0,
|
||||
(args['deltaY'] as num?)?.toInt() ?? 0,
|
||||
);
|
||||
case 'key':
|
||||
final args = call.arguments;
|
||||
if (args is! Map) return;
|
||||
final action = (args['action'] as num?)?.toInt();
|
||||
final vk = (args['vk'] as num?)?.toInt();
|
||||
if (action == null || vk == null) return;
|
||||
onVideoKey?.call(action, vk, (args['modifiers'] as num?)?.toInt() ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,14 +56,16 @@ class ControlToolbar extends StatelessWidget {
|
||||
_Action(
|
||||
icon: Icons.mouse,
|
||||
label: '滚轮向下',
|
||||
// 协议约定 deltaY 正数 = 向下滚(与真实滚轮事件一致)。
|
||||
enabled: enabled,
|
||||
onPressed: () => onEvent('wheel', {'deltaX': 0, 'deltaY': -1, 'buttons': 0}),
|
||||
onPressed: () => onEvent('wheel', {'deltaX': 0, 'deltaY': 1, 'buttons': 0}),
|
||||
),
|
||||
_Action(
|
||||
icon: Icons.content_paste,
|
||||
label: '发送剪贴板',
|
||||
enabled: enabled,
|
||||
onPressed: () => onEvent('clipboard', {'text': 'TTSTD Connect'}),
|
||||
// 不带文本:由调用方读本机剪贴板实时内容再下发。
|
||||
onPressed: () => onEvent('clipboard', const <String, dynamic>{}),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -52,6 +52,7 @@ class _HomePageState extends State<HomePage> {
|
||||
Timer? _discoveryTimer;
|
||||
Timer? _videoStatsTimer;
|
||||
Timer? _eventTimer;
|
||||
Timer? _clipboardTimer;
|
||||
|
||||
List<DeviceInfo> _devices = const <DeviceInfo>[];
|
||||
DeviceInfo? _selected;
|
||||
@@ -73,6 +74,8 @@ class _HomePageState extends State<HomePage> {
|
||||
DateTime? _lastFrameAt;
|
||||
/// 因关窗而停流只下发一次 stopMirror。
|
||||
bool _stopSentForWindowClose = false;
|
||||
/// 最近一次同步过的剪贴板文本(收/发都记),用来避免两端来回回送同一条内容。
|
||||
String? _lastClipboardText;
|
||||
|
||||
static const Duration streamingIdleTimeout = Duration(seconds: 2);
|
||||
String? _nativeError;
|
||||
@@ -90,6 +93,10 @@ class _HomePageState extends State<HomePage> {
|
||||
_bridge.onVideoWindowClosed = _onStreamWindowClosed;
|
||||
// 串流窗口里的鼠标点击/拖拽 → 触摸控制帧。
|
||||
_bridge.onVideoPointer = _onVideoPointer;
|
||||
// 右键/中键、滚轮、键盘 → 对应的控制帧。
|
||||
_bridge.onVideoMouseButton = _onVideoMouseButton;
|
||||
_bridge.onVideoWheel = _onVideoWheel;
|
||||
_bridge.onVideoKey = _onVideoKey;
|
||||
_diagnosticLog.start();
|
||||
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
|
||||
_bridge.watchPhase().listen((phase) {
|
||||
@@ -120,6 +127,11 @@ class _HomePageState extends State<HomePage> {
|
||||
const Duration(milliseconds: 250),
|
||||
(_) => _drainInboundEvents(),
|
||||
);
|
||||
// 本机剪贴板 → 被控端:Windows 侧没有可用的剪贴板变更通知,只能轮询比较。
|
||||
_clipboardTimer = Timer.periodic(
|
||||
const Duration(milliseconds: 700),
|
||||
(_) => _syncLocalClipboard(),
|
||||
);
|
||||
_refreshVideoTextureId();
|
||||
// 自测/演示用开关:TTSTD_DEMO_AUTOSTART=1 时启动即开启本机监听。
|
||||
if (Platform.environment['TTSTD_DEMO_AUTOSTART'] == '1') {
|
||||
@@ -136,6 +148,7 @@ class _HomePageState extends State<HomePage> {
|
||||
_discoveryTimer?.cancel();
|
||||
_videoStatsTimer?.cancel();
|
||||
_eventTimer?.cancel();
|
||||
_clipboardTimer?.cancel();
|
||||
_diagnosticLog.dispose();
|
||||
_bridge.stopDemoServer();
|
||||
_bridge.stopLocalSignaling();
|
||||
@@ -232,10 +245,50 @@ class _HomePageState extends State<HomePage> {
|
||||
/// 被控端上行的剪贴板内容 → 写入本机剪贴板。
|
||||
Future<void> _applyRemoteClipboard(String text) async {
|
||||
if (text.isEmpty || !_settings.clipboardSync) return;
|
||||
// 先记住这条内容,否则本机剪贴板轮询会把它当成「本机新复制」再发回去。
|
||||
_lastClipboardText = text;
|
||||
await Clipboard.setData(ClipboardData(text: text));
|
||||
_diagnosticLog.write('clipboard from peer applied (${text.length} chars)');
|
||||
}
|
||||
|
||||
/// 本机剪贴板变化 → 下发被控端。
|
||||
///
|
||||
/// Windows 没有可订阅的剪贴板变更事件,只能定时读一次再和上次内容比较;
|
||||
/// 内容与 [_lastClipboardText] 相同说明是刚同步过来的,不再回送。
|
||||
Future<void> _syncLocalClipboard() async {
|
||||
if (!mounted || _nativeCore == null) return;
|
||||
if (!_phase.isConnected || !_settings.clipboardSync) return;
|
||||
final ClipboardData? data;
|
||||
try {
|
||||
data = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
} on Object {
|
||||
// 剪贴板被别的程序占用(Office 等的延迟渲染)时读会抛错,下一轮再试。
|
||||
return;
|
||||
}
|
||||
final text = data?.text;
|
||||
if (text == null || text.isEmpty || text == _lastClipboardText) return;
|
||||
_lastClipboardText = text;
|
||||
_bridge.sendControl('clipboard', {'text': text});
|
||||
_diagnosticLog.write('clipboard -> peer (${text.length} chars)');
|
||||
}
|
||||
|
||||
/// 手动发送一次本机剪贴板内容(控制栏「发送剪贴板」按钮)。
|
||||
Future<void> _sendLocalClipboard() async {
|
||||
if (!_phase.isConnected) {
|
||||
_snack('请先连接被控端');
|
||||
return;
|
||||
}
|
||||
final data = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
final text = data?.text;
|
||||
if (text == null || text.isEmpty) {
|
||||
_snack('本机剪贴板为空');
|
||||
return;
|
||||
}
|
||||
_lastClipboardText = text;
|
||||
_bridge.sendControl('clipboard', {'text': text});
|
||||
_snack('已发送剪贴板内容(${text.length} 字)');
|
||||
}
|
||||
|
||||
/// 把本机会话偏好下发给被控端(msgType=9 会话配置帧)。
|
||||
///
|
||||
/// 必须在请求投屏之前下发:被控端按该配置决定编码格式、码率、音频与剪贴板同步。
|
||||
@@ -379,6 +432,47 @@ class _HomePageState extends State<HomePage> {
|
||||
});
|
||||
}
|
||||
|
||||
/// 串流窗口里的右键/中键 → 鼠标控制帧。
|
||||
///
|
||||
/// 被控端把右键映射成「返回」、中键映射成「多任务」,符合桌面操作习惯。
|
||||
void _onVideoMouseButton(int button, int action, int x, int y) {
|
||||
if (!_phase.isConnected) return;
|
||||
final buttons = switch (button) {
|
||||
1 => 0x02, // bit1 = 右键
|
||||
2 => 0x04, // bit2 = 中键
|
||||
_ => 0x01, // bit0 = 左键
|
||||
};
|
||||
_sendControl('mouse', {
|
||||
'action': action,
|
||||
'x': x,
|
||||
'y': y,
|
||||
'buttons': buttons,
|
||||
});
|
||||
}
|
||||
|
||||
/// 串流窗口里的滚轮 → 滚轮控制帧(被控端用屏幕中心作为滚动起点)。
|
||||
void _onVideoWheel(int deltaX, int deltaY) {
|
||||
if (!_phase.isConnected) return;
|
||||
if (deltaX == 0 && deltaY == 0) return;
|
||||
_sendControl('wheel', {
|
||||
'deltaX': deltaX,
|
||||
'deltaY': deltaY,
|
||||
'buttons': 0,
|
||||
});
|
||||
}
|
||||
|
||||
/// 串流窗口里的键盘 → 按键控制帧。
|
||||
void _onVideoKey(int action, int vk, int modifiers) {
|
||||
if (!_phase.isConnected) return;
|
||||
final keycode = _androidKeycodeFromVk(vk);
|
||||
if (keycode == null) return;
|
||||
_sendControl('key', {
|
||||
'action': action,
|
||||
'keycode': keycode,
|
||||
'modifiers': modifiers,
|
||||
});
|
||||
}
|
||||
|
||||
void _snack(String message) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context)
|
||||
@@ -631,6 +725,11 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
|
||||
void _sendControl(String type, Map<String, dynamic> payload) {
|
||||
// 控制栏的「发送剪贴板」:payload 不带文本时读本机剪贴板实时内容。
|
||||
if (type == 'clipboard' && payload['text'] == null) {
|
||||
unawaited(_sendLocalClipboard());
|
||||
return;
|
||||
}
|
||||
_bridge.sendControl(type, payload);
|
||||
// 只有一次性动作才弹提示:触摸/鼠标/按键/滚轮是连续事件(拖拽时每秒几十条),
|
||||
// 每条都弹 SnackBar 会把 Flutter UI 线程压满,串流反而更卡。
|
||||
@@ -896,3 +995,47 @@ class _HomePageState extends State<HomePage> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Windows 虚拟键码(VK_*)→ Android KeyEvent keycode;不支持的键返回 null。
|
||||
///
|
||||
/// 只覆盖常见键:字母/数字/编辑键/方向键/功能键与标点。中文等文本输入依赖输入法,
|
||||
/// 注入按键无法替代,属于已知限制。
|
||||
int? _androidKeycodeFromVk(int vk) {
|
||||
if (vk >= 0x41 && vk <= 0x5A) return 29 + (vk - 0x41); // A-Z
|
||||
if (vk >= 0x30 && vk <= 0x39) return 7 + (vk - 0x30); // 0-9
|
||||
if (vk >= 0x70 && vk <= 0x7B) return 131 + (vk - 0x70); // F1-F12
|
||||
return switch (vk) {
|
||||
0x08 => 67, // Backspace
|
||||
0x09 => 61, // Tab
|
||||
0x0D => 66, // Enter
|
||||
0x10 => 59, // Shift → KEYCODE_SHIFT_LEFT
|
||||
0x11 => 113, // Ctrl → KEYCODE_CTRL_LEFT
|
||||
0x12 => 57, // Alt → KEYCODE_ALT_LEFT
|
||||
0x14 => 115, // CapsLock
|
||||
0x1B => 111, // Escape
|
||||
0x20 => 62, // Space
|
||||
0x21 => 92, // PageUp
|
||||
0x22 => 93, // PageDown
|
||||
0x23 => 123, // End
|
||||
0x24 => 122, // Home
|
||||
0x25 => 21, // Left
|
||||
0x26 => 19, // Up
|
||||
0x27 => 22, // Right
|
||||
0x28 => 20, // Down
|
||||
0x2D => 124, // Insert
|
||||
0x2E => 112, // Delete
|
||||
0x5B || 0x5C => 117, // Win → KEYCODE_META_LEFT
|
||||
0xBA => 74, // 分号
|
||||
0xBB => 70, // 等号
|
||||
0xBC => 55, // 逗号
|
||||
0xBD => 69, // 减号
|
||||
0xBE => 56, // 句点
|
||||
0xBF => 76, // 斜杠
|
||||
0xC0 => 68, // 反引号
|
||||
0xDB => 71, // 左方括号
|
||||
0xDC => 73, // 反斜杠
|
||||
0xDD => 72, // 右方括号
|
||||
0xDE => 75, // 单引号
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user