博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF中设置快捷键
阅读量:4983 次
发布时间:2019-06-12

本文共 2840 字,大约阅读时间需要 9 分钟。

方式1:

窗体中加入资源

 <UserControl.Resources>

        <RoutedUICommand x:Key="Cut" Text="剪切" />
        <RoutedUICommand x:Key="Copy" Text="复制" />
        <RoutedUICommand x:Key="Paste" Text="粘贴" />
        <RoutedUICommand x:Key="Select" Text="全选" />
    </UserControl.Resources>
    <UserControl.InputBindings>
        <KeyBinding Gesture="Ctrl+X" Command="{StaticResource Cut}" />
        <KeyBinding Gesture="Ctrl+C" Command="{StaticResource Copy}" />
        <KeyBinding Gesture="Ctrl+V" Command="{StaticResource Paste}" />
    </UserControl.InputBindings>
    <UserControl.CommandBindings>
        <CommandBinding Command="{StaticResource Cut}" Executed="CommandBinding_Cut"></CommandBinding>
        <CommandBinding Command="{StaticResource Copy}" Executed="CommandBinding_Copy"></CommandBinding>
        <CommandBinding Command="{StaticResource Paste}" Executed="CommandBinding_Paste"></CommandBinding>
    </UserControl.CommandBindings>

 

其中:CommandBinding_Cut ,CommandBinding_Copy ,CommandBinding_Paste 是按下快捷键对用的事件操作

 private void CommandBinding_Cut(object sender, ExecutedRoutedEventArgs e)

 {
           
 }

方式2:

写控件或者窗体的KeyDown事件 PreviewKeyDown="Window_KeyDown"

 

private void Window_KeyDown(object sender, KeyEventArgs e)

        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    //搜索
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement == SearchTxt) Search_Click(SearchBtn, e);
                    //文件或者文件夹重命名
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox")
                    {
                        TextBox box = Keyboard.FocusedElement as TextBox;
                        FilesModel model = box.DataContext as FilesModel;
                        if (model != null) ReName_LostFocus(box, e);
                    }
                    Keyboard.ClearFocus();
                }
                //Ctrl+C 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.C))
                {
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox") return;
                    CommandBinding_Copy(null, null);
                }
                //Ctrl+X 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.X))
                {
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox") return;
                    CommandBinding_Cut(null, null);
                }
                //Ctrl+V 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.V))
                {
                    if (Keyboard.FocusedElement != null && Keyboard.FocusedElement.GetType().Name == "TextBox") return;
                    CommandBinding_Paste(null, null);
                }
                //Ctrl+A 全选
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl)) && e.KeyboardDevice.IsKeyDown(Key.A))
                {
                    SelectAllCheck.IsChecked = true;
                    SelectAll_Click(SelectAllCheck, e);
                }
                //Shift+D 删除
                if ((e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)) && e.KeyboardDevice.IsKeyDown(Key.Delete))
                {
                    DeleteBtn_Click(null, e);
                }
            }
            catch (Exception)
            {
            }
        }

 

转载于:https://www.cnblogs.com/zhaolili/p/5133918.html

你可能感兴趣的文章
【转】C#生成验证码
查看>>
Linux环境下JDK/Eclipse一键安装脚本
查看>>
HwUI,CMS管理系统模板,漂亮,简单,兼容好
查看>>
特意给我的轩写的小知识
查看>>
LibreOJ #2003. 「SDOI2017」新生舞会
查看>>
sublime text there are no packages available for installation 解决办法
查看>>
Piston Pump Manufacturers - Mobile Cartridge Piston Pump: Advantages
查看>>
我喜欢的几款不错的vim插件
查看>>
eclipse在ubuntu13.04下崩溃crash
查看>>
wpf 右键ListBox可编辑
查看>>
hihocoder offer收割编程练习赛11 C 岛屿3
查看>>
maven+springmvc项目启动时,request mapping not found……
查看>>
提高JetBrains软件的性能
查看>>
ASP.NET:MVC中文件上传与地址变化处理
查看>>
Python 单向链表、双向链表
查看>>
Arrays, Hashtables and Dictionaries
查看>>
JAVA1种C++3种继承方式
查看>>
C#中DataTable排序
查看>>
架构学习提炼笔记(二):架构设计的流程是什么?
查看>>
hive常见问题解决干货大全
查看>>