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

相关推荐
神仙别闹1 小时前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
向宇it2 小时前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
斯是 陋室11 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
inwith13 小时前
C#语法基础总结(超级全面)(二)
开发语言·c#
ヾChen16 小时前
13届蓝桥杯省赛程序设计试题
物联网·学习·蓝桥杯·c#
我是唐青枫21 小时前
C#.NET 泛型详解
开发语言·c#·.net
Yasin Chen21 小时前
C# StringBuilder源码分析
开发语言·c#
格林威1 天前
Baumer工业相机堡盟工业相机如何通过YoloV8模型实现人物识别(C#)
开发语言·人工智能·数码相机·yolo·计算机视觉·c#
Rabbb1 天前
C# JSON 反序列化时,忽略转换失败的属性 JTokenSafeToExtensions
后端·c#·json
三目条件1 天前
C#将类属性保存到Ini文件方法(利用拓展方法,反射方式获取到分组名和属性名称属性值)
java·开发语言·c#