Search code examples
windows-runtimewindows-store-apps

How do I create a keyboard shortcut (like Ctrl+C) in a Windows Store app


I want to create keyboard shortcuts in my application, for instance Ctrl+C for copy. Also I want to ignore that shortcut if a TextBox has focus.


Solution

  • First you need a way to check if control key is pressed. CoreWindow.GetKeyState will do the job but might be a little bit tricky to use so I created an helper class:

    public class ModifierKeys
    {
        #region ShiftIsDown property
    
        public static bool ShiftIsDown 
        { 
            get
            {
                return (Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift) & CoreVirtualKeyStates.Down) != 0;
            }
        }
    
        public static bool OnlyShiftIsDown 
        { 
            get
            {
                return ShiftIsDown && !AltIsDown && !ControlIsDown;
            }
        }
    
        #endregion
    
        #region AltIsDown property
    
        public static bool AltIsDown
        {
            get
            {
                return (Window.Current.CoreWindow.GetKeyState(VirtualKey.Menu) & CoreVirtualKeyStates.Down) != 0;
            }
        }
    
        public static bool OnlyAltIsDown
        {
            get
            {
                return !ShiftIsDown && AltIsDown && !ControlIsDown;
            }
        }
    
        #endregion
    
        #region ControlIsDown property
    
        public static bool ControlIsDown
        {
            get
            {
                return (Window.Current.CoreWindow.GetKeyState(VirtualKey.Control) & CoreVirtualKeyStates.Down) != 0;
            }
    
        }
    
        public static bool OnlyControlIsDown
        {
            get
            {
                return !ShiftIsDown && !AltIsDown && ControlIsDown;
            }
        }
    
        #endregion
    
        #region NoModifierKeyIsDown property
    
        public static bool NoModifierKeyIsDown
        {
            get
            {
                return !ShiftIsDown && !AltIsDown && !ControlIsDown;
            }
        }
    
        #endregion
    }
    

    Now in OnNavigateTo/From in your page subscribe/unsubscribe on key down events:

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            /*...*/
            Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
        }
    
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            /*...*/
            Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
        }
    

    The CoreWindow_KeyDown will then looks something like this:

    void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
    {
        var focusedElement = FocusManager.GetFocusedElement();
    
        if (args.KeyStatus.WasKeyDown == false && ModifierKeys.OnlyControlIsDown &&
            !(focusedElement is TextBox)
            )
        {
                if (args.VirtualKey == VirtualKey.X)
                {
                    /*...cut...*/
                }
                else if (args.VirtualKey == VirtualKey.V)
                {
                    /*...paste...*/
                }
                else if (args.VirtualKey == VirtualKey.C)
                {
                    /*...copy...*/
                }
        }
    }