Getting input from keyboard problem

Hey guys,

first off my code is messy, none modular and hard to read but I'm not making any useful program, I'm just learning how to use the GetAsyncKeyState from the windows API to get input from a user.

anyhow I have followed many online sources but for some reason the program fails to read from the keyboard,

anyone possibly know why?

thanks

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  #include <iostream>
#include <cstdlib>
#include <windows.h>

using namespace std;

char sprite = 'X';

void printBoard(char buffer[][30])
{

    for(int i = 0; i < 20; i++)
    {

        for(int j = 0; j < 30; j++)
        {

            if(buffer[i][j] == sprite)
            {

                cout << sprite;
            }
            else
            {
                cout << buffer[i][j];
            }

        }
        cout << endl;
    }
}

void keyPress(bool& right,bool& left,bool& down,bool& up){

  if(GetAsyncKeyState(VK_LEFT & 0x8000)){

    right = false;
    down = false;
    up = false;
    left = true;
  }
  if(GetAsyncKeyState((VK_RIGHT) & 0x8000)){

    left = false;
    down = false;
    up = false;
    right = true;
  }
  if(GetAsyncKeyState(VK_UP & 0x8000)){

    down = false;
    right = false;
    left = false;
    up = true;
  }
  if(GetAsyncKeyState('f') & 0x8000){

    up = false;
    right = false;
    left = false;
    down = true;
    cout << "HI" << endl;
  }

}


int main()
{

    while(true){

        if(GetAsyncKeyState(VK_LEFT) & 0x8000){

            cout << "HEY" << endl; // not working :/
        }

    }

    char buffer[20][30];

    for(int i = 0; i < 20; i++)
    {

        for(int j = 0; j <30; j++)
        {

            if(i == 0)
            {

                //cout << "yes" << endl;
                buffer[i][j] = '#';
                continue;
            }
            if(i == 19)
            {

                buffer[i][j] = '#';
                continue;
            }
            if(j == 0)
            {

                buffer[i][j] = '#';
                continue;
            }
            if(j == 29)
            {

                buffer[i][j] = '#';
                continue;
            }

            buffer[i][j] = ' ';

        }
    }

    int xPos = 15;
    int yPos = 10;
    bool left = false;
    bool right = false;
    bool up = false;
    bool down = false;

    buffer[yPos][xPos] = sprite;

    keyPress(right,left,down,up);


    while(1)
    {

        printBoard(buffer);
        system("cls");

        if(right)
        {
            xPos++;

            if(buffer[yPos][xPos] == '#')
            {

                right = false;
                left = true;
                xPos--;
            }
            else
            {
                buffer[yPos][xPos-1] = ' ';
                buffer[yPos][xPos] = sprite;
            }
        }
        
        if(left){

            xPos--;

            if(buffer[yPos][xPos] == '#')
            {
                left = false;
                right = true;
                xPos++;
            }
            else
            {
                buffer[yPos][xPos+1] = ' ';
                buffer[yPos][xPos] = sprite;
            }
        }
    }
}
**update

strange when I include Windows.h with a capital 'w' everything works as expected, I wonder why windows.h with a lower case doesn't work, are they both valid headers? whats the diff?

thanks
There should be no difference unless you're doing something weird like cross-compiling for Windows from Linux. (Windows filesystem is case-insensitive, Linux isn't)

Can you change it back to lowercase and then re-compile and see if it starts to fail again?
Last edited on
strange seems to be working now with lower case w, wonder what was wrong first time around.
Topic archived. No new replies allowed.