Help making C++ accept input without having to hit enter?

Hey all.

I'm using Windows 7, and I need to know how to make my C++ program accept input without having to hit enter. I was told to come here because it might require the use of virtual keys, or the Windows API. Problem is I have no idea what either of those are.

Someone linked me here: http://msdn.microsoft.com/en-us/library/ms927178. But I can't figure out how this helps me.

Can anyone help me out? I'm a noob here.

Thanks in advance.
Windows API handles thing such as Windows, keyboard input, message boxes, Buttons, Graphics in general, C++ works in the background. thats what API stands for is Application Programming Interface. I would help you more but it's been such a long time since i did win32 programming i forgot quite a bit. but someone here will be able to help you more.
Yea this seems a bit over my head. If someone could walk me through it, or direct me to somewhere where I could start from the ground up with this stuff, I would be grateful.
I found a program on my computer i made a long time ago that when you press a it says you pressed a but not when you press anything else. I believe this is along the lines of what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int a;

        while(true)
        {
            a=GetAsyncKeyState(0x41);
            if(a)
            {
                cout << "you pressed a" << endl;
                Sleep(150);
            }
        }
}


Let me know if you have any questions.
Last edited on
That appears to be what I need. Let me see if I can get that to work in my program and I'll get back to you.

Thanks
Ok, if you cant let me see your program and maybe i can try to get it to work.

EDIT: you can also change which key you want pressed by changing the hex value in the parameters of GetAsyncKeyState Look at this for a list of them all

http://www.kbdedit.com/manual/low_level_vk_list.html
Last edited on
Okay, I can't figure out how to make what you've done trigger my timer to reset.
Basically, my program is a countdown timer, and my goal is to get the "v" key to reset it without having to press enter. Here it is
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;

 int main()
{
	//variables
int time=21;
char x=' ';

for (char x='v';x=='v';)
{
	cout << time << endl;
	Sleep(100);
	cout << time-1 << endl;
	Sleep(100);
	cout << time-2 << endl;
	Sleep(100);
	cout << time-3 << endl;
	Sleep(100);
	cout << time-4 << endl;
	Sleep(100);
	cout << time-5 << endl;
	Sleep(100);
	cout << time-6 << endl;
	Sleep(100);
	cout << time-7 << endl;
	Sleep(100);
	cout << time-8 << endl;
	Sleep(100);
	cout << time-9 << endl;
	Sleep(100);
	cout << time-10 << endl;
	Sleep(100);
	cout << time-11 << endl;
	Sleep(100);
	cout << time-12 << endl;
	Sleep(100);
	cout << time-13 << endl;
	Sleep(100);
	cout << time-14 << endl;
	Sleep(100);
	cout << time-15 << endl;
	Sleep(100);
	cout << time-16 << endl;
	Sleep(100);
	cout << time-17 << endl;
	Sleep(100);
	cout << time-18 << endl;
	Sleep(100);
	cout << time-19 << endl;
	Sleep(100);
	cout << time-20 << endl;
	Sleep(100);
	cout << time-21 << endl;

 

	int v;
	while(true)
	{
	v=GetAsyncKeyState(0x56);
            if(v)
			{
				cout << "you pressed v";
			}
	}
}

	

	return 0;
}


I know there's an easier way to clean the code up, but I'll worry with that later. Sleep values will be adjusted properly later.
I got it!
I'm embarrassed to post my terrible coding, but here it is:
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
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;

 int main()
{
	//variables
int time=21;
char x=' ';

	int v;
	while(true)
	{
	v=GetAsyncKeyState(0x56);
            if(v)
			{
				cout << time << endl;
	Sleep(100);
	cout << time-1 << endl;
	Sleep(100);
	cout << time-2 << endl;
	Sleep(100);
	cout << time-3 << endl;
	Sleep(100);
	cout << time-4 << endl;
	Sleep(100);
	cout << time-5 << endl;
	Sleep(100);
	cout << time-6 << endl;
	Sleep(100);
	cout << time-7 << endl;
	Sleep(100);
	cout << time-8 << endl;
	Sleep(100);
	cout << time-9 << endl;
	Sleep(100);
	cout << time-10 << endl;
	Sleep(100);
	cout << time-11 << endl;
	Sleep(100);
	cout << time-12 << endl;
	Sleep(100);
	cout << time-13 << endl;
	Sleep(100);
	cout << time-14 << endl;
	Sleep(100);
	cout << time-15 << endl;
	Sleep(100);
	cout << time-16 << endl;
	Sleep(100);
	cout << time-17 << endl;
	Sleep(100);
	cout << time-18 << endl;
	Sleep(100);
	cout << time-19 << endl;
	Sleep(100);
	cout << time-20 << endl;
	Sleep(100);
	cout << time-21 << endl;
			}
	
}
	return 0;
}


You have been extremely helpful! Can't thanks you enough!
Lol i just was about to post the same exact setup :P well glad you figured it out. Good luck :)
You need to read up on for loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	//variables
    int time=21;
    char x=' ';

    int v;
    while(true)
    {
        v=GetAsyncKeyState(0x56);
        if(v)
        {
            for(; time >= 0; --time)
            {
                cout << time << endl;
                Sleep(100);
            }
        }
    }
    return 0;
}


EDIT: fixed indentation
Last edited on
Topic archived. No new replies allowed.