• Winform,Wpf快捷键


    1. 基类
        1 using System;
        2 using System.Collections.Generic;
        3 using System.Linq;
        4 using System.Text;
        5 using System.Windows.Forms;
        6 using System.Windows.Input;
        7 using WUtilitiesV01.Controls;
        8 
        9 namespace WUtilitiesV01.Services
       10 {
       11     public abstract class ShortcutKeyServiceBase
       12     {
       13         private List<ShortcutKeyArgs> _mListShorcutKey = new List<ShortcutKeyArgs>();
       14         List<ShortcutKeyArgs> _mEmptyList = new List<ShortcutKeyArgs>();
       15         protected ShortcutKeyServiceBase()
       16         {
       17             Enabled = true;
       18 
       19             mListShorcutKey.Add(new ShortcutKeyArgs("快捷键列表", ModifierEnum.Control | ModifierEnum.Alt, KeyEnum.H, args =>
       20             {
       21                 MsgForm.ShowDialog("共计 {0} 个快捷键".ExtFormat(_mListShorcutKey.Count)
       22                                    + Environment.NewLine
       23                                    + string.Join(Environment.NewLine, _mListShorcutKey));
       24             }));
       25         }
       26 
       27 
       28         protected List<ShortcutKeyArgs> mListShorcutKey
       29         {
       30             get
       31             {
       32                 if (Enabled)
       33                 {
       34                     return _mListShorcutKey;
       35                 }
       36                 else
       37                 {
       38                     return _mEmptyList;
       39                 }
       40             }
       41             set { _mListShorcutKey = value; }
       42         }
       43 
       44         /// <summary>
       45         /// 注册单键,如F1,Esc等
       46         /// </summary>
       47         /// <param name="name"></param>
       48         /// <param name="key"></param>
       49         /// <param name="action"></param>
       50         public virtual void RegistSingleKey(string name, KeyEnum key, Action<ShortcutKeyArgs> action)
       51         {
       52             mListShorcutKey.Add(new ShortcutKeyArgs(name, ModifierEnum.None, key, action));
       53         }
       54 
       55         public virtual void RegistCombineKey(string name, ModifierEnum modifiers, KeyEnum key, Action<ShortcutKeyArgs> action)
       56         {
       57             mListShorcutKey.Add(new ShortcutKeyArgs(name, modifiers, key, action));
       58         }
       59 
       60         public virtual bool Enabled { get; set; }
       61     }
       62 
       63     [Flags]
       64     public enum ModifierEnum
       65     {
       66         None, Control = 0x1, Shift = 0x2, Alt = 0x4,
       67         Control_Shift = Control | Shift,
       68         Control_Alt = Control | Alt,
       69         Control_Shift_Alt = Control | Shift | Alt,
       70         Shift_Alt = Shift | Alt
       71     }
       72 
       73     /// <summary>
       74     /// 从WinForm key中来,名称为Form和Wpf中相同的项。值为Form中Key的值
       75     /// </summary>
       76     public enum KeyEnum
       77     {
       78         None = 0,
       79         Cancel = 3,
       80         Back = 8,
       81         Tab = 9,
       82         LineFeed = 10,
       83         Clear = 12,
       84         Enter = 13,
       85         Return = 13,
       86         Pause = 19,
       87         CapsLock = 20,
       88         Capital = 20,
       89         HangulMode = 21,
       90         KanaMode = 21,
       91         JunjaMode = 23,
       92         FinalMode = 24,
       93         KanjiMode = 25,
       94         HanjaMode = 25,
       95         Escape = 27,
       96         Space = 32,
       97         Prior = 33,
       98         PageUp = 33,
       99         PageDown = 34,
      100         Next = 34,
      101         End = 35,
      102         Home = 36,
      103         Left = 37,
      104         Up = 38,
      105         Right = 39,
      106         Down = 40,
      107         Select = 41,
      108         Print = 42,
      109         Execute = 43,
      110         Snapshot = 44,
      111         PrintScreen = 44,
      112         Insert = 45,
      113         Delete = 46,
      114         Help = 47,
      115         D0 = 48,
      116         D1 = 49,
      117         D2 = 50,
      118         D3 = 51,
      119         D4 = 52,
      120         D5 = 53,
      121         D6 = 54,
      122         D7 = 55,
      123         D8 = 56,
      124         D9 = 57,
      125         A = 65,
      126         B = 66,
      127         C = 67,
      128         D = 68,
      129         E = 69,
      130         F = 70,
      131         G = 71,
      132         H = 72,
      133         I = 73,
      134         J = 74,
      135         K = 75,
      136         L = 76,
      137         M = 77,
      138         N = 78,
      139         O = 79,
      140         P = 80,
      141         Q = 81,
      142         R = 82,
      143         S = 83,
      144         T = 84,
      145         U = 85,
      146         V = 86,
      147         W = 87,
      148         X = 88,
      149         Y = 89,
      150         Z = 90,
      151         LWin = 91,
      152         RWin = 92,
      153         Apps = 93,
      154         Sleep = 95,
      155         NumPad0 = 96,
      156         NumPad1 = 97,
      157         NumPad2 = 98,
      158         NumPad3 = 99,
      159         NumPad4 = 100,
      160         NumPad5 = 101,
      161         NumPad6 = 102,
      162         NumPad7 = 103,
      163         NumPad8 = 104,
      164         NumPad9 = 105,
      165         Multiply = 106,
      166         Add = 107,
      167         Separator = 108,
      168         Subtract = 109,
      169         Decimal = 110,
      170         Divide = 111,
      171         F1 = 112,
      172         F2 = 113,
      173         F3 = 114,
      174         F4 = 115,
      175         F5 = 116,
      176         F6 = 117,
      177         F7 = 118,
      178         F8 = 119,
      179         F9 = 120,
      180         F10 = 121,
      181         F11 = 122,
      182         F12 = 123,
      183         F13 = 124,
      184         F14 = 125,
      185         F15 = 126,
      186         F16 = 127,
      187         F17 = 128,
      188         F18 = 129,
      189         F19 = 130,
      190         F20 = 131,
      191         F21 = 132,
      192         F22 = 133,
      193         F23 = 134,
      194         F24 = 135,
      195         NumLock = 144,
      196         Scroll = 145,
      197         BrowserBack = 166,
      198         BrowserForward = 167,
      199         BrowserRefresh = 168,
      200         BrowserStop = 169,
      201         BrowserSearch = 170,
      202         BrowserFavorites = 171,
      203         BrowserHome = 172,
      204         VolumeMute = 173,
      205         VolumeDown = 174,
      206         VolumeUp = 175,
      207         MediaNextTrack = 176,
      208         MediaPreviousTrack = 177,
      209         MediaStop = 178,
      210         MediaPlayPause = 179,
      211         LaunchMail = 180,
      212         SelectMedia = 181,
      213         LaunchApplication1 = 182,
      214         LaunchApplication2 = 183,
      215         OemSemicolon = 186,
      216         Oem1 = 186,
      217         OemMinus = 189,
      218         OemPeriod = 190,
      219         Oem2 = 191,
      220         OemQuestion = 191,
      221         Oem3 = 192,
      222         Oem4 = 219,
      223         OemOpenBrackets = 219,
      224         OemPipe = 220,
      225         Oem5 = 220,
      226         OemCloseBrackets = 221,
      227         Oem6 = 221,
      228         OemQuotes = 222,
      229         Oem7 = 222,
      230         Oem8 = 223,
      231         Oem102 = 226,
      232         OemBackslash = 226,
      233         Attn = 246,
      234         EraseEof = 249,
      235         Play = 250,
      236         Zoom = 251,
      237         NoName = 252,
      238         Pa1 = 253,
      239         OemClear = 254
      240     }
      241 
      242     public class ShortcutKeyArgs : EventArgs
      243     {
      244         public ShortcutKeyArgs(string name, ModifierEnum modifiers, KeyEnum key, Action<ShortcutKeyArgs> action)
      245         {
      246             Name = name;
      247             Key = key;
      248             Modifiers = modifiers;
      249             Action = action;
      250         }
      251 
      252         public string Name { get; private set; }
      253         public ModifierEnum Modifiers { get; private set; }
      254         public KeyEnum Key { get; private set; }
      255         public Action<ShortcutKeyArgs> Action { get; private set; }
      256 
      257         public override string ToString()
      258         {
      259             if (Modifiers == ModifierEnum.None)
      260             {
      261                 return string.Format("{0}:	{1}", Name, Key);
      262             }
      263             return string.Format("{0}:	{1} + {2}", Name, Modifiers, Key);
      264         }
      265     }
      266 
      267     public static class KeyEnumExtionsion
      268     {
      269         public static bool ExtEquals(this KeyEnum key, Keys keyForm)
      270         {
      271             return (int)key == (int)keyForm;
      272         }
      273 
      274         //public static bool ExtEquals(this KeyEnum key, Key keyWpf)
      275         //{
      276         //    return (int)key == (int)keyWpf;
      277         //}
      278     }
      279 }
      View Code
    2. Wpf
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Windows;
       4 using System.Windows.Input;
       5 using WUtilitiesV01.Services;
       6 using WUtilitiesV01WPF.Helper;
       7 
       8 namespace WUtilitiesV01WPF.Services
       9 {
      10     public class ShortcutKeyService : ShortcutKeyServiceBase
      11     {
      12         private Window mWindow;
      13 
      14         public ShortcutKeyService()
      15         {
      16         }
      17 
      18         public void SetWindow(Window wnd)
      19         {
      20             mWindow = wnd;
      21             mWindow.PreviewKeyDown += Window_OnPreviewKeyDown;
      22         }
      23 
      24         Key toKey(KeyEnum keyEnum)
      25         {
      26             return KeyInterop.KeyFromVirtualKey((int)keyEnum);
      27         }
      28 
      29         private void Window_OnPreviewKeyDown(object sender, KeyEventArgs e)
      30         {
      31             if (!Enabled)
      32             {
      33                 return;
      34             }
      35 
      36             foreach (var sk in mListShorcutKey)
      37             {
      38                 if (sk.Modifiers == ModifierEnum.None)
      39                 {
      40                     if (toKey(sk.Key) == e.Key)
      41                     {
      42                         sk.Action(sk);
      43                     }
      44                 }
      45                 else
      46                 {
      47                     if (sk.Modifiers.HasFlag(ModifierEnum.Control))
      48                     {
      49                         if (!e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) && !e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
      50                         {
      51                             continue;
      52                         }
      53                     }
      54 
      55                     if (sk.Modifiers.HasFlag(ModifierEnum.Shift))
      56                     {
      57                         if (!e.KeyboardDevice.IsKeyDown(Key.LeftShift) && !e.KeyboardDevice.IsKeyDown(Key.RightShift))
      58                         {
      59                             continue;
      60                         }
      61                     }
      62 
      63                     if (sk.Modifiers.HasFlag(ModifierEnum.Alt))
      64                     {
      65                         if (!e.KeyboardDevice.IsKeyDown(Key.LeftAlt) && !e.KeyboardDevice.IsKeyDown(Key.RightAlt))
      66                         {
      67                             continue;
      68                         }
      69                     }
      70 
      71                     if (e.KeyboardDevice.IsKeyDown(toKey(sk.Key)))
      72                     {
      73                         sk.Action(sk);
      74                     }
      75                 }
      76             }
      77         }
      78 
      79     }
      80 }
      View Code
    3. Winform
       1 using System;
       2 using System.Collections.Generic;
       3 using System.Windows;
       4 using System.Windows.Input;
       5 using WUtilitiesV01.Services;
       6 using WUtilitiesV01WPF.Helper;
       7 
       8 namespace WUtilitiesV01WPF.Services
       9 {
      10     public class ShortcutKeyService : ShortcutKeyServiceBase
      11     {
      12         private Window mWindow;
      13 
      14         public ShortcutKeyService()
      15         {
      16         }
      17 
      18         public void SetWindow(Window wnd)
      19         {
      20             mWindow = wnd;
      21             mWindow.PreviewKeyDown += Window_OnPreviewKeyDown;
      22         }
      23 
      24         Key toKey(KeyEnum keyEnum)
      25         {
      26             return KeyInterop.KeyFromVirtualKey((int)keyEnum);
      27         }
      28 
      29         private void Window_OnPreviewKeyDown(object sender, KeyEventArgs e)
      30         {
      31             if (!Enabled)
      32             {
      33                 return;
      34             }
      35 
      36             foreach (var sk in mListShorcutKey)
      37             {
      38                 if (sk.Modifiers == ModifierEnum.None)
      39                 {
      40                     if (toKey(sk.Key) == e.Key)
      41                     {
      42                         sk.Action(sk);
      43                     }
      44                 }
      45                 else
      46                 {
      47                     if (sk.Modifiers.HasFlag(ModifierEnum.Control))
      48                     {
      49                         if (!e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) && !e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
      50                         {
      51                             continue;
      52                         }
      53                     }
      54 
      55                     if (sk.Modifiers.HasFlag(ModifierEnum.Shift))
      56                     {
      57                         if (!e.KeyboardDevice.IsKeyDown(Key.LeftShift) && !e.KeyboardDevice.IsKeyDown(Key.RightShift))
      58                         {
      59                             continue;
      60                         }
      61                     }
      62 
      63                     if (sk.Modifiers.HasFlag(ModifierEnum.Alt))
      64                     {
      65                         if (!e.KeyboardDevice.IsKeyDown(Key.LeftAlt) && !e.KeyboardDevice.IsKeyDown(Key.RightAlt))
      66                         {
      67                             continue;
      68                         }
      69                     }
      70 
      71                     if (e.KeyboardDevice.IsKeyDown(toKey(sk.Key)))
      72                     {
      73                         sk.Action(sk);
      74                     }
      75                 }
      76             }
      77         }
      78 
      79     }
      80 }
      View Code

    基类有个MsgForm,是个显示窗体,可以用MessageBox替代。

    使用方法:实例化Winform或者Wpf的快捷键服务类,调用SetForm/WIndow方法,关联窗体

    然后调用Registxxx方法注册快捷键。

     Ctrl+Alt+H 显示已经注册的快捷键,如果需要禁用本功能,基类修改

    只适用于窗体快捷键,不能用于全局热键。

    转载注明出处

  • 相关阅读:
    jbpm 为任务自由选择办理人
    我永远的 dell 15r
    select radio readonly
    面向对象的5条基本设计原则
    Java数据库缓存思路
    作为java应届生,面试求职那点事
    项目开发中数据字典设计实现缓存
    oracle 优化 —— 分区表
    myeclipse快捷键
    win8 安装myeclipse 失败 MyEclipse ForSpring 安装失败
  • 原文地址:https://www.cnblogs.com/dubuyunjie/p/10891386.html
Copyright © 2020-2023  润新知