kijato

getch (C++)

Mar 18th, 2020
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <ios>      // Required for streamsize
  2. #include <iostream>
  3. #include <istream>
  4. #include <windows.h>   // Required for numeric_limits
  5.  
  6. bool getconchar( KEY_EVENT_RECORD& krec )
  7. {
  8.     DWORD cc;
  9.     INPUT_RECORD irec;
  10.     HANDLE h = GetStdHandle( STD_INPUT_HANDLE );
  11.  
  12.     if (h == NULL)
  13.     {
  14.         return false; // console not found
  15.     }
  16.  
  17.     for( ; ; )
  18.     {
  19.         ReadConsoleInput( h, &irec, 1, &cc );
  20.         if( irec.EventType == KEY_EVENT
  21.             &&  ((KEY_EVENT_RECORD&)irec.Event).bKeyDown
  22.             )//&& ! ((KEY_EVENT_RECORD&)irec.Event).wRepeatCount )
  23.         {
  24.             krec= (KEY_EVENT_RECORD&)irec.Event;
  25.             return true;
  26.         }
  27.     }
  28.     return false; //future ????
  29. }
  30.  
  31. int main( )
  32. {
  33.     KEY_EVENT_RECORD key;
  34.     for( ; ; )
  35.     {
  36.         getconchar( key );
  37.         std::cout << "key: " << key.uChar.AsciiChar
  38.             << " code:  " << key.wVirtualKeyCode << std::endl;
  39.         if(key.wVirtualKeyCode==27) exit(0);
  40.     }
  41. }
Add Comment
Please, Sign In to add comment