Remove Keys.Control Control.ModifierKeys when Ctrl key is not down
<p>I have a third-party library which sends <code>Ctrl-C</code> to copy from the clipboard. If the user happens to physically press Ctrl key at the same time, I can be left with Ctrl modifier even when the Ctrl key is no longer pressed. Somehow, the keyup event is missed.</p>

<p>This can be detected using Control.ModifierKeys, and GetKeyState. The former returns "Keys.Control" and the latter returns that the <code>CtrlKey</code> is NOT down.</p>

<pre><code>if (!ctrlDown &amp;&amp; Control.ModifierKeys == Keys.Control)
{
   // Do something to get rid of the Control modifier key
}
</code></pre>

<p>What can I do?</p>

<p><strong>Update</strong> A partial solution seems to be to send Ctrl right after the event as Yorgye Nathan suggested. I currently use</p>

<pre><code>    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
    public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
    public const int VK_RCONTROL = 0xA3; // RControl key code
    public const int VK_LCONTROL = 0xA2; // LControl key code
</code></pre>

<p>and</p>

<pre><code>            if (!ctrlDown &amp;&amp; ctrlMod == Keys.Control)
            {
                onNotify("Detected stuck Ctrl+, unsticking...");
                keybd_event(VK_LCONTROL, 0, KEYEVENTF_EXTENDEDKEY, 0);
                keybd_event(VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0);                    
            }
</code></pre>

<p>which fixes everything except the active window. I suspect I need to send the control to a specific window, rather than just registering a keybd event.</p>
