Keyboard output

Hello.

I would like to try and create a small program which for example can make my keyboard press a key.

int main()
press tabulator
delay 1000 ms
press 1
repeat

something like that, but i cant find out how to make my keyboard the output?i tried searching around for it, but was unable to find anything to really help me. I think i've read that its OS depending, and i would like to make the program for windows 7. Im very new at this, and any help would be greatly appreciatet.

Thanks alot!
closed account (3qX21hU5)
Could you elaborate a little bit on what you mean by make your keyboard press a key?

I'm not sure I am understanding what exactly you are trying to do. From your example it looks like you want the program to simulate pressing the tab key then delay for a second then press the number 1 and then repeat the process. Is this correct?

If it is you are correct you will need to use win32 most likely to accomplish this for windows and you could use the SendInput() function to do this http://msdn.microsoft.com/en-us/library/ms646310%28v=vs.85%29.aspx

Though since you are just beginning the win32 api might be a bit hard to understand so you might want to get the hang of the basics first before moving on to this.
that is exactly what i want it to do :)

i will try to look into the SendInput() function and hope to understand it, although it looks pretty complicated

thanks !
Last edited on
ok i understood nothing about SendInput() so i tried looking into keybd_event

tried making alittle something with it, but.. yea well.. would this work? i guess this repeats it 100 times before stopping, with a delay of 2 seconds between each run? atleast im hoping thats what it does¨


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
#include <Windows.h>

void SetNUMPAD1 ( BOOL bState )
{
	BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_NUMPAD1] & 1)) ||
   (!bState && (keyState[VK_NUMPAD1] &1)) )
{
	// Simulate a key press
	keybd_event( VK_NUMPAD1,
		0x45,
		KEYEVENTF_EXTENDEDKEY | 0,
		0 );

	//Simulate a key press

	keybd_event( VK_NUMPAD1,
		0x45,
		KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
		0);
}
	
}
void SetTAB ( BOOL bState )
{
	BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_TAB] & 1)) ||
   (!bState && (keyState[VK_TAB] &1)) )
{
	// Simulate a key press
	keybd_event( VK_TAB,
		0x45,
		KEYEVENTF_EXTENDEDKEY | 0,
		0 );

	//Simulate a key press

	keybd_event( VK_TAB,
		0x45,
		KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
		0);
}
	
}
void main()
{
	int i = 0;
	while(i<=100)
	{
	SetTAB( TRUE );
	SetNUMPAD1( TRUE );
	i++;
	Sleep(2000);
	}
}


**EDIT 18:08 18/06/2013


ok this totally works now.

Is there a way of sending this into a window that is not active?

for example, i run this program, which sends the keyboard pressing into another program that is not active on the desktop, but runs in the background?

say i want to surf the internet while the program is running and sending the keyboard-pressing into a second program?
Last edited on
Topic archived. No new replies allowed.