Simulate keyboard input in C#

Viewed 67257

I need to know how to simulate keyboard input for keys W, S, A, D.

I've used SendKeys with no avail as well as the InputSimulator library with no fix.

What I'm trying to do is make it to where applications such as games (Battlefield, Natural Selection, ect. Newer FPS games) will register these simulated keys as real keystrokes. I'm trying to make a virtual controller program where external controls will register as keyboard inputs for these games.

Is there a way to manually simulate keyboard input like there is with something like this?

[DllImport("user32.dll", 
           CharSet = CharSet.Auto, 
           CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, 
                                      uint dx, 
                                      uint dy, 
                                      int cButtons, 
                                      uint dwExtraInfo);
10 Answers

Adding on to the great answer from @gareth, to simulate capital letters during a sentance you can use my solution below..

public static void SendWithShift(ScanCodeShort a){
    HoldLShift();
    Send(a);
    ReleaseLShift();
}
public static void HoldLShift(){
    var Inputs = new InputStruct[1];
    var Input = new InputStruct();
    Input.type = 1; 
    Input.U.ki.wScan = ScanCodeShort.LSHIFT;
    Input.U.ki.dwFlags = KeyEventF.SCANCODE;
    Inputs[0] = Input;
    SendInput(1, Inputs, InputStruct.Size);
}
public static void ReleaseLShift(){
    var Inputs = new InputStruct[1];
    var Input = new InputStruct();
    Input.type = 1; 
    Input.U.ki.wScan = ScanCodeShort.LSHIFT;
    Input.U.ki.dwFlags = KeyEventF.KEYUP | KeyEventF.SCANCODE;
    Inputs[0] = Input;
    SendInput(1, Inputs, InputStruct.Size);
}
public static void Send(ScanCodeShort a){
    var Inputs = new InputStruct[1];
    var Input = new InputStruct();
    Input.type = 1;
    Input.U.ki.wScan = a;
    Input.U.ki.dwFlags = KeyEventF.SCANCODE;
    Inputs[0] = Input;
    SendInput(1, Inputs, InputStruct.Size);
}

To send shift and hold, you simply need to send but to release shift you need to ADD the keyup flag, not just set it.

I was looking for emulation of multimedia button presses and found that you need to use these parameters(in @Gareth code):

Input.U.ki.wVk = VirtualKeyShort.VOLUME_MUTE;
Input.U.ki.dwFlags = KEYEVENTF.UNICODE;

Adding to @ogggre on keybd_event. I managed to have a working DllImport:

[DllImport("user32.dll")]
internal static extern void keybd_event(
    byte bVk,
    byte bScan,
    uint dwFlags,
    uint dwExtraInfo
);

Adding to @Gareth's solution since the keys get stuck after calling Send(). Here is a simplified version of @Matthew Wherry's ReleaseLShift() so that it works with any key.

public static void ReleaseKey(ScanCodeShort key)
{
    var Inputs = new INPUT[1];
    var Input = new INPUT();
    Input.type = 1;
    Input.U.ki.wScan = key;
    Input.U.ki.dwFlags = KEYEVENTF.KEYUP | KEYEVENTF.SCANCODE;
    Inputs[0] = Input;
    SendInput(1, Inputs, INPUT.Size);
}
Related