Shortcut

data class Shortcut(val key: String, val ctrl: Boolean = false, val alt: Boolean = false, val shift: Boolean = false, val meta: Boolean = false) : ModifierShortcut

Shortcut represents a grouping type upon the "key" and the modifier key properties of a KeyboardEvent. More info here

A shortcut can be easily constructed by a KeyboardEvent which makes this abstraction so feasible to use with the keyboard event handling like:

div {
// raw usage (prefer next example!)
keydowns.map { Shortcut(it) } handledBy { /* use object for further processing */}

// use factory function to create a Shortcut object
keydowns.map { shortcutOf(it) } handledBy { /* use object for further processing */}

// combine with `filter` functions is a common pattern:
keydowns.filter { shortcutOf(it) == Keys.Control + "k") handledBy {
// only if combination was pressed and with access to the original event too!
// all other key events will be ignored
}
}

This class enables by its implementation of ModifierShortcut the concatenation with other modifiers, but it prevents the meaningless combination of shortcuts:

// this works:
Shortcut("F") + Keys.Alt + Keys.Shift
// this won't work:
Shortcut("F") + Shortcut("P")

// Ths first example could also be constructed by an appropriate constructor call:
Shortcut("F", alt = true, shift = true)

Be aware that the Shortcut.key property is case sensitive just likte the KeyboardEvent.key property. So in order to match a shortcut with a capital key of an event, you must the Shortcut.shift flag to true.

// A capital "K" should be matched, but would fail here:
keydowns.map { shortcutOf(it) == Shortcut("K") } // would emit `false`!
// Instead this will work:
keydowns.map { shortcutOf(it) == Shortcut("K", shift = true) }
// or with this operator an better readbility:
keydowns.map { shortcutOf(it) == Keys.Shift + "K" }

On the other hand there will be no matching for a lowercase key with shift property set to true either!

See also

Constructors

Link copied to clipboard
constructor(event: KeyboardEvent)
constructor(key: String, ctrl: Boolean = false, alt: Boolean = false, shift: Boolean = false, meta: Boolean = false)

Properties

Link copied to clipboard
open override val alt: Boolean = false
Link copied to clipboard
open override val ctrl: Boolean = false
Link copied to clipboard
val key: String
Link copied to clipboard
open override val meta: Boolean = false
Link copied to clipboard
open override val shift: Boolean = false

Functions

Link copied to clipboard
open operator fun plus(other: Shortcut): Shortcut

This operator function enables the concatenation with a Shortcut:

open operator fun plus(other: String): Shortcut

This operator function enables the concatenation with simply a String to enable a nice readable keyboard combination:

operator fun plus(other: ModifierShortcut): Shortcut

This operator function enables the concatenation with additional modifier shortcuts:

Link copied to clipboard

Enables combination of ModifierShortcuts like "STRG + ALT + F":