Caliburn: adding keyboard shortcuts
Posted by Siim on June 28th, 2012We are creating a desktop app in WFP and using Caliburn.Micro. One thing we needed was to support keyboard shortcuts for almost all the interactions that user can do. Some of them are used globally and some of them depend on the focused control.
When talking about global shortcuts I mean that they can be used anywhere in the current view, they don’t depend on the element in focus. But they are not "globally" global, meaning that when I change the main view that is displayed in the window, shortcuts should be dependent on that view.
Caliburn uses cal:Message.Attach style of syntax to attach different actions for UI events, which can be defined in the XAML. It uses simple string parser to create correct bindings based on the content and it can be easily extended. I needed only to add custom syntax for my shortcuts, like ‘Shortcut Ctrl+F’. I override the parser for that:
public static class ShortcutParser { public static bool CanParse(string triggerText) { return !string.IsNullOrWhiteSpace(triggerText) && triggerText.Contains("Shortcut"); } public static TriggerBase CreateTrigger(string triggerText) { var triggerDetail = triggerText .Replace("[", string.Empty) .Replace("]", string.Empty) .Replace("Shortcut", string.Empty) .Trim(); var modKeys = ModifierKeys.None; var allKeys = triggerDetail.Split('+'); var key = (Key)Enum.Parse(typeof(Key), allKeys.Last()); foreach (var modifierKey in allKeys.Take(allKeys.Count() - 1)) { modKeys |= (ModifierKeys)Enum.Parse(typeof(ModifierKeys), modifierKey); } var keyBinding = new KeyBinding(new InputBindingTrigger(), key, modKeys); var trigger = new InputBindingTrigger { InputBinding = keyBinding }; return trigger; } }
And it’s attached to caliburn as follows:
var currentParser = Parser.CreateTrigger; Parser.CreateTrigger = (target, triggerText) => ShortcutParser.CanParse(triggerText) ? ShortcutParser.CreateTrigger(triggerText) : currentParser(target, triggerText);
The InputBindingTrigger class is pretty much the same as in this StackOverflow post. I had to modify the OnAttached method, because there are different paths when choosing the element to attach the shortcut.
If element is Focusable, then I can directly associate trigger with target element (like textbox, button etc). But when element isn’t focusable, it means the shortcut is meant to be used globally so I must attach it to the window. When attaching trigger to a window, it means that it is accessible anywhere in the app, whichever view is currently active.
We used the approach where there was only one active view in the conductor and because we needed global shortcuts per active view, I needed to add some code to remove the shortcut binding when the view is unloaded (some other view is activated). So here is revised OnAttached method:
protected override void OnAttached() { if (InputBinding != null) { InputBinding.Command = this; if (AssociatedObject.Focusable) { AssociatedObject.InputBindings.Add(InputBinding); } else { Window window = null; AssociatedObject.Loaded += delegate { window = GetWindow(AssociatedObject); if (!window.InputBindings.Contains(InputBinding)) { window.InputBindings.Add(InputBinding); } }; AssociatedObject.Unloaded += delegate { window.InputBindings.Remove(InputBinding); }; } } base.OnAttached(); }
This allows us to add shortcut binding in XAML with caliburn’s short-style syntax, like:
<UserControl cal:Message.Attach="[Shortcut F6] = [Action SomeAction]; [Shortcut F11] = [Action SomeAction2]; [Shortcut Ctrl+F] = [Action GlobalSearch]"></UserControl> <TextBox cal:Message.Attach="[Shortcut Escape] = [Action Clear]" />
There’s only a one thing you need to be aware of. When attaching global shortcuts in caliburn’s way (remember, they are attached to the window), it means that it also uses guard methods to control the execution of the action. And when guard method prevents execution, caliburn by default disables the control associated with the action, in our case the whole window. Unfortunately I haven’t been able to find where to change that behavior, that it wouldn’t disable the control. Workaround is not to use guard methods with such cases and to rely on manual condition checking in actions.