Keys Extender (Win 7 Compatible)
- modified:
- reading: 2 minutes
Introduction
In Windows 7, I really liked an opportunity to change the position of the windows by pressing hotkeys Win + (Left | Right | Up | Bottom):
- Win + Left - window attached to the left side
- Win + Right - window attached to the right side
- Win + Up - window is maximized
- Win + Bottom - window in the normal state
Using the Code
Making this work with Windows Vista (most likely will work in earlier versions, but I have not tried it) was not a difficult task. First of all, I imported many WinApi
functions in a WinForms application. The work was divided into two parts:
- interception of pressing the keys, and
- positioning the active window
The art Low-Level Keyboard Hook in C # helped me to solve the first part. I had only to find the way to know whether the button LWin is pressed. Here is the main code:
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WAKeysHook.WmKeyDown)
{
if ((int)WAKeysHook.GetAsyncKeyState((IntPtr)Keys.LWin) != 0) // Left Win Key
{
Keys key = (Keys) Marshal.ReadInt32(lParam);
if (key == Keys.Left || key == Keys.Right ||
key == Keys.Up || key == Keys.Down) // Arrows keys
SetWindowLocation(key);
}
}
return WAKeysHook.CallNextHookEx(WaKeysHook.HookId, nCode, wParam, lParam);
}
The second part: looking for the foreground window, setting state and size.
private static void SetWindowLocation(Keys k)
{
//Active Window
IntPtr window = WAWindows.GetForegroundWindow();
// Check SIZEBOX Style (Window can sizable)
if (((int)WAWindows.GetWindowLongPtr(window, WAWindows.GwlStyle) &
WAWindows.WsSizebox)
== WAWindows.WsSizebox)
{
// Show window in normal state (if always maximized)
WAWindows.ShowWindow(window, (int)WAWindows.WindowShowStyle.ShowNormal);
//Need Maximized
if (k != Keys.Down)
{
WAWindows.ShowWindow(window, (int)WAWindows.WindowShowStyle.ShowMaximized);
// Place Window on left or right
if (k != Keys.Up)
{
WAWindows.Rect rect, rectDesktop;
if (WAWindows.GetWindowRect(window, out rect) &&
WAWindows.GetWindowRect(WAWindows.GetDesktopWindow(),
out rectDesktop))
{
WAWindows.SetWindowPos(window, WAWindows.HwndTop,
(k == Keys.Left) ? Math.Min(rect.Left, 0) :
rectDesktop.Right / 2, rect.Top, Math.Min(rect.Width,
rectDesktop.Right / 2 - Math.Min(rect.Left, 0)),
Math.Min(rect.Height, rectDesktop.Bottom),
WAWindows.SwpShowwindow);
}
}
}
}
}
In addition, there is an opportunity to display (or hide) the icon in the taskbar with context menu (that can help you to close the application). You can set these options in app.config section "ShowNotifyIcon
".
<applicationSettings>
<VistaKeysExtender.Properties.Settings>
<setting name="ShowNotifyIcon" serializeAs="String">
<value>False</value>
</setting>
</VistaKeysExtender.Properties.Settings>
</applicationSettings>
Enjoy it!
Updated
- 20.04.2009 - Build version for x64 system
- 21.04.2009 - Project has new version. Now you can choose the setting, and some bugs fixed. This is the new version window: