Class that handles keyboard input.
| 279 | |
| 280 | |
| 281 | class KeyboardControl(object): |
| 282 | """Class that handles keyboard input.""" |
| 283 | def __init__(self, world, start_in_autopilot): |
| 284 | self._autopilot_enabled = start_in_autopilot |
| 285 | if isinstance(world.player, carla.Vehicle): |
| 286 | self._control = carla.VehicleControl() |
| 287 | self._lights = carla.VehicleLightState.NONE |
| 288 | world.player.set_autopilot(self._autopilot_enabled) |
| 289 | world.player.set_light_state(self._lights) |
| 290 | elif isinstance(world.player, carla.Walker): |
| 291 | self._control = carla.WalkerControl() |
| 292 | self._autopilot_enabled = False |
| 293 | self._rotation = world.player.get_transform().rotation |
| 294 | else: |
| 295 | raise NotImplementedError("Actor type not supported") |
| 296 | self._steer_cache = 0.0 |
| 297 | world.hud.notification("Press 'H' or '?' for help.", seconds=4.0) |
| 298 | |
| 299 | def parse_events(self, client, world, clock): |
| 300 | if isinstance(self._control, carla.VehicleControl): |
| 301 | current_lights = self._lights |
| 302 | for event in pygame.event.get(): |
| 303 | if event.type == pygame.QUIT: |
| 304 | return True |
| 305 | elif event.type == pygame.KEYUP: |
| 306 | if self._is_quit_shortcut(event.key): |
| 307 | return True |
| 308 | elif event.key == K_BACKSPACE: |
| 309 | if self._autopilot_enabled: |
| 310 | world.player.set_autopilot(False) |
| 311 | world.restart() |
| 312 | world.player.set_autopilot(True) |
| 313 | else: |
| 314 | world.restart() |
| 315 | elif event.key == K_F1: |
| 316 | world.hud.toggle_info() |
| 317 | elif event.key == K_h or (event.key == K_SLASH and pygame.key.get_mods() & KMOD_SHIFT): |
| 318 | world.hud.help.toggle() |
| 319 | elif event.key == K_TAB: |
| 320 | world.camera_manager.toggle_camera() |
| 321 | elif event.key == K_c and pygame.key.get_mods() & KMOD_SHIFT: |
| 322 | world.next_weather(reverse=True) |
| 323 | elif event.key == K_c: |
| 324 | world.next_weather() |
| 325 | elif event.key == K_g: |
| 326 | world.toggle_radar() |
| 327 | elif event.key == K_BACKQUOTE: |
| 328 | world.camera_manager.next_sensor() |
| 329 | elif event.key == K_n: |
| 330 | world.camera_manager.next_sensor() |
| 331 | elif event.key > K_0 and event.key <= K_9: |
| 332 | world.camera_manager.set_sensor(event.key - 1 - K_0) |
| 333 | elif event.key == K_r and not (pygame.key.get_mods() & KMOD_CTRL): |
| 334 | world.camera_manager.toggle_recording() |
| 335 | elif event.key == K_r and (pygame.key.get_mods() & KMOD_CTRL): |
| 336 | if (world.recording_enabled): |
| 337 | client.stop_recorder() |
| 338 | world.recording_enabled = False |