reading letter keys

i would like to read all the keys but i had to enter it in the hard why which is letter by letter is there a simpler way and i also only reacts to the key (p)

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
#include <iostream>
#include <conio.h>
#include <windows.h>
#define _VK_A 0x50
#define _VK_B 0x50
#define _VK_C 0x50
#define _VK_D 0x50
#define _VK_E 0x50
#define _VK_F 0x50
#define _VK_G 0x50
#define _VK_H 0x50
#define _VK_I 0x50
#define _VK_J 0x50
#define _VK_K 0x50
#define _VK_L 0x50
#define _VK_M 0x50
#define _VK_N 0x50
#define _VK_O 0x50
#define _VK_P 0x50
#define _VK_Q 0x50
#define _VK_R 0x50
#define _VK_S 0x50
#define _VK_T 0x50
#define _VK_U 0x50
#define _VK_V 0x50
#define _VK_W 0x50
#define _VK_X 0x50
#define _VK_Y 0x50
#define _VK_Z 0x50

using namespace std;

int main()
{
	int key;
	while (true)
	{
		if (GetAsyncKeyState(_VK_A), GetAsyncKeyState(_VK_B), GetAsyncKeyState(_VK_C), GetAsyncKeyState(_VK_D), GetAsyncKeyState(_VK_E), GetAsyncKeyState(_VK_F), GetAsyncKeyState(_VK_G), GetAsyncKeyState(_VK_H), GetAsyncKeyState(_VK_I), GetAsyncKeyState(_VK_J), GetAsyncKeyState(_VK_K), GetAsyncKeyState(_VK_L), GetAsyncKeyState(_VK_M), GetAsyncKeyState(_VK_N), GetAsyncKeyState(_VK_O), GetAsyncKeyState(_VK_P), GetAsyncKeyState(_VK_Q), GetAsyncKeyState(_VK_R), GetAsyncKeyState(_VK_S), GetAsyncKeyState(_VK_T), GetAsyncKeyState(_VK_U), GetAsyncKeyState(_VK_V), GetAsyncKeyState(_VK_W), GetAsyncKeyState(_VK_X), GetAsyncKeyState(_VK_Y), GetAsyncKeyState(_VK_Z))
		{
			cout << "you are pressing a button" << endl;
			break;
		}
		else
		{
			cout << "you are not pressing any buttons" << endl;
			continue;
		}
		return 0;
	}
}
Well you defined all the key values as the same value (0x50, which is probably the value used for P). Also, rather than using the comma operator (,) in your if statement you probably want the logical or operator (||).

You're also returning immediately after one iteration of your loop, so it seems unlikely to me your program will react at all.

What exactly was your goal with this? Checking every key like that, as you've noticed, is annoying to write (and read).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <conio.h>
#include <windows.h>

#include <iostream>
using namespace std;
int main()
{
    while(true)
    {
        if(_kbhit()) { cout << "you are pressing a button" << endl; break; }
        else cout << "you are not pressing any buttons" << endl;
    }
    _getch();
    return 0;
}
Last edited on
An alternate way would be this :

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
#include <iostream>
using namespace std;
#include <windows.h>
#include <conio.h>
#define _VK_A 0x41
#define _VK_Z 0x5A
int GetAsyncLetter()
{
    int i;
    bool k=false;
    for(i=_VK_A;i<=_VK_Z;++i)
        if(GetAsyncKeyState(i)) {
        k=true; break;
        }
    return (k?i:k);
}

int main()
{
    char key;
    while(true)
    {
        if(key=GetAsyncLetter()) cout<<(char)MapVirtualKeyA(key,2)<<endl;
        else cout<<"no key"<<endl;
    }
    return 0;
}
programming challenge check it out-->> http://helpprogram.webeden.co.uk/
@HelpProgram thats ultra fake.Now stop spaming !!
Topic archived. No new replies allowed.