zoukankan      html  css  js  c++  java
  • WPF中设置快捷键

    方式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)
                {
                }

            }

  • 相关阅读:
    导出表格,导出表格为excel,导出表格带样式,怎么导出表格数据为excel并且带样式呢
    webpack打包文件 可以npm cdn使用
    Webpack的externals的使用
    如何在微信小程序中使用iconfont
    如何发布和取消发布 NPM 包?
    js中数组对象去重的方法
    小程序列表性能优化
    wepy全局拦截器
    js中prototype与__proto__的关系详解
    JavaScript中本地对象、内置对象和宿主对象
  • 原文地址:https://www.cnblogs.com/zhaolili/p/5133918.html
Copyright © 2011-2022 走看看