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);
Adding on to the great answer from @gareth, to simulate capital letters during a sentance you can use my solution below..
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;
keybd_event
should do the job:More info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx
Windows Input Simulator makes this super easy.
The Windows Input Simulator provides a simple .NET (C#) interface to simulate Keyboard or Mouse input using the Win32 SendInput method. All of the Interop is done for you and there's a simple programming model for sending multiple keystrokes. Nuget package -> Install-Package InputSimulator
https://inputsimulator.codeplex.com/
Have you tried using
SendInput
which supersedeskeybd_event
?To call SendInput from C#, you're going to need to create a whole bunch of structs. Fortunately, most of this information can be gleaned from pinvoke.net.
Looking at the pinvoke documentation for SendInput and the INPUT struct, I came up with the following. The method I've written at the top called SendInputWithAPI is calling SendInput to simulate the sending of "wsad". The rest of the code is just the structure declarations you need to make it work.
I like Gareth's approach at it. However, in his method, you need to hard code your key strokes into the SendInputWithAPI() method. I made a few changes to the code; you now can call Send() and pass ScanCodeShort parameter to it. This allows you to pass any keystroke to it, any time, any place.