7

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.

4 Answers 4

7

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.

2
  • How can I create a shell link programmatically?
    – Luke
    Jun 3, 2009 at 22:14
  • Thank you for this. I had a web page opening every time I went to Save All (Ctrl+Shift+S) on any program and I couldn't figure out why. Turns out there was a Windows shortcut that had that that key combo set. Thanks again.
    – adam0101
    Feb 4, 2022 at 16:06
4

This library is pretty good too

EDIT: I recently created this library, which uses a different approach: NHotkey

4

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

3

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.