Click here to Skip to main content
15,903,632 members
Articles / Desktop Programming / Windows Forms

Keys Extender (Win 7 Compatible)

Rate me:
Please Sign up or sign in to vote.
4.30/5 (6 votes)
21 Apr 2009GPL31 min read 22.1K   555   15  
Work with Windows by hot keys in Vista like Windows 7


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

 Image 1

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:

  1. interception of pressing the keys, and
  2. 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
    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)
        // 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) && 
		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), 

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".

        <setting name="ShowNotifyIcon" serializeAs="String">

Enjoy it!    


  • 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:

You can download the new version and code from here.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

-- There are no messages in this forum --