UWP 响应键盘组合快捷键

方法1:响应Ctrl+?快捷键

首先在load事件或者keydown事件内注册事件

        public MainPage()
        {
            this.InitializeComponent();
            // Register for accelerator key events used for button hotkeys
            Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
        }

 

注册的事件如下(检测Ctrl+V和Ctrl+N):

private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
    if (args.EventType.ToString().Contains("Down"))
    {
        var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
        if (ctrl.HasFlag(CoreVirtualKeyStates.Down))
        {
            switch (args.VirtualKey)
            {
                case VirtualKey.V:
                    ViewOrders_Tapped(this, null);
                    break;
                case VirtualKey.N:
                    NewOrder_Tapped(this, null);
                    break;
            }
        }
    }
}

 

 

 

方法2:响应Ctrl+鼠标滚轮

private void WheelChanged(object sender, PointerRoutedEventArgs e)
{
    RichEditBox editor = sender as RichEditBox;
    var x = e.GetCurrentPoint(editor).Properties.MouseWheelDelta;
    var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
    if (ctrl.HasFlag(CoreVirtualKeyStates.Down))
    {
        if (x > 0)
        {
            redit.Document.Selection.CharacterFormat.Size += 1;
        }
        else
        {
            redit.Document.Selection.CharacterFormat.Size -= 1;
        }
        e.Handled = true;//取消内容滚动
    }
}

说明:所有UI元素都具有PointerWheelChanged事件,响应此事件同时判断Ctrl状态判断用户操作。例子中响应的UIElement元素为RichEditBox,根据实际情况更改。

 

 

参考:http://www.songshizhao.com/blog/blogPage/405.html

 

    原文作者:星期八再娶你
    原文地址: https://www.cnblogs.com/hupo376787/p/8890589.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞