Keyboard Input Problem

Hello fellow coders,

One week ago I stumbled upon a nasty problem. I need my application to provide keyboard input, I used the keybd_event and SendInput functions but I later discovered that this was not wat I was looking for. The problem is that this function writes keystrokes to for example an open notepad file without any problems, But these keystrokes that are being send are not the same as keystrokes generated from a real keyboard.

I need some way to generate keyboard input trough my application that is exactly the same as keyboard input generated from an actual keyboard.

I looked for some solutions and wat I was able to find was that the windows API is not able to provide me with the function I need. One solution would be writing my own keyboard driver, I am currently not capable of such a project due to my lack of understanding of programming and the windows OS.(I am trying my very best to learn this ATM). Isn't there some way for me to do this other then creating my own keyboard driver?

I use the compiler Visual C++ 2010 Express , And OS windows xp.

EDIT: added SendInput to the function i used
Last edited on
Have you tried SendInput http://msdn.microsoft.com/en-us/library/ms646310%28v=vs.85%29.aspx ?

I don't know what exactly you are trying to do, but that should technically work.
Technically yes I also used SendInput, SendInput superceeds keybd_event and mouse_event, that means the functionality hasn't fundamentally changed. The layer at which the injection occurs is still the same. Wat I want is a method to send keystrokes that resemble REAL keystrokes, A program will be able to tell the difference between this post made by pressing keys on the keyboard and a post made using the SendInput (keybd_event) function. (afcourse only while intercepting keystrokes not after the post is made)
SendInput works just fine - unless the program in question is a game that has cheat prevention measures installed. And as to that, you won't find information on how to cheat games here.
Thanks for your reply, I am not trying to create a game macro, If that was wat you were assuming. I am thinking about creating my own keyboard driver with DDK from MSDN. It is so hard to do wat i want to do that i feel this is my only option.
The problem is that this function writes keystrokes to for example an open notepad file without any problems, But these keystrokes that are being send are not the same as keystrokes generated from a real keyboard.


What do you mean by this? If SendInput simulates keystrokes then the input should appear to notepad exactly as it expects. It sounds like you want to change the way notepad receives or interprets input. I think you need to provide more details.
I'm not sure I understand what you want, but if you have a string which you want to send to the window which has focus using SendInput() use following function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
BOOL SendText( LPCTSTR lpctszText )
	{
	std::vector<INPUT> EventQueue;

	TCHAR Buff[120 * sizeof(TCHAR)] = {0};
	GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, Buff, sizeof(Buff));
	HKL hKeyboardLayout = ::LoadKeyboardLayout( Buff, KLF_ACTIVATE );

	const size_t Len = _tcslen( lpctszText );
	for( size_t Index = 0; Index < Len; ++Index )
		{
		INPUT Event = { 0 };

		const SHORT Vk = VkKeyScanEx(lpctszText[Index], hKeyboardLayout);
		const UINT VKey = ::MapVirtualKey( LOBYTE( Vk ), 0 );

		if( HIBYTE( Vk ) == 1 ) // Check if shift key needs to be pressed for this key
			{
			// Press shift key
			::ZeroMemory( &Event, sizeof( Event ));
			Event.type = INPUT_KEYBOARD;
			Event.ki.dwFlags = KEYEVENTF_SCANCODE;
			Event.ki.wScan = ::MapVirtualKey( VK_LSHIFT, 0 );
			EventQueue.push_back( Event );
			}

		// Keydown
		::ZeroMemory( &Event, sizeof( Event ));
		Event.type = INPUT_KEYBOARD;
		Event.ki.dwFlags = KEYEVENTF_SCANCODE;
		Event.ki.wScan = VKey;
		EventQueue.push_back( Event );

		// Keyup
		::ZeroMemory( &Event, sizeof( Event ));
		Event.type = INPUT_KEYBOARD;
		Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
		Event.ki.wScan = VKey;
		EventQueue.push_back( Event );

		if( HIBYTE( Vk ) == 1 )// Release if previously pressed
			{
			// Release shift key
			::ZeroMemory( &Event, sizeof( Event ));
			Event.type = INPUT_KEYBOARD;
			Event.ki.dwFlags = KEYEVENTF_SCANCODE| KEYEVENTF_KEYUP;
			Event.ki.wScan = ::MapVirtualKey( VK_LSHIFT, 0 );
			EventQueue.push_back( Event );
			}
		}// End for

	if( hKeyboardLayout )
		{
		UnloadKeyboardLayout( hKeyboardLayout );
		}

	return static_cast<BOOL>(::SendInput( static_cast<UINT>(EventQueue.size()), &EventQueue[0], sizeof( INPUT )));
	}
Without knowing much about drivers or the objective of the OP, I am guessing he/she is trying to fool another driver or similar. AFAIK, SendInput() generates the exact same window messages in regular user mode that real keyboards do, so if this is correct, then I conclude he/she is trying to fool a low level layer.
closed account (DSLq5Di1)
Kahiko wrote:
Thanks for your reply, I am not trying to create a game macro, If that was wat you were assuming. I am thinking about creating my own keyboard driver with DDK from MSDN. It is so hard to do wat i want to do that i feel this is my only option.

Then could you explain in more detail what you are trying to do..? and why SendInput is not an option?
I will try to explain it one more time hanst99 came really close to my reall intention. But the program that i want to fool is not a game, Its another application created by a friend of mine that is able to tell wich input came from a real keyboard and wich input came from either SendInput or keybd_event. This is kind of a bet between us, He said if you are able to let a program write into my program you will win. So i took the bet and wrote the program wich used keybd_event to send text but it wouldnt appear on the textbox of the program my friend created. I thought my code was not good so i tested it on a notepad file but it did wrote into that notepad file.

Wat i need is to create keyboard input that is the same as REAL keyboard input. keybd_event and SendInput are not the same as keystrokes you PRESS on the keyboard. I hope this explained wat i am trying to achieve. Thanks in advance

webJose I think your answer is exactly wat i am trying to achieve here.
Topic archived. No new replies allowed.