CharacterActivator class

A shortcut combination that is triggered by a key event that produces a specific character.

Keys often produce different characters when combined with modifiers. For example, it might be helpful for the user to bring up a help menu by pressing the question mark ('?'). However, there is no logical key that directly represents a question mark. Although 'Shift+Slash' produces a '?' character on a US keyboard, its logical key is still considered a Slash key, and hard-coding 'Shift+Slash' in this situation is unfriendly to other keyboard layouts.

For example, CharacterActivator('?') is triggered when a key combination results in a question mark, which is 'Shift+Slash' on a US keyboard, but 'Shift+Comma' on a French keyboard.

In the following example, when a key combination results in a question mark, the SnackBar gets shown:
link

To create a local project with this code sample, run:
flutter create --sample=widgets.CharacterActivator.1 mysample

The alt, control, and meta flags represent whether the respective modifier keys should be held (true) or released (false). They default to false. CharacterActivator cannot check shifted keys, since the Shift key affects the resulting character, and will accept whether either of the Shift keys are pressed or not, as long as the key event produces the correct character.

By default, the activator is checked on all KeyDownEvent or KeyRepeatEvents for the character in combination with the requested modifier keys. If includeRepeats is false, only the character events with that are KeyDownEvents will be considered.

On macOS and iOS, the alt flag indicates that the Option key (⌥) is pressed. Because the Option key affects the character generated on these platforms, it can be unintuitive to define CharacterActivators for them.

For instance, if you want the shortcut to trigger when Option+s (⌥-s) is pressed, and what you intend is to trigger whenever the character 'ß' is produced, you would use CharacterActivator('ß') or CharacterActivator('ß', alt: true) instead of CharacterActivator('s', alt: true). This is because CharacterActivator('s', alt: true) will never trigger, since the 's' character can't be produced when the Option key is held down.

If what is intended is that the shortcut is triggered when Option+s (⌥-s) is pressed, regardless of which character is produced, it is better to use SingleActivator, as in SingleActivator(LogicalKeyboardKey.keyS, alt: true).

See also:

  • SingleActivator, an activator that represents a single key combined with modifiers, such as Ctrl+C or Ctrl-Right Arrow.
Implemented types
Mixed in types

Constructors

CharacterActivator(String character, {bool alt = false, bool control = false, bool meta = false, bool includeRepeats = true})
Triggered when the key event yields the given character.
const

Properties

alt bool
Whether either (or both) Alt keys should be held for the character to activate the shortcut.
final
character String
The character which triggers the shortcut.
final
control bool
Whether either (or both) Control keys should be held for the character to activate the shortcut.
final
hashCode int
The hash code for this object.
no setterinherited
includeRepeats bool
Whether this activator accepts repeat events of the character.
final
meta bool
Whether either (or both) Meta keys should be held for the character to activate the shortcut.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
triggers Iterable<LogicalKeyboardKey>?
An optional property to provide all the keys that might be the final event to trigger this shortcut.
no setteroverride

Methods

accepts(KeyEvent event, HardwareKeyboard state) bool
Whether the triggering event and the keyboard state at the time of the event meet required conditions, providing that the event is a triggering event.
override
debugDescribeKeys() String
Returns a description of the key set that is short and readable.
override
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
serializeForMenu() ShortcutSerialization
Implement this in a ShortcutActivator subclass to allow it to be serialized for use in a PlatformMenuBar.
override
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringShort() String
A brief description of this object, usually just the runtimeType and the hashCode.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited