Skip to content

Keyboard Controls

Welcome to CARLA manual control.

Use ARROWS or WASD keys for control.

W            : throttle
S            : brake
A/D          : steer left/right
Q            : toggle reverse
Space        : hand-brake
P            : toggle autopilot
M            : toggle manual transmission
,/.          : gear up/down

L            : toggle next light type
SHIFT + L    : toggle high beam
Z/X          : toggle right/left blinker
I            : toggle interior light

TAB          : change sensor position
[1-9]        : change to sensor [1-9]
G            : toggle radar visualization

R            : toggle recording images to disk

CTRL + R     : toggle recording of simulation (replacing any previous)

F1           : toggle HUD
ESC          : quit

KeyboardControl ¤

Class that handles keyboard input.

__init__(self, world, carla_setting) special ¤

Source code in carla_client/util/keyboard_control.py
def __init__(self, world, carla_setting: CarlaConfig):
    self.logger = logging.getLogger(__name__)
    if carla_setting.print_keyboard_hint:
        print(__doc__)
        print()
    if isinstance(world.player, carla.Vehicle):
        self._control = carla.VehicleControl()
        self._lights = carla.VehicleLightState.NONE
        world.player.set_light_state(self._lights)
    elif isinstance(world.player, carla.Walker):
        self._control = carla.WalkerControl()
        self._rotation = world.player.get_transform().rotation
    else:
        raise NotImplementedError("Actor type not supported")
    self._steer_cache = 0.0
    self.logger.debug("Keyboard Control initiated")

parse_events(self, client, world, clock) ¤

Parse keyboard press. :param client: carla.Client :param world: carla.Client :param clock: pygame clock :return: bool - True if should continue, aka no exit key was pressed control - carla.VehicleControl

Source code in carla_client/util/keyboard_control.py
def parse_events(self, client, world, clock) -> \
        Tuple[bool, carla.VehicleControl]:
    """
    Parse keyboard press.
    :param client: carla.Client
    :param world: carla.Client
    :param clock: pygame clock
    :return:
        bool - True if should continue, aka no exit key was pressed
        control - carla.VehicleControl
    """
    if isinstance(self._control, carla.VehicleControl):
        current_lights = self._lights
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return False, None
        elif event.type == pygame.KEYUP:
            if self._is_quit_shortcut(event.key):
                return False, None
            elif event.key == K_F1:
                world.hud.toggle_info()
            elif event.key == K_h or (
                event.key == K_SLASH and pygame.key.get_mods() & KMOD_SHIFT
            ):
                world.hud.help.toggle()
            elif event.key == K_TAB:
                world.camera_manager.toggle_camera()
            elif event.key == K_g:
                world.toggle_radar()
            elif event.key > K_0 and event.key <= K_9:
                world.camera_manager.set_sensor(event.key - 1 - K_0)
            elif event.key == K_r and not (pygame.key.get_mods() & KMOD_CTRL):
                world.camera_manager.toggle_recording()
            elif event.key == K_r:
                if world.recording_enabled:
                    client.stop_recorder()
                    world.recording_enabled = False
                    world.hud.notification("Recorder is OFF")
                else:
                    client.start_recorder("manual_recording.rec")
                    world.recording_enabled = True
                    world.hud.notification("Recorder is ON")

            if isinstance(self._control, carla.VehicleControl):
                if event.key == K_q:
                    self._control.gear = 1 if self._control.reverse else -1
                elif event.key == K_m:
                    self._control.manual_gear_shift = (
                        not self._control.manual_gear_shift
                    )
                    self._control.gear = world.player.get_control().gear
                    world.hud.notification(
                        "%s Transmission"
                        % (
                            "Manual"
                            if self._control.manual_gear_shift
                            else "Automatic"
                        )
                    )
                elif self._control.manual_gear_shift and event.key == K_COMMA:
                    self._control.gear = max(-1, self._control.gear - 1)
                elif self._control.manual_gear_shift and event.key == K_PERIOD:
                    self._control.gear = self._control.gear + 1
                elif event.key == K_l and pygame.key.get_mods() & KMOD_CTRL:
                    current_lights ^= carla.VehicleLightState.Special1
                elif event.key == K_l and pygame.key.get_mods() & KMOD_SHIFT:
                    current_lights ^= carla.VehicleLightState.HighBeam
                elif event.key == K_l:
                    # Use 'L' key to switch between lights:
                    # closed -> position -> low beam -> fog
                    if not self._lights & carla.VehicleLightState.Position:
                        world.hud.notification("Position lights")
                        current_lights |= carla.VehicleLightState.Position
                    else:
                        world.hud.notification("Low beam lights")
                        current_lights |= carla.VehicleLightState.LowBeam
                    if self._lights & carla.VehicleLightState.LowBeam:
                        world.hud.notification("Fog lights")
                        current_lights |= carla.VehicleLightState.Fog
                    if self._lights & carla.VehicleLightState.Fog:
                        world.hud.notification("Lights off")
                        current_lights ^= carla.VehicleLightState.Position
                        current_lights ^= carla.VehicleLightState.LowBeam
                        current_lights ^= carla.VehicleLightState.Fog
                elif event.key == K_i:
                    current_lights ^= carla.VehicleLightState.Interior
                elif event.key == K_z:
                    current_lights ^= carla.VehicleLightState.LeftBlinker
                elif event.key == K_x:
                    current_lights ^= carla.VehicleLightState.RightBlinker
    if isinstance(self._control, carla.VehicleControl):
        self._parse_vehicle_keys(pygame.key.get_pressed(), clock.get_time())
        self._control.reverse = self._control.gear < 0
        # Set automatic control-related vehicle lights
        if self._control.brake:
            current_lights |= carla.VehicleLightState.Brake
        else:  # Remove the Brake flag
            current_lights &= carla.VehicleLightState.All ^ carla.VehicleLightState.Brake
        if self._control.reverse:
            current_lights |= carla.VehicleLightState.Reverse
        else:  # Remove the Reverse flag
            current_lights &= carla.VehicleLightState.All ^ carla.VehicleLightState.Reverse
        if current_lights != self._lights:  # Change the light state only if necessary
            self._lights = current_lights
            world.player.set_light_state(carla.VehicleLightState(self._lights))
    elif isinstance(self._control, carla.WalkerControl):
        self._parse_walker_keys(pygame.key.get_pressed(), clock.get_time(), world)
    return True, self._control