Winform,Wpf快捷键
2021-06-03 16:04
标签:mail join src message omv send img reg ast 基类有个MsgForm,是个显示窗体,可以用MessageBox替代。 使用方法:实例化Winform或者Wpf的快捷键服务类,调用SetForm/WIndow方法,关联窗体 然后调用Registxxx方法注册快捷键。 Ctrl+Alt+H 显示已经注册的快捷键,如果需要禁用本功能,基类修改 只适用于窗体快捷键,不能用于全局热键。 转载注明出处 Winform,Wpf快捷键 标签:mail join src message omv send img reg ast 原文地址:https://www.cnblogs.com/dubuyunjie/p/10891386.html
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
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 }
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 }