Shortcut

data class Shortcut(key: String, ctrl: Boolean, alt: Boolean, shift: Boolean, meta: Boolean) : 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

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

Functions

plus
Link copied to clipboard
js
open operator fun plus(other: Shortcut): Shortcut
This operator function enables the concatenation with a Shortcut:
Keys.Alt + Shortcut("K")
open operator fun plus(other: String): Shortcut
This operator function enables the concatenation with simply a String to enable a nice readable keyboard combination:
Keys.Shift + "F"
Be aware that the Shortcut.key property is case sensitive just likte the KeyboardEvent.key property.
operator fun plus(other: ModifierShortcut): Shortcut
This operator function enables the concatenation with additional modifier shortcuts:
Shortcut("F") + Keys.Alt
// or even
Shortcut("F") + Keys.Alt + Keys.Shift
// will already result in a `Shortcut`

Properties

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