Skip to content

handleKeyboardEvent

ts
function LSM.handleKeyboardEvent<T>(
    lsm: LSM<T>,
    evt: {
        ctrlKey: boolean;
        key: string;
        metaKey: boolean;
        shiftKey: boolean;
    },
    options?: {
        multiselect: boolean;
    }
): [boolean, LSM<T>];

Handles keyboard navigation events and returns a tuple of [handled, newState].

Supported Keys

KeyModifierAction
ArrowDown-goToNext
ArrowDownShiftselectNext
ArrowDownMetagoToBottom
ArrowDownMeta+ShiftselectToBottom
ArrowUp-goToPrev
ArrowUpShiftselectPrev
ArrowUpMetagoToTop
ArrowUpMeta+ShiftselectToTop
Home-goToTop
HomeShiftselectToTop
End-goToBottom
EndShiftselectToBottom

Options

  • multiselect - If false, disables shift-based selection extensions. Default: true

Example

ts
import { LSM } from "@monstermann/lsm";

let lsm = LSM.create(["a", "b", "c"]);

const [handled, newLsm] = LSM.handleKeyboardEvent(lsm, {
    key: "ArrowDown",
    shiftKey: false,
    metaKey: false,
    ctrlKey: false,
});

handled; // true
newLsm.selected; // ["a"]