Click here to Skip to main content
Click here to Skip to main content

Keys Extender (Win 7 Compatible)

, 21 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Work with Windows by hot keys in Vista like Windows 7

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:

  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
                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:
0000k00h.png

You can download the new version and code from here.

License

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

Share

About the Author

outcoldman
Software Developer (Senior) Mobile Systems International Ltd
Russian Federation Russian Federation
I'm MVP Client App Dev. At sum have more than 5 years’ experience in developing applications and components. You can find more information about me and read all of my articles on my personal site http://outcoldman.ru.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 21 Apr 2009
Article Copyright 2009 by outcoldman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid