misu – Diff between revs 1 and 3
?pathlinks?
Rev 1 | Rev 3 | |||
---|---|---|---|---|
Line 7... | Line 7... | |||
7 | using Configuration; |
7 | using Configuration; |
|
8 | using Gma.System.MouseKeyHook; |
8 | using Gma.System.MouseKeyHook; |
|
Line 9... | Line 9... | |||
9 | |
9 | |
|
10 | namespace misu |
10 | namespace misu |
|
11 | { |
11 | { |
|
12 | public partial class Form1 : Form |
12 | public partial class MisuMainForm : Form |
|
13 | { |
13 | { |
|
Line 14... | Line 14... | |||
14 | #region Public Enums, Properties and Fields |
14 | #region Public Enums, Properties and Fields |
|
Line 15... | Line -... | |||
15 | |
- | ||
16 | public Settings Settings { get; set; } |
- | ||
17 | |
15 | |
|
Line 18... | Line 16... | |||
18 | public Point LockedCursorPosition { get; set; } |
16 | public Settings Settings { get; set; } |
|
Line 19... | Line 17... | |||
19 | |
17 | |
|
Line 33... | Line 31... | |||
33 | |
31 | |
|
Line 34... | Line 32... | |||
34 | #endregion |
32 | #endregion |
|
Line 35... | Line 33... | |||
35 | |
33 | |
|
36 | #region Constructors, Destructors and Finalizers |
34 | #region Constructors, Destructors and Finalizers |
|
37 | |
35 | |
|
Line 38... | Line 36... | |||
38 | public Form1() |
36 | public MisuMainForm() |
|
Line 52... | Line 50... | |||
52 | Settings = new Settings(); |
50 | Settings = new Settings(); |
|
53 | break; |
51 | break; |
|
54 | } |
52 | } |
|
Line 55... | Line 53... | |||
55 | |
53 | |
|
- | 54 | Settings.PropertyChanged += Settings_PropertyChanged; |
||
Line 56... | Line 55... | |||
56 | Settings.PropertyChanged += Settings_PropertyChanged; |
55 | Settings.Binding.PropertyChanged += Binding_PropertyChanged; |
|
57 | |
56 | |
|
Line 58... | Line 57... | |||
58 | // Hook to global events. |
57 | // Hook to global events. |
|
59 | GlobalHook = Hook.GlobalEvents(); |
58 | GlobalHook = Hook.GlobalEvents(); |
|
60 | |
59 | |
|
- | 60 | // Bind to receive keyboard presses. |
||
- | 61 | GlobalHook.KeyDown += GlobalHook_KeyDown; |
||
- | 62 | GlobalHook.KeyUp += GlobalHook_KeyUp; |
||
- | 63 | |
||
- | 64 | GlobalHook.MouseUpExt += GlobalHook_BlockMouse; |
||
61 | // Bind to receive keyboard presses. |
65 | GlobalHook.MouseDragFinishedExt += GlobalHook_BlockMouse; |
|
- | 66 | GlobalHook.MouseDragStartedExt += GlobalHook_BlockMouse; |
||
- | 67 | GlobalHook.MouseWheelExt += GlobalHook_BlockMouse; |
||
- | 68 | GlobalHook.MouseMoveExt += GlobalHook_BlockMouse; |
||
- | 69 | GlobalHook.MouseDownExt += GlobalHook_BlockMouse; |
||
- | 70 | |
||
- | 71 | if (Settings.Binding.Keys.Any()) |
||
- | 72 | { |
||
62 | GlobalHook.KeyDown += GlobalHook_KeyDown; |
73 | Notify($"Shortcut is bound.", |
|
Line 63... | Line 74... | |||
63 | GlobalHook.KeyUp += GlobalHook_KeyUp; |
74 | $"Press {Settings.Binding} to toggle locking."); |
|
Line 64... | Line 75... | |||
64 | GlobalHook.MouseMove += GlobalHook_MouseMove; |
75 | } |
|
Line 65... | Line 76... | |||
65 | } |
76 | } |
|
66 | |
77 | |
|
67 | #endregion |
78 | #endregion |
|
- | 79 | |
||
- | 80 | #region Event Handlers |
||
- | 81 | |
||
- | 82 | private async void Binding_PropertyChanged(object sender, PropertyChangedEventArgs e) |
||
- | 83 | { |
||
- | 84 | await Settings.Serialize(@"Settings.xml"); |
||
68 | |
85 | } |
|
69 | #region Event Handlers |
86 | |
|
70 | |
87 | private void GlobalHook_BlockMouse(object sender, MouseEventExtArgs e) |
|
- | 88 | { |
||
71 | private void GlobalHook_MouseMove(object sender, MouseEventArgs e) |
89 | // Do not perform any operations while the binding form is visible. |
|
72 | { |
- | ||
73 | if (Settings.Mouse) |
- | ||
74 | { |
90 | if (BindKeyForm != null && BindKeyForm.Visible) |
|
75 | switch (_lock) |
- | ||
76 | { |
91 | { |
|
77 | case true: |
- | ||
78 | Cursor.Clip = new Rectangle(LockedCursorPosition, new Size(1, 1)); |
92 | return; |
|
79 | break; |
93 | } |
|
Line 80... | Line 94... | |||
80 | default: |
94 | |
|
81 | Cursor.Clip = Screen.PrimaryScreen.Bounds; |
95 | if (_lock && Settings.Mouse) |
|
Line 90... | Line 104... | |||
90 | await Settings.Serialize(@"Settings.xml"); |
104 | await Settings.Serialize(@"Settings.xml"); |
|
91 | } |
105 | } |
|
Line 92... | Line 106... | |||
92 | |
106 | |
|
93 | private void GlobalHook_KeyUp(object sender, KeyEventArgs e) |
107 | private void GlobalHook_KeyUp(object sender, KeyEventArgs e) |
|
- | 108 | { |
||
94 | { |
109 | // Do not perform any operations while the binding form is visible. |
|
95 | if (Settings.Binding == null) |
110 | if (BindKeyForm != null && BindKeyForm.Visible) |
|
96 | { |
111 | { |
|
97 | return; |
112 | return; |
|
Line 98... | Line 113... | |||
98 | } |
113 | } |
|
99 | |
114 | |
|
100 | if (!PressedKeysBufferBlock.TryReceiveAll(out var keys)) |
115 | if (!PressedKeysBufferBlock.TryReceiveAll(out var keys)) |
|
101 | { |
116 | { |
|
Line -... | Line 117... | |||
- | 117 | return; |
||
- | 118 | } |
||
- | 119 | |
||
- | 120 | if (Settings.Binding == null) |
||
- | 121 | { |
||
102 | return; |
122 | return; |
|
103 | } |
123 | } |
|
104 | |
124 | |
|
Line 105... | Line 125... | |||
105 | if (Settings.Binding.Keys.SequenceContains(keys.Select(key => key.KeyCode))) |
125 | if (Settings.Binding.Keys.SequenceContains(keys.Select(key => key.KeyCode))) |
|
- | 126 | { |
||
- | 127 | _lock = !_lock; |
||
- | 128 | |
||
- | 129 | // Lock the cursor in position. |
||
- | 130 | switch (_lock) |
||
- | 131 | { |
||
106 | { |
132 | case true: |
|
- | 133 | Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1)); |
||
- | 134 | break; |
||
Line 107... | Line 135... | |||
107 | _lock = !_lock; |
135 | default: |
|
108 | |
136 | Cursor.Clip = Screen.PrimaryScreen.Bounds; |
|
109 | // Save the cursor position in order to lock the mouse. |
137 | break; |
|
Line 110... | Line 138... | |||
110 | LockedCursorPosition = Cursor.Position; |
138 | } |
|
- | 139 | |
||
111 | |
140 | // Dismiss the current keypress to avoid typing the key. |
|
112 | // Dismiss the current keypress to avoid typing the key. |
141 | e.Handled = true; |
|
Line 113... | Line 142... | |||
113 | e.Handled = true; |
142 | e.SuppressKeyPress = true; |
|
114 | e.SuppressKeyPress = true; |
143 | |
|
- | 144 | Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.", |
||
- | 145 | $"Press {Settings.Binding} to toggle mouse and keyboard locking."); |
||
- | 146 | } |
||
- | 147 | } |
||
- | 148 | |
||
- | 149 | private async void GlobalHook_KeyDown(object sender, KeyEventArgs e) |
||
115 | |
150 | { |
|
116 | Notify($"Input is {(_lock ? "locked" : "unlocked")}", "Press the keyboard shortcut to toggle locking."); |
151 | // Do not perform any operations while the binding form is visible. |
|
117 | } |
152 | if (BindKeyForm != null && BindKeyForm.Visible) |
|
118 | } |
153 | { |
|
119 | |
154 | return; |
|
Line 159... | Line 194... | |||
159 | { |
194 | { |
|
160 | BindKeyForm.KeyBound -= BindKeyForm_KeyBound; |
195 | BindKeyForm.KeyBound -= BindKeyForm_KeyBound; |
|
161 | BindKeyForm.Closing -= BindKeyForm_Closing; |
196 | BindKeyForm.Closing -= BindKeyForm_Closing; |
|
162 | } |
197 | } |
|
Line 163... | Line 198... | |||
163 | |
198 | |
|
164 | private void BindKeyForm_KeyBound(object sender, KeyBoundEventArgs e) |
199 | private async void BindKeyForm_KeyBound(object sender, KeyBoundEventArgs e) |
|
- | 200 | { |
||
165 | { |
201 | Notify("Shortcut key is bound.", |
|
Line 166... | Line 202... | |||
166 | Notify("Keys bound", "Press to toggle between locked and unlocked states."); |
202 | $"Press {e.Binding} to toggle between locked and unlocked keyboard and mouse."); |
|
- | 203 | |
||
- | 204 | Settings.Binding.Keys.AddRange(e.Binding.Keys); |
||
167 | |
205 | |
|
Line 168... | Line 206... | |||
168 | Settings.Binding = e.Binding; |
206 | await Settings.Serialize(@"Settings.xml"); |
|
169 | } |
207 | } |
|
170 | |
208 | |
|
Line 196... | Line 234... | |||
196 | |
234 | |
|
Line 197... | Line 235... | |||
197 | #region Private Methods |
235 | #region Private Methods |
|
198 | |
236 | |
|
199 | private void Notify(string title, string message, int timeout = 10000) |
237 | private void Notify(string title, string message, int timeout = 10000) |
|
200 | { |
238 | { |
|
201 | notifyIcon1.BalloonTipTitle = title; |
239 | misuNotificationIcon.BalloonTipTitle = title; |
|
202 | notifyIcon1.BalloonTipText = message; |
240 | misuNotificationIcon.BalloonTipText = message; |
|
203 | // the timeout parameter is not used for Windows Vista and up |
241 | // the timeout parameter is not used for Windows Vista and up |
|
Line 204... | Line 242... | |||
204 | notifyIcon1.ShowBalloonTip(timeout); |
242 | misuNotificationIcon.ShowBalloonTip(timeout); |
|
205 | } |
243 | } |
|
206 | |
244 | |
|
207 | #endregion |
245 | #endregion |