-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHotkeyHandler.cs
188 lines (158 loc) · 6.91 KB
/
HotkeyHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Drawing;
using System.Windows.Forms;
using GlobalLowLevelHooks;
namespace WindowTool
{
internal class HotkeyHandler
{
private MouseHook mouseHook;
private KeyboardHook keyboardHook;
private bool isLeftControlDown;
private bool isLeftWinDown;
private bool isLeftAltDown;
private bool isMovingWindow;
private bool isResizingWindow;
private IntPtr currentWindowHandle;
private Rectangle currentWindowRectangle;
private Point moveWindowMouseOffset;
private Point resizeWindowsMouseOffset;
private int resizeStartWidth;
private int resizeStartHeight;
private bool clampToScreen;
private Point currentWindowPosition;
private int currentWindowWidth;
private int currentWindowHeight;
internal void Start()
{
mouseHook = new MouseHook();
mouseHook.MouseMove += MouseHook_MouseMove;
mouseHook.Install();
keyboardHook = new KeyboardHook();
keyboardHook.KeyDown += KeyboardHook_KeyDown;
keyboardHook.KeyUp += KeyboardHook_KeyUp;
keyboardHook.Install();
}
internal void Stop()
{
mouseHook.MouseMove -= MouseHook_MouseMove;
mouseHook.Uninstall();
keyboardHook.KeyDown -= KeyboardHook_KeyDown;
keyboardHook.KeyUp -= KeyboardHook_KeyUp;
keyboardHook.Uninstall();
}
internal void SetClampToScreen(bool clamp)
{
clampToScreen = clamp;
}
private void MouseHook_MouseMove(MouseHook.MSLLHOOKSTRUCT mouseStruct)
{
if (isMovingWindow == false && isResizingWindow == false) return;
if (currentWindowHandle == IntPtr.Zero) return;
Point mousePosition = Control.MousePosition;
if (isMovingWindow)
{
currentWindowPosition.X = mousePosition.X - moveWindowMouseOffset.X;
currentWindowPosition.Y = mousePosition.Y - moveWindowMouseOffset.Y;
if (clampToScreen)
{
Screen currentScreen = Screen.FromHandle(currentWindowHandle);
int remainingAreaX = currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width - currentWindowWidth;
if (remainingAreaX < currentScreen.WorkingArea.X)
{
remainingAreaX = currentScreen.WorkingArea.X;
}
int remainingAreaY = currentScreen.WorkingArea.Y + currentScreen.WorkingArea.Height - currentWindowHeight;
if (remainingAreaY < currentScreen.WorkingArea.Y)
{
remainingAreaY = currentScreen.WorkingArea.Y;
}
currentWindowPosition.X = Math.Clamp(currentWindowPosition.X, currentScreen.WorkingArea.X, remainingAreaX);
currentWindowPosition.Y = Math.Clamp(currentWindowPosition.Y, currentScreen.WorkingArea.Y, remainingAreaY);
}
Window.SetWindowPosition(currentWindowHandle, currentWindowPosition.X, currentWindowPosition.Y);
}
if (isResizingWindow)
{
currentWindowWidth = resizeStartWidth + mousePosition.X - resizeWindowsMouseOffset.X;
currentWindowHeight = resizeStartHeight + mousePosition.Y - resizeWindowsMouseOffset.Y;
if (clampToScreen)
{
Screen currentScreen = Screen.FromHandle(currentWindowHandle);
currentWindowWidth = Math.Clamp(currentWindowWidth, 1, currentScreen.WorkingArea.Width - (currentWindowPosition.X - currentScreen.Bounds.X));
currentWindowHeight = Math.Clamp(currentWindowHeight, 1, currentScreen.WorkingArea.Height - (currentWindowPosition.Y - currentScreen.Bounds.Y));
}
Window.SetWindowSize(currentWindowHandle, currentWindowWidth, currentWindowHeight);
}
}
private void KeyboardHook_KeyDown(KeyboardHook.VKeys key)
{
if (key == KeyboardHook.VKeys.LCONTROL) isLeftControlDown = true;
if (key == KeyboardHook.VKeys.LWIN) isLeftWinDown = true;
if (key == KeyboardHook.VKeys.LMENU) isLeftAltDown = true;
CheckHotKey();
}
private void KeyboardHook_KeyUp(KeyboardHook.VKeys key)
{
if (key == KeyboardHook.VKeys.LCONTROL) isLeftControlDown = false;
if (key == KeyboardHook.VKeys.LWIN) isLeftWinDown = false;
if (key == KeyboardHook.VKeys.LMENU) isLeftAltDown = false;
CheckHotKey();
}
private void CheckHotKey()
{
if (isLeftControlDown == true && isLeftWinDown == true)
{
if (currentWindowHandle == IntPtr.Zero) SaveWindowAtPointer();
if (isLeftAltDown == false && isMovingWindow == false) StartMovingWindow();
if (isLeftAltDown == true && isResizingWindow == false) StartResizingWindow();
}
else
{
if (isMovingWindow) StopMovingWindow();
if (isResizingWindow) StopResizingWindow();
currentWindowHandle = IntPtr.Zero;
}
}
private void SaveWindowAtPointer()
{
Point mousePosition = Control.MousePosition;
currentWindowHandle = Window.GetWindow(mousePosition);
}
private void StartMovingWindow()
{
isMovingWindow = true;
isResizingWindow = false;
Window.HandleMaximizedWindow(currentWindowHandle);
SetStartingOffsets();
}
private void StopMovingWindow()
{
isMovingWindow = false;
}
private void StartResizingWindow()
{
isMovingWindow = false;
isResizingWindow = true;
Window.HandleMaximizedWindow(currentWindowHandle);
SetStartingOffsets();
}
private void StopResizingWindow()
{
isResizingWindow = false;
}
private void SetStartingOffsets()
{
Point mousePostion = Control.MousePosition;
currentWindowRectangle = Window.GetWindowPosition(currentWindowHandle);
moveWindowMouseOffset.X = mousePostion.X - currentWindowRectangle.X;
moveWindowMouseOffset.Y = mousePostion.Y - currentWindowRectangle.Y;
resizeWindowsMouseOffset.X = mousePostion.X;
resizeWindowsMouseOffset.Y = mousePostion.Y;
resizeStartWidth = currentWindowRectangle.Width - currentWindowRectangle.X;
resizeStartHeight = currentWindowRectangle.Height - currentWindowRectangle.Y;
currentWindowWidth = resizeStartWidth;
currentWindowHeight = resizeStartHeight;
}
}
}