Problem with GetAsyncKeyState

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool IsKeyPressed(int iKey) {  //here is the IsKeyPressed Function

return ((GetAsyncKeyState(iKey) & 1) == 1);

}
        if(IsKeyPressed(VK_RETURN)==true && bFirstTime==true) {  //select
            aux=99;
           iaArray[iSelectedX][iSelectedY]=aux;                  /* WORKS! kinda*/
            bFirstTime=false;
        }
        if(IsKeyPressed(VK_RETURN)==true && bFirstTime==false) {  //confirm

            iaArray[iSelectedX][iSelectedY]=11;                 /* Doesn't WORK! doesn't get executed*/
            bFirstTime=true;
        }
/*
The first if gets executed... the second if will never get executed... so there must be a problem
 within the second if statement at (IsKeyPressed(VK_RETURN)==true) ...
 it probably doesn't return true.. for some reason... Why? 
*/
Last edited on
You need to post more code. The declaration and use of bFirstTime is unclear.

You should reverse the tests as in:
if(IsKeyPressed(VK_RETURN)==true && bFirstTime==true) should be:
if (bFirstTime && IsKeyPressed(VK_RETURN))

and
if(IsKeyPressed(VK_RETURN)==true && bFirstTime==false) should be:
if (!bFirstTime && IsKeyPressed(VK_RETURN))

The order of the tests is important.
Here is the full code:
What the code should do ... There's an 3 by 3 array like the X/O game ... and you can move through the game table by using key UP, DOWN, LEFT, RIGHT.. and when you press ENTER it should copy the number in the selected place and move it in another place when you press ENTER again...
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
#include <iostream>
#include <Windows.h>

using namespace std;


bool IsKeyPressed(int iKey) {

return ((GetAsyncKeyState(iKey) & 1) == 1);

}
void printArray(int iaArray[3][3], int&, int&);

int main () {
    int iSelectedX, iSelectedY;
    int iaArray[3][3];
    bool bFirstTime=true;
    //init array;
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
            iaArray[i][j]=i+j;

        iSelectedX=0;
        iSelectedY=0;

        int aux;

    do{

        system("cls");
        if(IsKeyPressed(VK_UP)) {
           iSelectedX--;
        }
        if(IsKeyPressed(VK_DOWN)) {
           iSelectedX++;
        }
        if(IsKeyPressed(VK_LEFT)) {
           iSelectedY--;
        }
        if(IsKeyPressed(VK_RIGHT)) {
           iSelectedY++;
        }
          //i'm using aux=99 for debug purposes
        if(IsKeyPressed(VK_RETURN)==true && bFirstTime==true) {  //select
            aux=99;
           iaArray[iSelectedX][iSelectedY]=aux;                  /* WORKS! kinda*/
            bFirstTime=false;
        }
         //again for debugging
        if(IsKeyPressed(VK_RETURN)==true && bFirstTime==false) {  //confirm

            iaArray[iSelectedX][iSelectedY]=11;                 /* Doesn't WORK! doesn't get executed*/
            bFirstTime=true;
        }

    printArray(iaArray, iSelectedX, iSelectedY);
Sleep(100);
  }
    while(true);
    return 0;
}


void printArray(int iaArray[3][3], int& x, int& y) {
    int i,j;
         //fix for the out-of-range bug
    if(x>2) { x=2; }
    if(x<0) { x=0; }
    if(y>2) { y=2; }
    if(y<0) { y=0; }
        //end of fix
       
//printing the array:
        for(i=0; i<3; i++) {
          for(j=0;j<3;j++) {

              if(i==x && j==y) {  //if it is the selected square print it nicer
                    cout << "|*|" << iaArray[i][j] << "|*|";
              }
              else { // if it's not the selected square
                    cout << "| |" << iaArray[i][j] << "| |";
              }

          }
            cout << endl << endl;
        }

}
fixed: here the fixed part:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        if(((GetKeyState(VK_RETURN)==true) & 1) && bFirstTime==true) {  //select

           aux=iaArray[iSelectedX][iSelectedY];                  
            bFirstTime=false;
           aX=iSelectedX;
           aY=iSelectedY;
        }
        if(GetKeyState(VK_RETURN)==false && bFirstTime==false) {  //confirm

            iaArray[aX][aY]=iaArray[iSelectedX][iSelectedY];
            iaArray[iSelectedX][iSelectedY]=aux;
            bFirstTime=true;

        }
((GetKeyState(VK_RETURN)==true) & 1)?
Topic archived. No new replies allowed.