Primary APIs:
ICommandSourceButton.Command and MenuItem.CommandButton.HotKey and MenuItem.HotKeyHotKeyManagerInputElement.KeyBindingsKeyBindingKeyGestureGesturesMenuItem.InputGestureImportant members:
ICommandSource.Command, CommandParameterHotKeyManager.HotKeyProperty, SetHotKey(...), GetHotKey(...)InputElement.KeyBindings listKeyBinding.Gesture, Command, CommandParameter, TryHandle(...)KeyBinding.GestureProperty, KeyBinding.CommandParameterPropertyKeyGesture.Parse(...), Matches(...), ToString(...)KeyGesture.KeyModifiersGestures.AddTappedHandler(...), AddHoldingHandler(...), AddPinchHandler(...)Reference source files:
src/Avalonia.Base/Input/ICommandSource.cssrc/Avalonia.Controls/Button.cssrc/Avalonia.Controls/MenuItem.cssrc/Avalonia.Controls/HotkeyManager.cssrc/Avalonia.Base/Input/InputElement.cssrc/Avalonia.Base/Input/KeyBinding.cssrc/Avalonia.Base/Input/KeyGesture.cssrc/Avalonia.Base/Input/Gestures.csArchitecture layers:
Button, MenuItem, custom ICommandSource).Click, key binding, hotkey, pointer gesture).CanExecute semantics.Use hotkeys for control-scoped shortcuts and KeyBindings for root-level or view-level shortcuts.
Property-system details that matter in custom input infrastructure:
KeyBinding.GestureProperty and KeyBinding.CommandParameterProperty are styled properties,KeyGesture.KeyModifiers is the canonical modifier set used by Matches(...).<Button Content="Save"
Command="{Binding SaveCommand}"
CommandParameter="{Binding CurrentDocument}" />
using Avalonia.Input;
var binding = new KeyBinding
{
Gesture = KeyGesture.Parse("Ctrl+S"),
Command = viewModel.SaveCommand
};
rootControl.KeyBindings.Add(binding);
<Button Content="Run"
Command="{Binding RunCommand}"
HotKey="Ctrl+R" />
using Avalonia.Input;
Gestures.AddTappedHandler(canvas, (_, e) => HandleTap(e));
Gestures.AddHoldingHandler(canvas, (_, e) => HandleHold(e));
<MenuItem Header="_Save"
Command="{Binding SaveCommand}"
InputGesture="Ctrl+S" />
InputGesture for menu display consistency, with actual command binding.PhysicalKey from KeyEventArgs instead of locale-dependent Key when required.ICommandSource nor IClickableControl.TopLevel.KeyGesture registered in both parent and child routes.CanExecute false or control not effectively enabled.Default mode:
XAML-first complete example:
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyApp.ViewModels"
x:DataType="vm:EditorViewModel">
<UserControl.KeyBindings>
<KeyBinding Gesture="Ctrl+S" Command="{CompiledBinding SaveCommand}" />
</UserControl.KeyBindings>
<StackPanel Spacing="8">
<Button Content="Save" Command="{CompiledBinding SaveCommand}" HotKey="Ctrl+S" />
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_Save"
InputGesture="Ctrl+S"
Command="{CompiledBinding SaveCommand}" />
</MenuItem>
</Menu>
</StackPanel>
</UserControl>
Code-only alternative (on request):
using Avalonia.Controls;
using Avalonia.Input;
var saveBinding = new KeyBinding
{
Gesture = KeyGesture.Parse("Ctrl+S"),
Command = viewModel.SaveCommand
};
root.KeyBindings.Add(saveBinding);
HotKeyManager.SetHotKey(saveButton, KeyGesture.Parse("Ctrl+S"));
Gestures.AddTappedHandler(canvas, (_, e) => viewModel.SelectAt(e.GetPosition(canvas)));