Advertisement
obernardovieira

Get Pressed Key

Sep 5th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3.  
  4. const int EXTENDED_KEY = 0xE0;
  5. const int ARROW_UP = 'H';
  6. const int ARROW_DOWN = 'P';
  7. const int ESCAPE = 0x1B;
  8.  
  9. int main(){
  10.     for (;;) {
  11.         int ch = _getch();
  12.         if (ch == 0 || ch == EXTENDED_KEY) {
  13.             int ch2 = _getch();
  14.             if (ch2 == ARROW_UP) {
  15.                 printf("Up\n");
  16.             } else if (ch2 == ARROW_DOWN) {
  17.                 printf("Down\n");
  18.             } else {
  19.                 printf("Extended key: %d %x %c\n", ch2, ch2, ch2);
  20.             }
  21.         } else {
  22.             printf("Regular key: %d %x %c\n", ch, ch, ch);
  23.         }
  24.         if (ch == ESCAPE) {
  25.             break;
  26.         }
  27.     }
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement