Modifier Keys (alt, ctrl shift)

Using CTRL, ALT, and SHIFT in your tools

Sometimes we may want fancier behaviors in our tools / UIs depending on whether Alt, Ctrl, or Shift are held.

Unreal does provide access to these in their API on the InputLibrary module:

is_shift_down = unreal.InputLibrary.modifier_keys_state_is_shift_down(
    unreal.InputLibrary.get_modifier_keys_state()
)
is_alt_down = unreal.InputLibrary.modifier_keys_state_is_alt_down(
    unreal.InputLibrary.get_modifier_keys_state()
)
is_ctrl_down = unreal.InputLibrary.modifier_keys_state_is_control_down(
    unreal.InputLibrary.get_modifier_keys_state()
)

This gives us easier access to the modifier keys, simplifying our Python requirements and ease of us:

EditorActorSubsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)

selection = set()


def update_selection():
    """Set or update the current selection"""
    global selection 
    new_selection = EditorActorSubsystem.get_selected_level_actors() or []
    is_shift_down = unreal.InputLibrary.modifier_keys_state_is_shift_down(
        unreal.InputLibrary.get_modifier_keys_state()
    )
    
    # If shift is down, add to the selection
    # otherwise, replace the selection
    if is_shift_down:
        selection |= set(new_selection)
    else:
        selection = set(new_selection)
    
    print("Current Selection:")
    for actor in selection:
        print(f"\t{actor.get_actor_label()}")

Not really groundbreaking, but there's a lot of neat little things in the Python Docs to discover

Last updated