简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM