3

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!

Joe Korolewicz
  • 474
  • 4
  • 21
raaj
  • 2,869
  • 4
  • 38
  • 58

4 Answers4

5

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(); 
}
Community
  • 1
  • 1
Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
  • i wish to use this, but what do i have to #include/using.etc? it says Hotkey does not exist – raaj Sep 15 '12 at 09:10
  • There is a wrapper class linked to from the page ("Global Hotkeys with .NET"), most of these things are really Win32 features. – Ray Hayes Sep 17 '12 at 06:48
3

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

Franck Charlier
  • 414
  • 4
  • 17
  • 3
    Don't need to go 'low-level' if you're willing to have a global hotkey. Low-level tends to be for snooping on any keyboard activity rather than reserving a key-combination for your own use. – Ray Hayes Sep 14 '12 at 11:10
  • 1
    Gotta agree with @RayHayes here, you may even get called out by some anti-malware software. – Mike Perrenoud Sep 14 '12 at 12:04
1

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    But now you've gone from wanting a simple keyboard combination to show the application to needing to handle every keystroke made across the whole.... – Ray Hayes Sep 14 '12 at 11:19
  • 1
    Uwe by taking more than you need (e.g. all keys rather than a specific combination), not only are you adding more work - needing to filter the results to get what you're after, but you're also not sticking to the 'ask only for what you need' principle. Imagine if a 'notepad' application on an Android phone asked for 'make phone calls' access - you'd be concerned. I'd be concerned if a tray utility wanted to see all of my typed passwords just so it could show itself. An antivirus tool might be concerned on my behalf too! – Ray Hayes Sep 14 '12 at 15:29
  • @RayHayes Yeah, but really: How does this impact the end-user? – Uwe Keim Sep 14 '12 at 15:34
  • 1
    It wasn't an end-user asking - it was, I imagine, a developer. It might affect the end-user if an anti-virus tool spots the use of the keyboard hook and adds it to the malware list! Again back to my 'android' example, it doesn't really affect me if a notepad app wants access to my phone making capability - unless it is abused. – Ray Hayes Sep 14 '12 at 16:02
  • Sorry, that's way to esoteric for me. – Uwe Keim Sep 14 '12 at 17:36
  • 1
    Low level keyboard hooks should be used as a last resort if Global Hotkeys dont do the job you require. When you low level hook like this, you are running your code for every keystroke in every application, which not only can slow it down or potentially create issues with other keystroke event handlers, it can be flagged by antivirus etc. Its not 'esoteric' to want to avoid unnecessary overhead and issues. Using hooks like this is more the equivalent of using a textbox to store strings instead of an array or List - it will work, but is poor coding. – WiredEarp Apr 07 '16 at 23:30
1

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
    }
}
Scott
  • 21,211
  • 8
  • 65
  • 72