Skip to content

Disable "Home" and "End" keys for selecting items in list #293

Closed

Description

@tobiasSchaathun

Hello, is there a way to disable that the "Home" and "End" jump to the first and last index of the item list? I want to use it to do this in the input and wish to disable this behaviour for the items themselves.

Activity

  1. tobiasSchaathun commented on Aug 5, 2024

    @tobiasSchaathun
    Author

    I figured it out, leaving it here if anyone is interested i the answer in the future....

    You can add an "onKeyDown" to the Command itself and use event.preventDefault to achieve this :)

  2. skirsten commented on Sep 7, 2024

    @skirsten

    Hi, I am running into the same issue. The problem is that with event.preventDefault the input will also not handle the event.

    What I want is the default functionality: Home goes to beginning of the input text and End to the end of the text. Unfortunately this seems not possible to achieve looking at the code: https://github.com/pacocoursey/cmdk/blob/92344c58a63b995f6eb149245c56ea548540dd18/cmdk/src/index.tsx#L571-L575

  3. kevin-lonestone commented on Nov 5, 2024

    @kevin-lonestone

    This workaround worked for me: stop the keydown event from propagating from the input.

    <Command.Input
      [...]
      onKeyDown={(event) => {
        switch (event.key) {
          // NOTE: prevents unwanted cmdk behaviour
          case 'Home':
          case 'End':
            event.stopPropagation()
            break
        }
      }}
    />
  4. skirsten commented on Nov 5, 2024

    @skirsten

    @kevin-lonestone Awesome workaround. Works perfectly. Thanks a lot! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions