From 85cdf2bc2004b43fcd1bd6e22d980f666f62d683 Mon Sep 17 00:00:00 2001 From: Arduinum628 Date: Mon, 17 Nov 2025 14:21:48 +0300 Subject: [PATCH] feat: added universal command handler for keyboard, updated command handler for mouse to universal. --- static/command.js | 109 +++++++++++++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/static/command.js b/static/command.js index 3a4f908..8d4ce47 100644 --- a/static/command.js +++ b/static/command.js @@ -63,41 +63,88 @@ document.addEventListener("DOMContentLoaded", () => { } } - const commandStop = {"command": "stop"}; + // Команда остановки + const commandStop = { "command": "stop" }; - // Назначение обработчиков событий для кнопки "Вперёд" - const forwardButton = document.getElementById("forward-button"); - forwardButton.addEventListener("mousedown", () => startSendingCommand({"command": "forward"})); // Начало отправки команды - forwardButton.addEventListener("mouseup", () => { - sendCommand(JSON.stringify(commandStop)); - stopSendingCommand(); - }); // отправка команды forwardOff и остановка отправки при отпускании кнопки - forwardButton.addEventListener("mouseleave", stopSendingCommand); // Остановка отправки, если курсор уходит с кнопки + let command = null; + let currentCommand = null; - // Назначение обработчиков событий для кнопки "Влево" - const leftButton = document.getElementById("left-button"); - leftButton.addEventListener("mousedown", () => startSendingCommand({"command": "left"})); - leftButton.addEventListener("mouseup", () => { - sendCommand(JSON.stringify(commandStop)); - stopSendingCommand(); + // Управление кнопками мыши + function robotControlButton(buttonId, commandName) { + const button = document.getElementById(buttonId); + if (!button) return; + + button.addEventListener("mousedown", () => { + if (currentCommand !== commandName) { + currentCommand = commandName; + startSendingCommand({command: commandName}); + } + }); + + const stopHandler = () => { + if (currentCommand === commandName) { + sendCommand(JSON.stringify(commandStop)); + stopSendingCommand(); + currentCommand = null; + } + }; + + button.addEventListener("mouseup", stopHandler); + button.addEventListener("mouseleave", stopHandler); + } + + // Инициализация кнопок + robotControlButton("forward-button", "forward"); + robotControlButton("backward-button", "backward"); + robotControlButton("left-button", "left"); + robotControlButton("right-button", "right"); + + const keyMap = { + forwardKeys: ["w", "arrowup", "8"], + backwardKeys: ["s", "arrowdown", "2"], + leftKeys: ["a", "arrowleft", "4"], + rightKeys: ["d", "arrowright", "6"] + }; + + let activeKey = null; // конкретная активная клавиша + + document.addEventListener("keydown", (event) => { + const key = event.key.toLowerCase(); + + // если уже нажата какая-то клавиша — игнорируем новую + if (activeKey) return; + + if (keyMap.forwardKeys.includes(key)) { + command = "forward"; + } else if (keyMap.backwardKeys.includes(key)) { + command = "backward"; + } else if (keyMap.leftKeys.includes(key)) { + command = "left"; + } else if (keyMap.rightKeys.includes(key)) { + command = "right"; + } else { + return; // неуправляющая клавиша + } + + if (command !== currentCommand) { + activeKey = key; // запоминаем, какая клавиша активна + currentCommand = command; + + startSendingCommand({ "command": command }); + } }); - leftButton.addEventListener("mouseleave", stopSendingCommand); - // Назначение обработчиков событий для кнопки "Вправо" - const rightButton = document.getElementById("right-button"); - rightButton.addEventListener("mousedown", () => startSendingCommand({"command": "right"})); - rightButton.addEventListener("mouseup", () => { - sendCommand(JSON.stringify(commandStop)); - stopSendingCommand(); - }); - rightButton.addEventListener("mouseleave", stopSendingCommand); + document.addEventListener("keyup", (event) => { + const key = event.key.toLowerCase(); - // Назначение обработчиков событий для кнопки "Назад" - const backwardButton = document.getElementById("backward-button"); - backwardButton.addEventListener("mousedown", () => startSendingCommand({"command": "backward"})); - backwardButton.addEventListener("mouseup", () => { - sendCommand(JSON.stringify(commandStop)); - stopSendingCommand(); + // сработает стоп только если отпущена именно активная клавиша + if (key === activeKey) { + sendCommand(JSON.stringify(commandStop)); + stopSendingCommand(); + + activeKey = null; + command = null; + currentCommand = null; + } }); - backwardButton.addEventListener("mouseleave", stopSendingCommand); }); \ No newline at end of file -- 2.49.1