Guest User

Win Key tracker

a guest
Feb 11th, 2011
6,796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. const short unsigned int Keyleft  = 37;
  9. const short unsigned int Keytop   = 38;
  10. const short unsigned int Keyright = 39;
  11. const short unsigned int Keydown  = 40;
  12. const short unsigned int Keyexit  = 81;
  13.  
  14. int getKey();
  15.  
  16. int main ()
  17. {
  18.  
  19.     int i;
  20.     cout << "Type a key \n" << endl;
  21.  
  22.     while (true)
  23.     {
  24.         i = getKey();
  25.        
  26.  
  27.         switch( i )
  28.         {
  29.  
  30.         case Keyleft:
  31.             {
  32.                 cout << "you pressed : left" <<endl;
  33.             }
  34.             break;
  35.         case Keytop:
  36.             {
  37.                 cout << "you pressed : up" <<endl;
  38.             }
  39.             break;
  40.         case Keyright:
  41.             {
  42.                 cout << "you pressed : right" <<endl;
  43.             }
  44.             break;
  45.         case Keydown:
  46.             {
  47.                 cout << "you pressed : down" <<endl;
  48.             }
  49.             break;
  50.         case Keyexit:
  51.             {
  52.                 cout << "exit key" <<endl;
  53.             }
  54.             break;
  55.         default:
  56.             {
  57.                 cout << "you pressed : " << i <<endl;
  58.         };
  59.     }
  60.    
  61.     system ("pause");
  62.     return 0;
  63. }
  64.  
  65. int getKey()
  66. {
  67.     while (true)
  68.     {
  69.         for(int i = 8; i <= 256; i++)
  70.         {
  71.             if(GetAsyncKeyState(i) & 0x7FFF)
  72.             {
  73.                
  74.                 // This if filters the keys, i want to allow direction arrows
  75.                 // and q for quit. If you want to add more just add the code for the key,
  76.                 // to know the key code just coment the if line and print the keycode.
  77.                 if( ( i >= 37 && i <= 40 ) || i == 81 )
  78.                 return i;
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment