Library and XAML control for keyboard ⌨ shortcuts customization.
Try it in Video Detail Player or in the sample app.
(See the sample project in this repo for details)
-
Add the package to your UWP project:
Install-Package CustomAccelerators
-
Add reference in XAML:
<Page ... xmlns:ca="using:CustomAccelerators">
-
Use attached property to add custom accelerator
<Button Content="Hello" ca:Extension.Identity="HelloCa" /> <!--this will also update the tooltip of the button-->
-
Add definitions of accelerators somewhere (e.g. in
OnNavigatedTo
of the Page).
var acceleratorsList = new List<(string identity, string label, VirtualKey key, VirtualKeyModifiers modifiers)>()
{
("HelloCa","I am Hello Button",VirtualKey.H,VirtualKeyModifiers.Control|VirtualKeyModifiers.Shift),
("Another Accelerator","",VirtualKey.PageDown,VirtualKeyModifiers.None)
};
AcceleratorsManager.AddDefaultsAndLoadFromStorage(acceleratorsList);
- Add
CustomAcceleratorsEditControl
somewhere in your xaml.
<ca:CustomAcceleratorsEditControl />
<!--this control allows to edit keybord shortcuts-->
-
You can also set accelerator without using attached properties:
<Button Content="Hello" > <Button.KeyboardAccelerators> <ca:CustomAccelerator Identity="HelloCa"/> </Button.KeyboardAccelerators> </Button> <!--this will NOT automaticaly update the tooltip of the button-->
-
You can enable/disable the accelerator through the attached property:
<Button ca:Extension.Identity="SimpleAccelerator" ca:Extension.IsEnabled="True"/>
-
Attached property has one great feature - it changes tooltip of button (and appbarButton) when accelerator is changed (this behaviour is not supported with basic accelerators).
-
Why is it neccessary to set accelerators somewhere in C# code?
- Main reason is necessity to reach all accelerators in one place (for edit control). Accelerators from all pages, event those, that aren't loaded yet.
-
Why set the accelerators in
OnNavigatedTo
method?- It's not requisite. You can set this also in
OnLaunched
ofApp.xaml.cs
, but note there are some IO operations (loading settings) that may delay the startup.
- It's not requisite. You can set this also in
-
It saves the shortcuts persistently (using local settings).
-
You can't edit the appearance of
CustomAcceleratorsEditControl
(yet). -
Dont't set
Key
,Modifiers
orLabel
in xaml. Only within definitions. -
It doesn't check if there are more action with same shortcut. In such case, just one action will be invoked.
-
There are cases when you want to use two accelerator that invoke same action. No problem.
- It's primary made for this special
CommandBar
bug and its workarround.
- It's primary made for this special
-
What about localization?
- You can do it this way:
var rl = ResourceLoader.GetForCurrentView(); acceleratorsList = acceleratorsList.Select(x => { x.label = rl.GetString(x.identity);return x; }).ToList();
- The ResourceExtractor is also prepared for this and will automatically modify the
.resw
with Identities and Labels.
-
It's not perfect, I know 🙃 . If you have anything to say about it, don't be shy and open an issue.