Advertisement
Elfik

GetAsyncKeyState

Mar 3rd, 2018
1,281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <chrono>
  4.  
  5. char keyboardCheck() {
  6.    
  7.     using namespace std::chrono;
  8.     char controls[] = { 'W','S','A','D','E','Q','R'};
  9.     char keyToCheck = 0;
  10.     static char lockedKey;
  11.     static steady_clock::time_point start;
  12.     steady_clock::time_point end;
  13.     int tick = 200;
  14.     bool iWantToMove = false;
  15.  
  16.     for (short i = 0; i < sizeof(controls) / sizeof(char); i++) {
  17.         if (GetAsyncKeyState(controls[i])) {
  18.             if (keyToCheck != 0) return 0;
  19.             keyToCheck = controls[i];
  20.         }
  21.     }
  22.    
  23.     end = steady_clock::now();
  24.    
  25.     if (keyToCheck == 'W' || keyToCheck == 'S' || keyToCheck == 'A' || keyToCheck == 'D')
  26.         iWantToMove = true;
  27.    
  28.     if (duration_cast<milliseconds>(end - start).count() > tick && iWantToMove) lockedKey = 0;
  29.  
  30.     if (keyToCheck != lockedKey) {
  31.         start = steady_clock::now();
  32.         lockedKey = keyToCheck;
  33.         return keyToCheck;
  34.     }
  35.     else return 0;
  36. }
  37.  
  38. int main(){
  39.    
  40.     while(true) {
  41.    
  42.         char click = keyboardCheck();
  43.         if(click) std::cout << click;
  44.        
  45.     }
  46.    
  47.     return 1;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement