Checking when a key is pressed in the Console

I was working on making a Calculator.
I decided to have the Add to memory as one function.
Now I was thinking of setting it so that if I press A, during runtime, it will activate a function.
I am making this program in Microsoft Visual C++ Express 2010, in Windows 7.
So is it even possible, and if it is how do we do so?
Last edited on
Are you making your calculator console based? Because in that case you probably just want a '+', '-', '*', '/', 'A', or whatever character to be passed into the standard input stream to perform these operations and actions. In that case when the user enters 'A' you could store the last value into a variable (aka memory.) Of course it would have to have been store somewhere else first...
Last edited on
The structure of the program is like 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
28
29
class Calculator_t
{
    //....The functions.....
}Calculator;

//Defining of the Functions in Calculator_t
//..............
//..............

int main()
{
    int a,b;
    char Operator;
    
    //instructions on using the calculator, ad a list of available functions...
    cout << "Number 1:  ";
    cin >> a;

    cout << "Operation:  ";
    cin >> Operator;

    cout << "Number 2:  ";
    cin >> b;

    switch(Operator)
    {
        case '+':/.........similarly for every operator, and a default one.
    }
}


So I actually can't enter 'A' as an Operator.
That is why I wanted to know if there was a way for the console to know when a specific key was being pressed.

I wanted to know one more thing (off this topic):
what is operator?
Some-one?
This is heavily operating system dependent. C++ has no understanding of what a keyboard is.

Try this: http://msdn.microsoft.com/en-us/library/ms646299%28v=vs.85%29.aspx
Hi I have a suggestion ... why dont you try to read the whole expression and then parse it ?
Suppose I want 3+6*10/(100-15)..
I could probably give first input string as 100-15. Then if the next input is A you can store the previous computaion in mem of your calculator and then await the next string which would be 3+6*10/M where M would indicate that you have to divide with the value in the memory. You could probably display at the instructions on using the calculator pahse of your program, how to actually strore and recall calc mem values.

Hope this helps.

Cheers
This is heavily operating system dependent. C++ has no understanding of what a keyboard is.

Try this: http://msdn.microsoft.com/en-us/library/ms646299%28v=vs.85%29.aspx


I guessed it would be OS dependent, that's why I specified the platform in my original post. Even then, its OS dependency isn't a problem as I am making it just to practice with C++ before my classes begin!

So I must first declare the syntax (after including windows.h):
1
2
3
BOOL WINAPI GetKeyboardState(
  __out  PBYTE lpKeyState
);

and then put in the ASCII code for the key I want to bind. And then make an if statement saying, if ( ## == true) and then the code.
But I want to know what should I write in place of ##, and if my method is the correct one.

@acpaluri:
The method
No no, you don't have to declare the function. That gets done for you by including windows.h

You should be able to just use the function, but here's a thread with some examples of how other people are doing it. They don't like GetKeyboardState. I can't comment on that, as I don't use the Win API myself.

http://www.gamedev.net/topic/43463-how-to-use-getkeyboardstate-/
Nisheeth wrote:
I wanted to know one more thing (off this topic):
what is operator?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct Point {
   double x, y;
   Point(double x = 0, double y = 0) : x(x), y(y) {}
};

Point operator+(const Point &a, const Point &b) { return Point( a.x + b.x, a.y + b.y ); }
Point operator-(const Point &a, const Point &b) { return Point( a.x - b.x, a.y - b.y ); }
const std::ostream& operator<<(const std::ostream &out, const Point &p) { return out << '(' << p.x << ',' << p.y << ')'; }

int main() {
   const Point p(5, 3), q(-1, 2), r(0, 8);
   std::cout << p + q + r << std::end;
   return 0;
}
(6,13)
Last edited on
OK, so operator is used to redefine a operator? I actually was about to use operator for the variable, but it turned out to be predefined (highlighted in blue)
Nisheeth wrote:
so operator is used to redefine a[n] operator?
...Why yes!
Argh.

Here's something for you to play with.

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
#include <iostream>
using namespace std;

#include <windows.h>

struct console_t
  {
  DWORD  mode;
  HANDLE hstdin;
  HANDLE hstdout;

  console_t()
    {
    hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    GetConsoleMode( hstdin, &mode );
    }

  ~console_t()
    {
    if (hstdin != INVALID_HANDLE_VALUE)
      SetConsoleMode( hstdin, mode );
    }

  bool iskeypressed( unsigned timeout_ms = 0 ) const
    {
    return WaitForSingleObject( hstdin, timeout_ms ) == WAIT_OBJECT_0;
    }

  int getkeypress() const
    {
    INPUT_RECORD inrec;
    DWORD        count;

    if (hstdin == INVALID_HANDLE_VALUE) return -1;
    SetConsoleMode( hstdin, 0 );

    do ReadConsoleInput( hstdin, &inrec, 1, &count );
    while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);

    SetConsoleMode( hstdin, mode );

    return inrec.Event.KeyEvent.wVirtualKeyCode;
    }

  void gotoxy( int column, int line )
    {
    COORD coord;
    coord.X = column;
    coord.Y = line;
    SetConsoleCursorPosition( hstdout, coord );
    }

  void clearscreen()
    {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    if (hstdout == INVALID_HANDLE_VALUE) return;

    if (!GetConsoleScreenBufferInfo( hstdout, &csbi )) return;
    cellCount = csbi.dwSize.X * csbi.dwSize.Y;

    if (!FillConsoleOutputCharacter(
      hstdout,
      (TCHAR) ' ',
      cellCount,
      homeCoords,
      &count
      )) return;

    if (!FillConsoleOutputAttribute(
      hstdout,
      csbi.wAttributes,
      cellCount,
      homeCoords,
      &count
      )) return;

    SetConsoleCursorPosition( hstdout, homeCoords );
    }
  };

int main()
  {
  console_t console;
  int key = 0;

  console.clearscreen();

  while (key != '3')
    {
    console.gotoxy( 0, 0 );
    cout << "\n"
            "1. Print Greeting\n"
            "2. Print Farewell\n"
            "3. Exit.\n"
            "> ";
    key = console.getkeypress();
    switch (key)
      {
      case '1': cout << "\n\nHello world!\n"; break;
      case '2': cout << "\n\nGood-bye!   \n"; break;
      case '3': cout << "\n\nDone.       \n"; break;
      }
    }

  return 0;
  }

Enjoy!
Topic archived. No new replies allowed.