Keyboard shortcuts response in UWP Apps

A good Windows application is not only for the mouse, touch screen users, but also for keyboard operations, especially some common shortcuts, such as undo (CTRL + Z), to provide a comprehensive user experience.

So how do you define keyboard shortcuts in UWP, in my "tracing" app, I need to implement Undo shortcuts (CTRL + Z). First, we need to define the operation of the shortcut key, which is the user's current focus on which control, if you need the entire application interface to listen to keyboard operation, then this UI control is the XAML page outermost container: GRID

So first we define 2 events on the first level grid, the outermost container, below the page: And, as follows:KeyDownKeyUp

<Grid Background="{ThemeResource TracingAcrylicBrush}" KeyDown="LayoutRoot_KeyDown" KeyUp="LayoutRoot_KeyUp">

Next, because the shortcut key (CTRL + Z) We want to manipulate is a key combination, we need to know whether the user presses the CTRL key and the Z key at the same time, so we define a Boolean variable to identify the state in which CTRL is pressed:

private bool _isCtrlKeyPressed;

Then define the event handler function:

private void LayoutRoot_KeyUp(object sender, KeyRoutedEventArgs e)

and

private async void LayoutRoot_KeyDown(object sender, KeyRoutedEventArgs e)

The argument used to determine which key the user is pressing is. The key is represented by this enumeration. So to determine whether to press CTRL, just judge. So the logic for recording the CTRL key state is that when you press CTRL, the assignment is true, otherwise (releasing the key) is false:KeyRoutedEventArgs eWindows.System.VirtualKeye.Key == VirtualKey.Control_isCtrlKeyPressed

private void LayoutRoot_KeyUp(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Control) _isCtrlKeyPressed = false;
}

private async void LayoutRoot_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Control) _isCtrlKeyPressed = true;
}

The next step is to determine whether the user has pressed the Z key, also in the Layoutroot_keydown event handler, to determine if CTRL has been pressed, and then to Judge E. Key is not Z, like this:

private async void LayoutRoot_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == VirtualKey.Control) _isCtrlKeyPressed = true;
    else if (_isCtrlKeyPressed)
    {
        switch (e.Key)
        {
            case VirtualKey.Z: ViewModel.CommandUndo.Execute(null); break;
        }
    }
}

Now your application interface is ready to respond to the CTRL+Z key combination.

Similarly, if you have other shortcuts that you use with CTRL, you can also put them together in Swtich case:

case VirtualKey.V: await PasteInkOrImageFromClipboard(); break;
case VirtualKey.Z: ViewModel.CommandUndo.Execute(null); break;
case VirtualKey.Y: ViewModel.CommandRedo.Execute(null); break;
case VirtualKey.S: ViewModel.CommandSaveCurrent.Execute(null); break;

© 2018 - Tamilan Subramani
Design and Developed by TAMILS1809
Version 1.0.0 CV:feb201827

Follow Me