From 62de92e7a21ccff332c377b1982476c1d2def6b3 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 24 Oct 2025 19:03:28 +0200 Subject: [PATCH] feat: add control by key through recorder --- src/recorder.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/recorder.py b/src/recorder.py index a2f9d5f..1837ffb 100644 --- a/src/recorder.py +++ b/src/recorder.py @@ -6,6 +6,7 @@ from typing import Optional from PyQt6 import uic from PyQt6.QtCore import QObject, QThread, QTimer, pyqtSignal, pyqtSlot +from PyQt6.QtGui import QKeyEvent from PyQt6.QtWidgets import QMainWindow from src.command import CarControl, Command, ControlCommand, RecordingCommand @@ -105,6 +106,13 @@ class RecorderWindow(Ui_Recorder, QMainWindow): SAVE_DIR: Path = Path(__file__).parent.parent / "records" + COMMAND_DIRECTIONS: dict[str, CarControl] = { + "w": CarControl.FORWARD, + "s": CarControl.BACKWARD, + "d": CarControl.RIGHT, + "a": CarControl.LEFT, + } + def __init__(self, host: str, port: int) -> None: super().__init__() @@ -120,13 +128,6 @@ class RecorderWindow(Ui_Recorder, QMainWindow): uic.load_ui.loadUi("src/recorder.ui", self) - self.command_directions = { - "w": CarControl.FORWARD, - "s": CarControl.BACKWARD, - "d": CarControl.RIGHT, - "a": CarControl.LEFT, - } - self.forwardButton.pressed.connect( lambda: self.on_car_controlled(CarControl.FORWARD, True) ) @@ -173,6 +174,25 @@ class RecorderWindow(Ui_Recorder, QMainWindow): def on_car_controlled(self, control: CarControl, active: bool): self.send_command(ControlCommand(control, active)) + def keyPressEvent(self, event): # type: ignore + if event.isAutoRepeat(): + return + + if isinstance(event, QKeyEvent): + key_text = event.text() + ctrl: Optional[CarControl] = self.COMMAND_DIRECTIONS.get(key_text) + if ctrl is not None: + self.on_car_controlled(ctrl, True) + + def keyReleaseEvent(self, event): # type: ignore + if event.isAutoRepeat(): + return + if isinstance(event, QKeyEvent): + key_text = event.text() + ctrl: Optional[CarControl] = self.COMMAND_DIRECTIONS.get(key_text) + if ctrl is not None: + self.on_car_controlled(ctrl, False) + def toggle_record(self): self.recording = not self.recording self.recordDataButton.setText(