views:

339

answers:

4

I would like for my app to detect keyboard shortcuts when it isn't running, but as there doesn't appear facility to do this in Windows a reasonable fallback would be to have a background process without any UI that listens for keypresses. Is it possible to monitor keypresses when an app isn't in the foreground in .Net?

Looks like this CodeProject article has what I need.

+1  A: 

There's no way to make it non intrusive.

It is always intrusive to add global keyboard shortcut. If you have to do it anyways, make sure that it is an opt-in, not opt-out feature.

Dev er dev
+3  A: 

A Windows shortcut can have an associated hotkey - right-click on any program in your Start menu, go Properties, and you'll see a "Shortcut key" box. Windows will then look for that hotkey on your behalf and launch your program when it sees it. (I use it to make Ctrl+Alt+C launch Calculator.)

To set this up programmatically, use IShellLink.

RichieHindle
How can I create a shell link programmatically?
Luke
@Luke: http://www.codeproject.com/KB/mcpp/mcppshortcuts.aspx
RichieHindle
+1  A: 

By calling this API:

RegisterHotKey

http://msdn.microsoft.com/en-us/library/ms646309.aspx

http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html

Then override this method "WndProc" to monitor keypresses.

protected override void WndProc(ref System.Windows.Forms.Message m)
 {
  // let the base class process the message
  base.WndProc(ref m);

  // if this is a WM_HOTKEY message, notify the parent object
  const int WM_HOTKEY = 0x312;
  if (m.Msg == WM_HOTKEY) ...............

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(VS.71).aspx

Amr ElGarhy
+2  A: 

This library is pretty good too

Thomas Levesque