How to make a specific key to do something in a console?

So, I know how to do this in windows.h:

 
WM_KEYDOWN: 'A';


But how do I do that in console? Is it the getch() function?
 
getch();

If it is how do I do it?

Anyway what I want to know how to do is like, if I click "a" at anytime in the console, do something.

Any help would be appreciated.
You'll need an event loop. For example, here is something really simple you can play with.

It isn't the only way to do things, but it is structured to prepare you for more advanced event handling concepts when you get to them.

It also does all the stuff that you need to make things work nicely on the Windows Console.

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

//----------------------------------------------------------------------------
// The windows stuff turns off the Ctrl-C handler and stuff like that.
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

struct InitConsole
  {
  DWORD mode;
  HANDLE hStdIn;
  InitConsole(): hStdIn( GetStdHandle( STD_INPUT_HANDLE ) )
    {
    GetConsoleMode( hStdIn, &mode );
    SetConsoleMode( hStdIn, mode & ~ENABLE_PROCESSED_INPUT );
    }
 ~InitConsole()
    {
    SetConsoleMode( hStdIn, mode );
    }
  };

//----------------------------------------------------------------------------
// Extended key handler
// Returns true to ask program to terminate
bool extended_key( int c )
  {
  switch (c)
    {
    // Extended key handlers
    case 72: cout << "up\n"; break;
    case 75: cout << "left\n"; break;
    case 77: cout << "right\n"; break;
    case 80: cout << "down\n"; break;
    
    // The default handler will just tell you what you pressed
    default: cout << "...extended = " << c << "\n"; break;
    }
  return false;
  }

//----------------------------------------------------------------------------
// Normal key handler
// Returns true to ask program to terminate
bool normal_key( int c )
  {
  switch (c)
    {
    // Chain to the extended key handler if needed
    case 0: case 0xE0: 
      return extended_key( getch() );
    
    // Quit if user presses 'q' or Ctrl-C
    case 'q': case 'Q': case 3:
      cout << "Good-bye!\n";
      return true;  
	  
    // Other random stuff
    case 'a': cout << "a\n"; break;
    case 'A': cout << "A\n"; break;
    case 27:  cout << "esc\n"; break;
    case 13:  cout << "return\n"; break;
    
    // The default handler will just tell you what you pressed
    default:  cout << "..." << c << "\n"; break;
    }
  return false;
  }

//----------------------------------------------------------------------------
int main()
  {
  InitConsole _;
  cout << "Turn your NumLock off.\n"
          "Press 'q' to quit.\n"
          "Press other keys to see what happens.\n\n";
  
  // Main event loop
  while (true)
    {
    if (kbhit()) // Here's the event -- a key press
      if (normal_key( getch() ))  // Get the key and pass it to the handler
        break;
    Sleep( 200 );   // A reasonable pause...
    }
    
  return 0;
  }

Try pressing Ctrl-[ and see what happens. Then try pressing the Esc key too. What do the two have in common?

Try pressing Alt-F4 and see what doesn't happen. (There is no event handler for it.)

Try pressing Ctrl-C and see what happens. (There is an event handler for it.)

Have fun!
Well thanks. Wow. I didn't realize it was THAT much just for that. I was expecting a simple function for it. Thanks anyways I will try it
Topic archived. No new replies allowed.