How to make a program that presses a key after a certain amount of time?

Say I want to leave a program running and it stops when I press F9 for instance, I'm looking for something like (and note this is just a generalization).

#include blah
using namespace std;
/wait 2 hours

/key.press = F9

return 0;

well im no pro in C++ programming, in fact im still learning, but i think you should use some sort of loop in order to make your program run until certain conditions met that may cause the program to stop or quit. Maybe a while loop?
@roxmate - you are correct, this is how most programs work. You need a while loop i.e.:

1
2
3
4
do
{
// main program code called:
} while (running);


the program continues to run until the Boolean variable named running is true, when the program needs to end this Boolean variable is set to false and the program ends.
Check this out

1
2
3
4
5
6
7
#include<iostream>
#include<windows.h>

int main()
{
	keybd_event(VK_F9,0,0,0);
}


VK_NUMPAD7 	0x67 	VK_BACK 	0x08
VK_NUMPAD8 	0x68 	VK_TAB 	0x09
VK_NUMPAD9 	0x69 	VK_RETURN 	0x0D
VK_MULTIPLY 	0x6A 	VK_SHIFT 	0x10
VK_ADD 	0x6B 	VK_CONTROL 	0x11
VK_SEPARATOR 	0x6C 	VK_MENU 	0x12
VK_SUBTRACT 	0x6D 	VK_PAUSE 	0x13
VK_DECIMAL 	0x6E 	VK_CAPITAL 	0x14
VK_DIVIDE 	0x6F 	VK_ESCAPE 	0x1B
VK_F1 	0x70 	VK_SPACE 	0x20
VK_F2 	0x71 	VK_END 	0x23
VK_F3 	0x72 	VK_HOME 	0x24
VK_F4 	0x73 	VK_LEFT 	0x25
VK_F5 	0x74 	VK_UP 	0x26
VK_F6 	0x75 	VK_RIGHT 	0x27
VK_F7 	0x76 	VK_DOWN 	0x28
VK_F8 	0x77 	VK_PRINT 	0x2A
VK_F9 	0x78 	VK_SNAPSHOT 	0x2C
VK_F10 	0x79 	VK_INSERT 	0x2D
VK_F11 	0x7A 	VK_DELETE 	0x2E
VK_F12 	0x7B 	VK_LWIN 	0x5B
VK_NUMLOCK 	0x90 	VK_RWIN 	0x5C
VK_SCROLL 	0x91 	VK_NUMPAD0 	0x60
VK_LSHIFT 	0xA0 	VK_NUMPAD1 	0x61
VK_RSHIFT 	0xA1 	VK_NUMPAD2 	0x62
VK_LCONTROL 	0xA2 	VK_NUMPAD3 	0x63
VK_RCONTROL 	0xA3 	VK_NUMPAD4 	0x64
VK_LMENU 	0xA4 	VK_NUMPAD5 	0x65
VK_RMENU 	0xA5 	VK_NUMPAD6 	0x66



edit:
1
2
3
4
5
6
7
8
#include<iostream>
#include<windows.h>

int main()
{
        Sleep(999999) //waiting time
	keybd_event(VK_F9,0,0,0);
}
Last edited on


- I believe that there is a sleep() function in c or c++ that you could make use of.
closed account (Dy7SLyTq)
mmmhhhhhmmm. its in cstdlib. and it works on linux to
Topic archived. No new replies allowed.