Skip to main content
gamedev.net
🔒 Locked

Simulating keyboard input

Started by Caeddyn 3 replies 950+ views
Original Post
Caeddyn
Caeddyn
Hi, I'm writing a program which grabs what your type on one computer, and types it on another. I have most of this program done, but I'm having trouble simulating key presses correctly.

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
	Sleep(5000);

	keybd_event(VK_SPACE, MapVirtualKey(VK_SPACE, 0), 0, 0);

	Sleep(10000);

	keybd_event(VK_SPACE, MapVirtualKey(VK_SPACE, 0), KEYEVENTF_KEYUP, 0);
	
	return 0;
}

In this sample code I'm trying to hold the spacebar down for 10 seconds. But, what actually happens is the spacebar is being pressed and immediately released. Am I using keybd_event incorrectly? Is there some other function I can use to hold a key down for N seconds? My program MUST work on Windows 98, so I can't use SendInput. Thanks. -Caeddyn
Drew_Benton
Drew_Benton
Quote:
Original post by Caeddyn
In this sample code I'm trying to hold the spacebar down for 10 seconds.


In that case, how about sending that keyboard event 10 seconds straight?
int start = timeGetTime();while( timeGetTime() - start < 10000){   keybd_event(VK_SPACE, MapVirtualKey(VK_SPACE, 0), 0, 0);}

You may need to add in a Sleep(1) inside there at the end so you don't just kill the Windows messaging loop. See if that will work wihtout it first, then add in the sleep and change its values.

OTH,
Quote:
The Sleep function suspends the execution of the current thread for at least the specified interval.


So how about also trying
keybd_event(VK_SPACE, MapVirtualKey(VK_SPACE, 0), 0, 0);int start = timeGetTime();while( timeGetTime() - start < 10000 );keybd_event(VK_SPACE, MapVirtualKey(VK_SPACE, 0), KEYEVENTF_KEYUP, 0);

This way, 10 seconds of nothing is done, but the key is still technically 'held' down. When you call sleep, it'd disrupt that keypress for 10 seconds, thus messing up your results.
Caeddyn
Caeddyn
Heh, for some reason I didn't think about just looping the function. However when I tried your solution, it just pressed the spacebar very, very rapidly. That's not what I'm looking for; it needs to hold the key down. Thanks anyway.

-Caeddyn
Caeddyn
Caeddyn
*bump*

I still haven't fixed the problem and it's very frustrating. Please help.

-Caeddyn
Drew_Benton
Drew_Benton
Ok refering back to my post and this thread I have made a simple program that demonstrates what I am talking about

#include "Main.h"#include <windows.h>#pragma comment( lib, "winmm.lib")int main( int argc, char* argv[] ){	HWND notepad = FindWindow("Notepad","Untitled - Notepad");	if( !notepad )	{	    cout  << "Cannot find window!";	    return 0;	}	HWND control = ::FindWindowEx( notepad, 0, "Edit", "" );	if( !control )	{		cout  << "Cannot find control!";		return 0;	}	SetForegroundWindow(notepad); // bring up the window	SetFocus(notepad); // set focus 	keybd_event( VK_SPACE, 1, 0, 0);	int start = timeGetTime();	while( timeGetTime() - start < 10000 )	{		keybd_event( VK_SPACE, 1, 0, 0);		//Sleep(1); // If you don't use this, then you will be getting a lot of messages at once, when you do use it, then you get that slight delay, but the key is still held down	}	keybd_event(VK_SPACE, 1, KEYEVENTF_KEYUP, 0);	return 0;}


So make a new console program, and paste that in. Add in an include for iostream and a using namespace std as well. Open up notepad and type in a single '|'. Now move the cursor to the left of it. Now compile and run my program and you will see how the space bar is being 'held' down. When you don't use that sleep function, then you won't get the realistic key delay when a button is held. If that's not what you are after, then I'm not sure of any other ways so good luck!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.