Shortcut
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
Functions
Enables combination of ModifierShortcuts like "STRG + ALT + F":