Global Keyboard Hooks (C #)

Possible duplicate:
Global keyboard capture in C # application

Can someone help me set up a global keyboard hook for my application?

I want to set hotkeys (e.g. Ctrl+ S) that can be used if they are not focused on the actual form.

+5
source share
2 answers

Paul posts links to two answers, one tells you how to implement the hook, and the other tells you to call RegisterHotKey. You do not need to set the hook for something simple, like the Ctrl + S hotkey, so call RegisterHotKey .

+7

# MessageFilter. , - / .

:

class KeyboardMessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == ((int)Helper.WindowsMessages.WM_KEYDOWN))
        {
            switch ((int)m.WParam)
            {
                case (int)Keys.Escape:
                    // Do Something
                    return true;
                case (int)Keys.Right:
                    // Do Something
                    return true;
                case (int)Keys.Left:
                    // Do Something
                    return true;
            }
        }

        return false;
    }
}

MessageFilter :

Application.AddMessageFilter(new KeyboardMessageFilter());
+2

All Articles