Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting application-wide hotkey in C#

There is a post to set system-wide global hotkey in C# here

I want to set application-wide hotkey such that if user presses a hotkey on any child window of an application, a particular window will receive and process it.

Thanks.

like image 942
Liton Avatar asked Dec 07 '10 14:12

Liton


Video Answer


1 Answers

You can create a base form for your application and set keypreview property to true and handle keydown event so all your forms will have the same key definition.

You also can use the following routine to register a hotkey for your forms but in this method, you will require to call same method on load event of each form.

protected override bool ProcessCmdKey(ref Message message, Keys keys)
{
    switch (keys)
    {
        case Keys.F2 | Keys.Control:
            //Process action here.
            return false;
    }

    return false;
}

You also can use the following unmanaged methods from user32.dll but of course i would not suggest that.

static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKey);
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
like image 61
Kadir Sümerkent Avatar answered Oct 05 '22 05:10

Kadir Sümerkent