سؤال

I have defined a HotKey Ctrl + Space to activate my Windows Form. When the form handles this HotKey, this event is not seen by the system. Is there subsequently any way to unhandle these keystrokes? (That is, unregister the HotKey.)

Here is what I have tried:

private int tuyen_HotKey_Active = 1208132019;

private void Reg_HotKey(bool mode)
{
    if (mode)
    {
        RegisterHotKey(this.Handle, tuyen_HotKey_Active, 2, 32); // Ctrl + Space
    }
    else
    {
        UnregisterHotKey(this.Handle, tuyen_HotKey_Active);
    }
}

protected override void WndProc(ref Message m)
{            
    if (m.Msg == 0x0312) if(m.WParam.ToInt32() == tuyen_HotKey_Active)
    {
        // Do something here. I want Ctrl + Space keystroke to be
        // unhandled here so that it can be seen by the system.
    }
    base.WndProc(ref m);
}
هل كانت مفيدة؟

المحلول

Create a global bool variable. Set its value to false.

In Windows KeyDown event check if that bool variable is true return from method.

When you want to unhandle these keystrokes set its value to true and when you want to handle these keystrokes set its value to false.

public bool flag = false;

public void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (flag)
        return;

    //.. Code Here something
}

Now you just have to set this flag value to handle or unhandle your keystrokes.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top