keyboard_shortcuts

//

Types

Key represents a keyboard key, this can be used to be a keyboard combination. The “Modifier” key represents a modifier key, which is the Cmd key on macOS and the Ctrl key on other platforms.

For example the typical Undo key combination is “Cmd + Z” on macOS and “Ctrl + Z” on other platforms, this is [Modifier, Key(“Z”)], while redo can be represented as [Modifier, Shift, Key(“Z”)].

For more information about the “Key” and “Code” types, see the documentation: https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event

Note that only one of Key or Code should be used in a combination, if both are used and either of them matches it will trigger the message. Either Key or Code should be present to trigger a message. For binding some edge cases like the shift key itself, use: [Modifier, Shift, Key(“shift”)]

The Key and Code fields are case insensitive.

pub type Key {
  Key(String)
  Code(String)
  Modifier
  Shift
  Alt
}

Constructors

  • Key(String)
  • Code(String)
  • Modifier
  • Shift
  • Alt

KeyEventType represents the type of keyboard event.

pub type KeyEventType {
  KeyDown
  KeyUp
}

Constructors

  • KeyDown
  • KeyUp

Shortcut represents a keyboard shortcut, this can be used to bind a keyboard combination to a message. The combination is a list of keys including potential modifiers.

pub type Shortcut(msg) {
  Shortcut(
    combination: List(Key),
    msg: msg,
    options: List(ShortcutOption),
  )
}

Constructors

The Exclude option can be used to specify the target elements that the shortcut should not be applied to. For example input elements may need to be excluded from the shortcut. Note that unlike other elements, input elements can also be handled in gleam/lustre with the lustre event.on_keydown like functions.

The ExcludeInputElements excludes “INPUT”, “TEXTAREA” and “SELECT”.

The PreventDefault option can be used to prevent the default action of the event when the shortcut is triggered, for example preventing the browser standard Undo shortcut from also triggering. NOTE: it seems that on Safari the PreventDefault option does not work as expected for the “Refresh” (⌘R on Mac) shortcut, but does work for other keys such as the Undo (⌘Z on Mac) shortcut.

pub type ShortcutOption {
  PreventDefault
  Exclude(String)
  ExcludeInputElements
}

Constructors

  • PreventDefault
  • Exclude(String)
  • ExcludeInputElements

Values

pub fn install_keyboard_shortcuts(
  dispatch: fn(msg) -> Nil,
  event_type: KeyEventType,
  shortcuts: List(Shortcut(msg)),
) -> Nil
pub fn is_mac() -> Bool

Returns true if the current platform is macOS.

pub fn modifier_key() -> String

Returns the modifier key for the current platform. For macOS, it returns “meta”, and for other platforms, it returns “ctrl”.

pub fn modifier_name() -> String

Returns the modifier name for the current platform. For macOS, it returns “Cmd”, and for other platforms, it returns “Ctrl”.

pub fn modifier_symbol() -> String

Returns the modifier symbol for the current platform. For macOS, it returns “⌘”, and for other platforms, it returns “Ctrl”.

Search Document