Maximize C# Application from System Tray using Keyboard Shortcut

raaj picture raaj · Sep 14, 2012 · Viewed 7k times · Source

Can I know if there is anyway that I can maximise my windows form application from the system tray using say a Keyboard Shortcut rather than clicking on it?

I am currently minimizing using this piece of code

//Minimize to Tray with over-ride for short cut
private void MinimiseToTray(bool shortCutPressed)
{
    notifyIcon.BalloonTipTitle = "Minimize to Tray App";
    notifyIcon.BalloonTipText = "You have successfully minimized your app.";

    if (FormWindowState.Minimized == this.WindowState || shortCutPressed)
    {
        notifyIcon.Visible = true;
        notifyIcon.ShowBalloonTip(500);
        this.Hide();
    }
    else if (FormWindowState.Normal == this.WindowState)
    {
        notifyIcon.Visible = false;
    }
}

Hence, I need a keyboard shortcut that should maximize it. Much thanks!

Answer

Ray Hayes picture Ray Hayes · Sep 14, 2012

EDIT: If you simply want to 'reserve a key combination' to perform something on your application, a Low-Level keyboard hook whereby you see every keypress going to any other application is not only an overkill, but bad practice and in my personal view likely to have people thinking that you're keylogging! Stick to a HOT-KEY!

Given that your icon will not have keyboard focus, you need to register a global keyboard hotkey.

Other similar questions:

Example from Global Hotkeys With .NET:

Hotkey hk = new Hotkey();
hk.KeyCode = Keys.1;
hk.Windows = true;
hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };

if (!hk.GetCanRegister(myForm))
{ 
    Console.WriteLine("Whoops, looks like attempts to register will fail " +
                      "or throw an exception, show error to user"); 
}
else
{ 
    hk.Register(myForm); 
}

// .. later, at some point
if (hk.Registered)
{ 
   hk.Unregister(); 
}