繁体   English   中英

如何使用c#注册三键组合的热键

[英]How to Register Hotkey for Three key combination using c#

我使用下面的代码完成了两个组合键的全局热键。 如何对三个键组合进行相同的操作,如(ctrl + shift + esc)(ctrl + shift + tab)??

两个组合键的代码:

   var TabShift = Keys.Tab | Keys.Shift;
   RegisterGlobalHotKey(TabShift, USE_ALT);


    DllImport("user32.dll")]


     private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);

    [DllImport("user32.dll")]
    private static extern int UnregisterHotKey(IntPtr hwnd, int id);


    private void RegisterGlobalHotKey(Keys hotkey, int modifiers)
    {
        try
        {
            // increment the hot key value - we are just identifying
            // them with a sequential number since we have multiples
            mHotKeyId++;

            if (mHotKeyId > 0)
            {
                // register the hot key combination
                if (RegisterHotKey(this.Handle, mHotKeyId, modifiers, Convert.ToInt16(hotkey)) == 0)
                {
                    // tell the user which combination failed to register -
                    // this is useful to you, not an end user; the end user
                    // should never see this application run
                    MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " +
                        Marshal.GetLastWin32Error().ToString(),
                        "Hot Key Registration");
                }
            }
        }
        catch
        {
            // clean up if hotkey registration failed -
            // nothing works if it fails
            UnregisterGlobalHotKey();
        }
    }

    private void UnregisterGlobalHotKey()
    {
        // loop through each hotkey id and
        // disable it
        for (int i = 0; i < mHotKeyId; i++)
        {
            UnregisterHotKey(this.Handle, i);
        }
    }

您可以简单地将“或”值组合在一起:

// Alt + Shift + Tab
RegisterGlobalHotKey(Keys.Tab, MOD_ALT | MOD_SHIFT);

请注意, MOD_ALT和朋友在WinUser.h中定义为:

#define MOD_ALT         0x0001
#define MOD_CONTROL     0x0002
#define MOD_SHIFT       0x0004
#define MOD_WIN         0x0008

因此,您必须确保在那里为修改器传递正确的值。

像这样:

Keys.Control | Keys.Shift | Keys.Tab

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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