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控件中,所有快捷键的使用方法了,感觉有用的话,点个赞呗。

相关推荐
吃饭只吃七分饱30 分钟前
arm开发板通信
arm开发·c#
Hellc0073 小时前
什么是 WebApiEngine?
c#
dangoxiba4 小时前
【Unity学习心得】如何使用Unity制作“饥荒”风格的俯视角2.5D游戏
游戏·unity·c#·游戏引擎
咩咩觉主4 小时前
en造数据结构与算法C# 群组行为优化 和 头鸟控制
开发语言·c#
一丝晨光5 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符
friklogff5 小时前
【C#生态园】从图像到视觉:Emgu.CV、AForge.NET、OpenCvSharp 全面解析
开发语言·c#·.net
△曉風殘月〆7 小时前
WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
friklogff7 小时前
【C#生态园】构建你的C#操作系统:框架选择与实践
服务器·开发语言·c#
code bean10 小时前
【C#基础】函数传参大总结
服务器·开发语言·c#
shanshan209911 小时前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机