Question

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!

Was it helpful?

Solution

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(); 
}

OTHER TIPS

To do this, you must use "Low-Level Hook". You will find all information about it on this article : http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

Look at this too : http://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET

I second Franck's suggestion about a global keyboard hook. Personally, I had very good experiences with the CodeProject article "Processing Global Mouse and Keyboard Hooks in C#".

As they write in their article, you can do things like:

private UserActivityHook _actHook;

private void MainFormLoad(object sender, System.EventArgs e)
{
    _actHook = new UserActivityHook();

    _actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
}

You could then call a function in your MyKeyPress handler that opens your window.

If you follow the guide here. It will show you how to register a global shortcut key.

public partial class Form1 : Form
{
    KeyboardHook hook = new KeyboardHook();

    public Form1()
    {
        InitializeComponent();

        // register the event that is fired after the key press.
        hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);

        // register the CONTROL + ALT + F12 combination as hot key.
        // You can change this.
        hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt, Keys.F12);
    }

    private void hook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        // Trigger your function
        MinimiseToTray(true);
    }

    private void MinimiseToTray(bool shortCutPressed)
    {
        // ... Your code
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top