WPF Menu实现快捷键操作

很多小伙伴说,在Menu中,实现单个快捷键操作很简单,怎么实现多个快捷键操作和,组合快捷键呢,今天他来了。

上代码和效果图

一、Ctrl + Shift + 任意子母键实现快捷键组合

<Window x:Class="XH.TemplateLesson.MenuWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:XH.TemplateLesson"
        mc:Ignorable="d"
        Title="MenuWindow" Height="450" Width="800">
 <Window.InputBindings>
   <!-- 定义组合快捷键 Ctrl+Shift+S -->
   <!--先要什么组合修改key 或者 Modifiers 即可
     注意:这里只可以改为Ctrl Alt Shift Windows + 任意字母键的快捷方式-->
   <KeyBinding Key="S" Modifiers="Control+Shift" Command="{Binding NewCommand}" />
 </Window.InputBindings>
 <Grid>
   <Menu>
    <MenuItem Header="新建(_F)">
        <MenuItem Header="新建" />
        <MenuItem Header="全部保存" Command="{Binding NewCommand}" InputGestureText="Ctrl+Shift+S"  />
    </MenuItem>
    <MenuItem Header="编辑(_E)"/>
  </Menu>
  </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XH.TemplateLesson
{
    /// <summary>
    /// MenuWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MenuWindow : Window
    {
        public ICommand NewCommand { get; set; }
        public MenuWindow()
        {
            InitializeComponent();
            DataContext = this;
            NewCommand = new RelayCommand(ExecuteNew);
        }
        private void ExecuteNew(object parameter)
        {
            MessageBox.Show("New Command Executed");
        }


        public class RelayCommand : ICommand
        {
            private readonly Action<object> _execute;
            private readonly Func<object, bool> _canExecute;

            public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
            {
                _execute = execute;
                _canExecute = canExecute;
            }

            public bool CanExecute(object parameter)
            {
                return _canExecute == null || _canExecute(parameter);
            }

            public void Execute(object parameter)
            {
                _execute(parameter);
            }

            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
        }
    }
}

直接Ctrl + Shift + S弹出:

二、实现Ctrl + 多个字母键组合

<Window x:Class="XH.TemplateLesson.MenuWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:XH.TemplateLesson"
        mc:Ignorable="d" 
        <!--绑定键盘事件-->
        PreviewKeyDown="Window_PreviewKeyDown"
        PreviewKeyUp="Window_PreviewKeyUp"
        Title="MenuWindow" Height="450" Width="800">
  <Grid>
    <Menu>
      <MenuItem Header="新建(_F)">
          <MenuItem Header="新建" />
          <MenuItem Header="整理代码格式" InputGestureText="Ctrl+K+D"  />
      </MenuItem>
    </Menu>
  </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XH.TemplateLesson
{
    /// <summary>
    /// MenuWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MenuWindow : Window
    {
        private bool _isCtrlPressed;
        private bool _isAPressed;
        public MenuWindow()
        {
            InitializeComponent();
        }
        // 下方的K 和D 根据自己写的代码来进行修改
        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            // 先 Cttrl
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
                _isCtrlPressed = true;
            }
            // 再 Ctrl + K 
            else if (e.Key == Key.K && _isCtrlPressed)
            {
                _isAPressed = true;
            }
            // 最后 Cttrl + K + D
            else if (e.Key == Key.D && _isCtrlPressed && _isAPressed)
            {
                ExecuteCustomCommand();
            }
        }
        // 键盘释放 如果释放 无效
        private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
                _isCtrlPressed = false;
            }
            else if (e.Key == Key.K)
            {
                _isAPressed = false;
            }
        }

        private void ExecuteCustomCommand()
        {
            MessageBox.Show("快捷键调用成功: Ctrl + K + D");
        }
    }
}

按下Ctrl + K + D:

单键 Alt + 字母弹出

<Menu>
  <!--直接下划线 + 字母 就可以alt + 字母弹出-->
  <MenuItem Header="新建(_F)">
    <MenuItem Header="新建" />
    <MenuItem Header="整理代码格式" InputGestureText="Ctrl+K+D"  />
  </MenuItem>
</Menu>

演示 :

好了,以上就是WPF中Menu控件中,所有快捷键的使用方法了,感觉有用的话,点个赞呗。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值