Search code examples
c#keyboardsendkeys

SendKeys.Send and turning off key modifiers


I want to make ^N work the same as Down arrow in a tree control. I thought I'd just have to add the following to the KeyDown handler:

SendKeys.Send("{Down}");

but this gets treated as a Control-Down arrow since the control key is currently pressed. The msdn page describes how to turn on the control modifier but not how to turn it off.

Thanks, Keith


Solution

  • Sorry to come late to the party but I think I found a solution:

    First, import SetKeyboardState:

    [DllImport("user32.dll")]
    public static extern bool SetKeyboardState(byte[] lpKeyState);
    

    Then, just call it with a zeroed byte array before calling SendKeys.Send():

    SetKeyboardState(new byte[256]);
    SendKeys.Send("your key sequence");
    

    That worked for me. Hope this helps!