Home Ask Login Register

Developers Planet

Your answer is one click away!

Michal B. February 2016

How to programatically simulate a button press?

How can I simulate a button press in .NET?

I can do it with AutoHotKey with ease:


Send, {F5}

Any application will know that the button was pressed (just like it was pressed by me). I tried doing the same with:

[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);


static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

or using Windows Input Simulator (https://inputsimulator.codeplex.com/):


They all worked in Notepad or some other apps, but they never worked in my full screen game. Any idea how I can achieve it? I would like a solution which simulates a click globally on the system level.


Gabe February 2016

There is a lib out there called Interceptor that is a wrapper for the windows keyboard driver, however there are limitations - namely win8 or above are not supported - and it seems that work on it has ceased (11 months ago now). Have a look into that if it suits your needs.

I've read somewhere that there are ways to do this via the DirectInput API - as to whether this is the case I'm unsure. I've used Interceptor a few times and it worked for my needs at the time.

Hope this helps

Also Link

Edit: Fixed links

Michal B. February 2016

I learned that my application is using DirectInput (thanks Gabe). This made me focus more on SendInput and I managed to send an "A" key to my application like this:

// A = 30

All key codes are available here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb321074%28v=vs.85%29.aspx

The KeyboardDirectInput class:

public class KeyboardDirectInput
    static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);

    public void Press(VirtualKeys key)
        INPUT[] InputData = new INPUT[2];

        InputData[0].type = (int)InputType.INPUT_KEYBOARD;
        InputData[0].ki.wScan = (short)key;
        InputData[0].ki.dwFlags = (int)KEYEVENTF.SCANCODE;

        InputData[1].type = (int)InputType.INPUT_KEYBOARD;
        InputData[1].ki.wScan = (short)key;
        InputData[1].ki.dwFlags = (int)(KEYEVENTF.KEYUP | KEYEVENTF.SCANCODE);

        if (SendInput(2, InputData, Marshal.SizeOf(InputData[1])) == 0)
            Logger.GetInstance().Warn("SendInput failed (CODE {0})", Marshal.GetLastWin32Error());

    public struct INPUT
        public HARDWAREINPUT hi;
        public KEYBDINPUT ki;
        public MOUSEINPUT mi;
        public int type;

    public struct HARDWAREINPUT
        public int uMsg;
        public short wParamL;
        public short wParamH;

    public struct MOUSEINPUT
        public int dx;
        public int dy;
        public int mouseData;
        public int d 

Post Status

Asked in February 2016
Viewed 3,347 times
Voted 9
Answered 2 times


Leave an answer

Quote of the day: live life