From c9cb4b74bcd022ad57aa9ac799e6fab18c7cf38b Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Wed, 29 Dec 2021 23:20:32 -0500 Subject: [PATCH] use keyboard to control robot --- software/pc/hexapod.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/software/pc/hexapod.py b/software/pc/hexapod.py index 343da48..d66cf40 100644 --- a/software/pc/hexapod.py +++ b/software/pc/hexapod.py @@ -129,8 +129,36 @@ class MyApp(QtWidgets.QMainWindow): self.on_turnright_button_clicked ) + self.ui.textBrowser_TcpClientMessage.installEventFilter(self) + self.ui.show() + def eventFilter(self, widget, event): + if self.ui.textBrowser_TcpClientMessage.isEnabled(): + if (event.type() == QtCore.QEvent.KeyPress): + key = event.key() + if key == QtCore.Qt.Key_Up: + self.on_forward_button_clicked() + elif key == QtCore.Qt.Key_Down: + self.on_backward_button_clicked() + elif key == QtCore.Qt.Key_Left: + self.on_turnleft_button_clicked() + elif key == QtCore.Qt.Key_Right: + self.on_turnright_button_clicked() + elif key == QtCore.Qt.Key_A: + self.on_shiftleft_button_clicked() + elif key == QtCore.Qt.Key_D: + self.on_shiftright_button_clicked() + elif key == QtCore.Qt.Key_W: + self.on_forward_button_clicked() + elif key == QtCore.Qt.Key_S: + self.on_backward_button_clicked() + elif key == QtCore.Qt.Key_Space: + self.on_standby_button_clicked() + + return True + return QtWidgets.QWidget.eventFilter(self, widget, event) + def save_config(self): try: json.dump(self.config, open('config.json', 'w+')) @@ -142,6 +170,7 @@ class MyApp(QtWidgets.QMainWindow): self.update_network_interfaces() self.ui.groupBox_Control.setEnabled(False) + self.ui.textBrowser_TcpClientMessage.setEnabled(False) tcp_client_ip = self.config.get('TCP_Client_IP', '127.0.0.1') tcp_client_port = self.config.get('TCP_Client_Port', '1234') @@ -299,6 +328,7 @@ class MyApp(QtWidgets.QMainWindow): self.ui.textBrowser_TcpClientMessage.setEnabled(False) self.ui.groupBox_Control.setEnabled(False) + self.ui.textBrowser_TcpClientMessage.setEnabled(False) self.ui.status_bar.clearMessage() self.ui.status_bar.setStyleSheet('color: green') @@ -307,8 +337,10 @@ class MyApp(QtWidgets.QMainWindow): elif status == TCPClient.CONNECTED: self.ui.button_TcpClient.setText('Disconnect') self.ui.groupBox_Control.setEnabled(True) + self.ui.textBrowser_TcpClientMessage.setEnabled(True) self.ui.textBrowser_TcpClientMessage.setEnabled(True) + self.ui.textBrowser_TcpClientMessage.setFocus() self.ui.status_bar.clearMessage() self.ui.status_bar.setStyleSheet('color: green')