Advertisement
TermSpar

Get Keyboard Input

Jun 8th, 2016
5,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. #define KEY_UP 72
  8. #define KEY_DOWN 80
  9. #define KEY_LEFT 75
  10. #define KEY_RIGHT 77
  11. #define KEY_X 120
  12.  
  13. int main(){
  14.    
  15.     char playerName;
  16.     cout << "Enter Player Name: ";
  17.     cin >> playerName;
  18.  
  19.     char key = getch();
  20.     int value = key;
  21.  
  22.     while(value != KEY_X){
  23.        
  24.         switch(getch()){
  25.            
  26.         case KEY_UP:
  27.             cout << playerName << " GOES UP\n\n";
  28.             break;
  29.         case KEY_DOWN:
  30.             cout << playerName << " GOES DOWN\n\n";
  31.             break;
  32.         case KEY_LEFT:
  33.             cout << playerName << " GOES LEFT\n\n";
  34.             break;
  35.         case KEY_RIGHT:
  36.             cout << playerName << " GOES RIGHT\n\n";
  37.             break;
  38.         }
  39.  
  40.         key = getch();
  41.         value = key;
  42.  
  43.     }
  44.  
  45.     system("pause");
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement