sending code to specific window

Hey.

im trying to write a small program than can do some simple tasks for me on the computer. I want the program to be able to press keys in another program without the program being active.

ive written some code to make it press the keys on the keyboard i want, but i dont know how to make it run for the specific program. If i have the specific program running, it presses the keys just fine, but as soon as i tab out of the program, and it starts running in the background instead, it obviously no longer presses the keys in the program, but now in whatever i tabbed into. Is there a way of sending my code to a specific window?

heres my code for the keyboard simulate key pressing


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


*also is there a way of pressing letters with the VK_ funktion? i can only find numbers and other buttons, but not letters :(
Last edited on
also is there a way of pressing letters with the VK_ funktion? i can only find numbers and other buttons, but not letters :(

The letters don't have a vk_code but rather a number: http://www.kbdedit.com/manual/low_level_vk_list.html
so this would mean that "VK_0x41" would write an A?
No, just 0x41.
Topic archived. No new replies allowed.