How to implement Global Hotkeys in C#?

I need to write an application which globally intercepts Alt+Shift+S.

What I did is I created a DLL which sets global hooks:


namespace Hotkeydll
{
    public class MyHotKey
    {
        public static void setHooks()
        {
            KeyboardHookProcedure = new HookProc(KeyboardHookProc);
            hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
        }

        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
        	//write something into file
        }
   }
}

Then I created a program which loads this DLL and set the hook:



using Hotkeydll;
namespace IWFHotkeyStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            MyHotKey.setHooks();
        }
    }
}

Now the problem is that the hotkey doesn’t work.

It looks like the DLL is not loaded permanently into memory. I see that I can delete the dll file from file system.

So please advise what I am doing wrong?

Should I use a different approach?

Thank you.

I would imagine that your application will have to be running in order to intercept key press events. Possibly an .exe with no UI that is set to auto run on boot?