Search code examples
wpfcaliburn.microkey-bindings

How does one implement application keyboard shortcuts in Caliburn.Micro


In larger applications, there can be multiple dozens of keyboard shortcuts. Users tend to expect to be able to remap these, similar to the Options/Environment/Keyboard section in Visual Studio 2013. Creating the form is not really the challenge, nor is individual key bindings.

What I want to know is how to leverage Caliburn.Micro to set these for the application, window, and sometimes the control. If Caliburn.Micro doesn't provide a direct mechanism, what can I nuget, implement, or learn from that does something like this and remain compatible with Caliburn.Micro?

Thanks David


Solution

  • Not sure if this is what you're looking for but Caliburn.Micro does have a sample to create key bindings in WPF, it's available on Github.

    The important parts to see are in the Bootstrapper where it adds custom code for a new sort trigger and then in ShellView.xaml using that event.

    This should be enough to create key bindings are the appropriate scope like you mentioned.

    What you're looking for this more complicated than this (especially around the remapping) sample but should help you on your way.

    Update: I would look at creating a dictionary mapping "command name" to a key combination. Then rather than modifying the parser to create a key trigger have a custom trigger than takes the command name as input.

    That way you could modify the dictionary from user settings but not have to modify your xaml. Something like

    <Window cm:Message.Attach="[Key NewProject] = [NewProject]" />
    

    Where in your custom key trigger something maps "New Project" to "Ctrl + N".