Trying to keep track of keys pressed

For some reason, it doesn't print anything correctly. EnterPressed is "essed", and characters on the keyboard come out as "@" or nothing. I'm a bit of an amateur, so please be specific. Thanks in advance!

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
#include<iostream>
#include<windows.h>
#include<fstream>
#include<conio.h>

void UpdateTxt(char input, int type)//Why is updatetext being called
{
    std::ifstream dataFileOut("DataFile.txt");

    std::string currentData;
    std::string item;

    while(dataFileOut >> item)
    {
        currentData += " " + item;
    }

    dataFileOut.close();

    std::ofstream dataFileIn("DataFile.txt");

    if(type == 1)
    {
        //printf("Calling");

        dataFileIn << currentData << input;

        printf(input + "\n");
    }
    else if(type == 2)
    {
        dataFileIn << currentData << "__";

        printf("__\n");
    }
    else if(type == 3)
    {
        dataFileIn << currentData << "_EnterPressed_";

        printf("_EnterPressed_\n");
    }
    else if(type == 4)
    {
        dataFileIn << currentData << "_BackPressed_";

        printf(input + "_BackPressed_\n");
    }

    dataFileIn.close();
}

int main()
{
    bool spaceHit = false;
    bool enterHit = false;
    bool backspaceHit = false;

    while(true)//make it work outside of application
    {
        if(kbhit())
        {
            char keyHit = getch();

            UpdateTxt(keyHit, 1);
        }
        else if(GetAsyncKeyState(VK_SPACE))
        {
            spaceHit = true;
        }
        else if(!GetAsyncKeyState(VK_SPACE) && spaceHit == true)
        {
            spaceHit = false;

            UpdateTxt(' ', 2);
        }
        else if(GetAsyncKeyState(VK_ACCEPT))
        {
            enterHit = true;
        }
        else if(!GetAsyncKeyState(VK_ACCEPT) && enterHit == true)
        {
            enterHit = false;

            UpdateTxt(' ', 3);
        }
        else if(GetAsyncKeyState(VK_BACK))
        {
            backspaceHit = true;
        }
        else if(!GetAsyncKeyState(VK_BACK) && backspaceHit == true)
        {
            backspaceHit = false;

            UpdateTxt(' ', 4);
        }
    }
}
You are:
  • throwing away data
  • mixing input methods

I'm not sure I follow some of your commentary either:

  • “why is updatetxt being called?”
    ...because you are calling it in main?

  • “make it work outside of application”
    ...how exactly is code supposed to run if your program is not running?

You really should take the time to read the documentation for a function, and obey it. The getch() function reads all the keys you are testing for with GetAsyncKeyState(), so... the program is not doing what you think it is. Toss the GetAsyncKeyState() stuff and just use getch().

The getch() function is a low-level function that returns PC keyboard scan codes. Extended keys (like arrow keys) come as two key codes: '\0' or '\xE0' followed by the extended key scan code.

Good luck!
Thanks for the help Duth. Sorry for the commentary, that wasn't intended for you to read. They were brief notes from the past I forgot to delete. I'll look into getch though.
Last edited on
The general form for using getch() is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int ch;
  switch (ch = getch())
  {
    case 0:
    case 0xE0:
      switch (ch = getch())
      {
      case 72: cout << "up arrow\n"; break;
      case 75: cout << "left arrow\n"; break;
      case 77: cout << "right arrow\n"; break;
      case 80: cout << "down arrow\n"; break;
      default: cout << "extended key " << c << "\n";
      }
      break;
    case   9: cout << "backspace\n"; break;
    case  13: cout << "return\n"; break;
    case ' ': cout << "space\n"; break;
    default:  cout << "normal key " << c << "\n";
  }

Hope this helps.
Last edited on
Topic archived. No new replies allowed.