Member Avatar for geekman92

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 =)
Ollie

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));
}

http://www.pinvoke.net/default.aspx/user32/sendinput.html

http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx

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

--edit--
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.
http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx

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!

Cool man! I will check that out. Thanks.

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:
http://www.boostpro.com/download/

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:
http://tortoisesvn.net/

with the SVN client you can clone the whole repository.

Ok thanks. I am actually using C++ for this project, so it should be fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.