机器人控制界面 添加键盘 控件 控制页面方向盘
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useCallback, useRef, useState } from 'react';
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { ChevronUp, ChevronDown, ChevronLeft, ChevronRight, Circle } from 'lucide-react';
|
||||
|
||||
@@ -15,6 +15,22 @@ interface DPadControlProps {
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
// 键盘按键映射
|
||||
const KEY_MAP: Record<string, string> = {
|
||||
ArrowUp: 'up',
|
||||
ArrowDown: 'down',
|
||||
ArrowLeft: 'left',
|
||||
ArrowRight: 'right',
|
||||
w: 'up',
|
||||
s: 'down',
|
||||
a: 'left',
|
||||
d: 'right',
|
||||
W: 'up',
|
||||
S: 'down',
|
||||
A: 'left',
|
||||
D: 'right',
|
||||
};
|
||||
|
||||
const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChange, disabled = false }) => {
|
||||
const { t } = useTranslation();
|
||||
const [pressed, setPressed] = useState<Record<string, boolean>>({});
|
||||
@@ -23,6 +39,8 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
||||
// 用 ref 存储最新状态,避免 setInterval 闭包捕获旧值
|
||||
const pressedRef = useRef<Record<string, boolean>>({});
|
||||
const speedRef = useRef(0.5);
|
||||
// 记录当前按下的键盘按键,避免 keydown 重复触发
|
||||
const activeKeysRef = useRef<Set<string>>(new Set());
|
||||
|
||||
// 构建状态:只传输 xy 坐标(axes),buttons 全部为 false
|
||||
const buildState = useCallback(
|
||||
@@ -86,6 +104,53 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
||||
[disabled, emit],
|
||||
);
|
||||
|
||||
// 键盘控制:方向键 / WASD
|
||||
useEffect(() => {
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
// 焦点在输入框时不触发
|
||||
const target = e.target as HTMLElement;
|
||||
if (target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable)) {
|
||||
return;
|
||||
}
|
||||
const key = KEY_MAP[e.key];
|
||||
if (!key) return;
|
||||
// 防止 keydown 持续触发(系统自动重复)
|
||||
if (activeKeysRef.current.has(key)) return;
|
||||
activeKeysRef.current.add(key);
|
||||
e.preventDefault();
|
||||
startPress(key);
|
||||
};
|
||||
|
||||
const handleKeyUp = (e: KeyboardEvent) => {
|
||||
const key = KEY_MAP[e.key];
|
||||
if (!key) return;
|
||||
activeKeysRef.current.delete(key);
|
||||
e.preventDefault();
|
||||
endPress(key);
|
||||
};
|
||||
|
||||
// 窗口失焦时清空所有按键状态
|
||||
const handleBlur = () => {
|
||||
activeKeysRef.current.forEach((key) => endPress(key));
|
||||
activeKeysRef.current.clear();
|
||||
};
|
||||
|
||||
window.addEventListener('keydown', handleKeyDown);
|
||||
window.addEventListener('keyup', handleKeyUp);
|
||||
window.addEventListener('blur', handleBlur);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('keydown', handleKeyDown);
|
||||
window.removeEventListener('keyup', handleKeyUp);
|
||||
window.removeEventListener('blur', handleBlur);
|
||||
// 清理定时器
|
||||
if (pressTimer.current) {
|
||||
window.clearInterval(pressTimer.current);
|
||||
pressTimer.current = null;
|
||||
}
|
||||
};
|
||||
}, [startPress, endPress]);
|
||||
|
||||
const btnBase =
|
||||
'flex items-center justify-center w-8 h-8 rounded-xl border transition-all duration-150 select-none active:scale-95';
|
||||
const btnActive = 'border-cyan-400 bg-cyan-500/30 text-cyan-300 shadow-[0_0_16px_rgba(34,211,238,0.4)]';
|
||||
|
||||
Reference in New Issue
Block a user