#!/usr/bin/env python import sys from pynput.keyboard import Key, Listener import subprocess running_process = None def execute_launch(key): global running_process launch_files = { 'a': ('/home/firefly/pibot_ros/ros_ws/src/upbot_vision/launch/upbot_vision.launch', []), # stereo 'b': ('/home/firefly/pibot_ros/ros_ws/src/pibot_navigation/launch/nav.launch', ['map_name:=10_10m']), # nav 'c': ('/home/firefly/pibot_ros/ros_ws/src/ipa_coverage_planning/ipa_room_exploration/ros/test/room_exploration_client.launch', ['robot_env:=10_10m']), # client 'd': ('/home/firefly/pibot_ros/ros_ws/src/ipa_coverage_planning/ipa_room_exploration/ros/launch/room_exploration_action_server.launch', []), # server } # 检查按键是否对应一个.launch文件 if key in launch_files: # 获取对应的.launch文件和参数 launch_file, launch_args = launch_files[key] # 构造启动命令 launch_command = ["roslaunch", launch_file] + launch_args # 执行对应的.launch文件 running_process = subprocess.Popen(launch_command) # 监听按键事件 def on_press(key): global running_process if key.char == 'q': print("stop1") if running_process: running_process.terminate() running_process = None # 清除已结束的进程 print("stop2") return False # 停止监听器 try: if key.char == 's': print("按下 's' 键,程序停止") return False # 停止监听器 execute_launch(key.char) except AttributeError: # 按下的不是字符键,忽略 pass with Listener(on_press=on_press) as listener: listener.join()