Continue when a certain key is pressed/get pressed key

Hi. I'm kind of new to C++ and I was wondering if it's possible to make a program pause until a certain key is pressed. (I don't mean any key, I mean a certain key.

Also, is it possible to set a parameter's value to the key pressed?
I use Windows XP and my compiler is Visual C++ 2010 professional.

Thanks!
Last edited on
I'm surprised nobody answered you coding4fun. That's easy to do. When a key is pressed, a key code is generated by the keyboard driver. There are ways to obtain this key code using the C Standard Library functions, the C++ Standard Library functions, and the Windows Api. For example, the Asci code (decimal) of the numeric zero key is 48. I havn't played with this stuff in awhile, but was quickly able to dig this up for you. There are many other ways to do it...

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
//Main.cpp
#include <stdio.h>
#include <conio.h>
#include <string.h>


typedef struct KEY
{
 bool blnKeyPressed;   //1 byte
 bool blnExtended;     //1 byte
 char ch;              //1 byte
 char code;            //1 byte
}KEY;


void Inkey(KEY& k)
{
 int ch;

 memset(&k,0,4);
 if(_kbhit())
 {
    ch=getch();
    if(ch==0x00||ch==0xE0)
    {
       k.blnExtended=true,  k.blnKeyPressed=true;
       k.ch='\0',           k.code=getch();
    }
    else
    {
       k.blnExtended=false, k.blnKeyPressed=true;
       k.ch=(char)ch,       k.code=(char)ch;
    }
 }
}


int main(void)
{
 KEY key;

 printf("key.blnKeyPressed\tkey.blnExtended\t\tkey.ch\t\tkey.code\n");
 printf("========================================================================\n");
 while(1)
 {
  Inkey(key);
  if(key.blnKeyPressed)
  {
     if(key.code==13||key.code==27)
        break;
     printf("%u\t\t\t%u\t\t\t%c\t\t%u\n",key.blnKeyPressed,key.blnExtended,key.ch,key.code);
  }
 };

 return 0;
}


Compile and run the program as a console program. Any key you hit will show up in the display. You can end the program by hitting the [ENTER] key (13) or the [ESCAPE] key (27). Hope this helps!
Last edited on
Here's an even better one for you. It not only shows keys pressed, but mouse movements too!. It is fairly advanced though, and uses the kernel functions in the Windows Api. Hit [ESCAPE] to exit.

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
#include <windows.h>
#include <stdio.h>

int main(void)
{
 HANDLE hStdInput,hStdOutput;
 INPUT_RECORD ir[128];
 bool blnLoop=true;
 DWORD nRead;
 COORD xy;
 UINT i;

 hStdInput=GetStdHandle(STD_INPUT_HANDLE);
 hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleMode(hStdInput,ENABLE_MOUSE_INPUT|ENABLE_EXTENDED_FLAGS);
 FlushConsoleInputBuffer(hStdInput);
 do
 {
   if(WaitForSingleObject(hStdInput,INFINITE)==WAIT_TIMEOUT)
      break;
   else
   {
      ReadConsoleInput(hStdInput,ir,128,&nRead);
      for(i=0;i<nRead;i++)
      {
          switch(ir[i].EventType)
          {
           case KEY_EVENT:
             if(ir[i].Event.KeyEvent.wVirtualKeyCode==VK_ESCAPE)
                blnLoop=false;
             else
             {
                xy.X=0;xy.Y=0;
                SetConsoleCursorPosition(hStdOutput,xy);
                printf
                (
                 "AsciiCode = %d: symbol = %c\n",
                 ir[i].Event.KeyEvent.uChar.AsciiChar,
                 ir[i].Event.KeyEvent.uChar.AsciiChar
                );
             }
             break;
           case MOUSE_EVENT:
             xy.X=0, xy.Y=1;
             SetConsoleCursorPosition(hStdOutput,xy);
             printf
             (
              "%.3d\t%.3d\t%.3u",
              ir[i].Event.MouseEvent.dwMousePosition.X,
              ir[i].Event.MouseEvent.dwMousePosition.Y,
              (unsigned int)ir[i].Event.MouseEvent.dwButtonState & 0x07
             );
             break;
          }//end switch
      }//end for
   }//end if
 }//end do
 while(blnLoop==true);

 return 0;
}


Awesome, thanks! Both programs work!
Topic archived. No new replies allowed.