开发者

How to unregister a specific hotkey using c#

开发者 https://www.devze.com 2023-01-01 20:46 出处:网络
I am using the below code to register a HotKey : RegisterGlobalHotKey(Keys.F4, USE_ALT); private void RegisterGlobalHotKey(Keys hotkey, int modifiers)

I am using the below code to register a HotKey :

RegisterGlobalHotKey(Keys.F4, USE_ALT);

 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 re开发者_开发问答gistration 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);
            }
        }

How can i unregister the Hot key and Make Alt+ F4 keep working again ?


In order to unregister only Alt+F4 you need to call UnregisterHotKey method with the same id which you used for registering it.

See an example here: Hotkeys


You can use the following methods from the win32 api.

[DllImport(“user32.dll”)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

[DllImport(“user32.dll”)]
private static extern bool [UnregisterHotKey][1](IntPtr hWnd, int id); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号