Fork me on GitHub

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

 

posted @ 2018-04-20 13:55  猫叔Vincent  阅读(1205)  评论(0编辑  收藏  举报