hi i am trying to make a program that whilst ingame (magicka if anyone is interested) reads what is pressed on the keyboard and the simulates a combination of keyboard key down and keyboar key up and mouse press.

eg if i press NumPad0 it would simulate Key Press A, Key Press S, Key Press F

or if i press NumPad1 it would simulate Shift Down, LButton Press, Shift Up

i have already managed to read what is pressed on the keyboard and know just need to know how to simulate pressing my combination

i have read about DirectX's SendInput and other stuff but was wondering what is the best way to do this so the game will read what inputs i have pressed? and also if anyone has any good tutorials/help sites they can link me to!

Thanks in advance =)

7 Years
Discussion Span
Last Post by cybereality

The Windows API has SendInput, and you need to send the hardware scan code of the key you wish to press for games to detect it (I know this from experience).

I have a small example in C++ here, I'm assuming you can Pinvoke it with no trouble.

#include "stdafx.h"
#pragma comment(lib,"user32")
using namespace std;

int main()
	char ch = 'a';
	INPUT key;
	memset(&key,0,sizeof(INPUT));//Zero the structure.
	key.type = INPUT_KEYBOARD;
	key.ki.dwExtraInfo = GetMessageExtraInfo();//<-- you will need to pinvoke this too.
	key.ki.wScan = 
		static_cast<WORD>(MapVirtualKeyEx(VkKeyScanA(ch), MAPVK_VK_TO_VSC, GetKeyboardLayout(0)));//more pinvoking
	key.ki.dwFlags = KEYEVENTF_SCANCODE;//<-- you will probably have to declare this constant somewhere-
	//in your C# program.

	//Ready to send the key-down event.
	SendInput(1, &key, sizeof(INPUT));
	Sleep(1000);//Wait one second before sending key-up.

	//Sending key-up.
	key.ki.dwExtraInfo = GetMessageExtraInfo();
	key.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;//Key-up need be defined too, or just use the value.
	SendInput(1, &key, sizeof(INPUT));



Overall it seems very tedious to use the raw Winapi w/ C# to do this, there may be a better way that I am not aware of.

It appears as though SendKeys offers the familiar C# level of abstraction, please see here: http://msdn.microsoft.com/en-us/library/ms171548.aspx

Actually it sounds as if a windows low-level keyboard hook would be better for your purpose, you can intercept the input before it reaches the application and either prevent it from reaching it or modify it whilst simulating input in response.

There appears to be a C# wrapper hosted on CodeProject for creating Low-level hooks.

Edited by pseudorandom21: n/a


Oh and I almost forgot, your application has to have equal or higher "authorization" than the application that receives the simulated input.

I'm not quite sure but you may be able to use a windows low-level keyboard hook to remove the injected flag from the input (I have done this). But I'm not sure that will make administrator mode applications detect it (haven't tried that).

By far the simplest solution is to run your application in administrator mode.


Thanks so much pseudorandom21. This totally saved me with an application I am writing. Its basically a mouse/keyboard emulator for games, so you can use exotic controllers like the Nintendo Wiimote. Using SendInput I was able to get mouse events simulated pretty easy, and keyboard events worked in Notepad, but not in games. Your method totally works in games, thanks a bunch man!


If you wish to use some of the classes in C# or another .NET language I might also suggest making a C++/CLI wrapper for them, and using it as a class library, also the project uses the boost library (C++) which you can find an installer for here:

The VS2010 solution file is also included on sourceforge so hopefully you can see how to use boost with Visual Studio (add the lib/include directories to the project settings).

and finally if you don't want to download all the files from sourceforge manually, a Windows SVN app I use is:

with the SVN client you can clone the whole repository.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.