API korrekt?



  • #include <math.h>
    #include <iostream.h>
    #include <windows.h>
    
    using namespace std; 
    
    int main() {
      POINT mPos;
        while(1) {
         if(GetAsyncKeyState(VK_RBUTTON)) {
          GetCursorPos(&mPos);
          cout << "X:" << mPos.x << "  Y:" << mPos.y << endl;
          system("CLS");
         }
        } 
    	return 0;
    }
    

    Ersetze ich GetAsyncKeyState(VK_RBUTTON) durch eine 1, klappt es.
    Nur so nicht. Warum? Wenn die rechte maustaste gedrückt ist, sollen nur die koordinaten angezeigt werden. Sonst nicht.

    Kann mir wer helfen?

    (Es klappt insofern nicht, dass die konsole zwar kommt, aber unendlich (scheinbar) läd. Nach 2 Stunden habe ich sie dann geschlossen.



  • Hier nochmal die erweiterte datei:
    Es wird nur einmal kjp ausgegebn.

    #include <iostream>
    #include <windows.h>
    #include <winuser.h>
    #include <math.h>
    #include <stdio.h>
    
    // hier fängt der main teil an
    int main() {
    
    POINT mPos;
    BOOL rc;
    int x,y;
    long lx,ly;
    float dx,dy;
    short dif=60;
    
     while(1) {
    
       if(GetAsyncKeyState(VK_RBUTTON)) { // rechte Maustaste gedrückt?
        printf("kjp");
    
        rc = GetCursorPos(&mPos);  
        x = mPos.x;
        y = mPos.y;
        Sleep(500);
    
        dx = x - lx;
        dy = y - ly;
        if(dx>dif || dy>dif) {
    
          // Rückgabewert entspricht Ziffer auf Nummernpad
          if( (fabs(dx)<dif) && (fabs(dy)<dif) ) printf("0");;
            if( fabs(dx) < fabs( dy / 2 ) ) {
              // vertikale Bewegung
              if( dy > 0 ) printf("2");;
              if( dy < 0 ) printf("8");;
            }
    
          if( fabs(dy) < fabs( dx / 2 ) ) {
            // horizontale Bewegung
            if( dx > 0 ) printf("6");;
            if( dx < 0 ) printf("4");;
          }
    
         // Diagonale Bewegung
         if( (dx > 0) && (dy > 0 ) ) printf("3");;
         if( (dx > 0) && (dy < 0 ) ) printf("9");;
         if( (dx < 0) && (dy > 0 ) ) printf("1");;
         if( (dx < 0) && (dy < 0 ) ) printf("7");;
    
        //cout << x << dx << lx << endl;
        //cout << y << dy << ly << endl;
    
          x = 0;
          lx = 0;
          y = 0;
          ly = 0;
          dy = 0;
          dx = 0;
    }
    return 0;
      }
    }
    }
    


  • Also bei mir klappt dein Code tadellos! Ich habe nur die Header angepasst, damit es sich kompilieren lässt.

    #include <math.h>
    #include <iostream>   //<iostream.h> zu <iostream> geändert
    #include <windows.h> 
    
    using namespace std; 
    
    int main() { 
      POINT mPos; 
      while(1) { 
        if(GetAsyncKeyState(VK_RBUTTON)) { 
          GetCursorPos(&mPos); 
          cout << "X:" << mPos.x << "  Y:" << mPos.y << endl; 
          system("CLS"); 
        } 
      } 
      return 0; 
    }
    


  • Ja, ist korrekt.



  • du wertest den rückgabewert von GetAsyncKeyState nicht richtig aus, man muss testen ob das "most significant bit" gesetzt ist



  • GetAsyncKeyState nicht richtig aus, man muss testen ob das "most significant bit" gesetzt ist

    kannst du dich bitte erklären?



  • if(GetAsyncKeyState(VK_RBUTTON) & 0x8000)
    {
        // rechte Maustaste ist unten
    }
    

Anmelden zum Antworten