GetAsyncKeyState



  • Folgendes Problem:

    #include "Windows.h"
    
    ...
    
    public: array<short> ^A1;
    
    ...
    
    A1 = gcnew array<short>(256);
    
    ...
    
    	private: void Start()
    			 {
    				 for( int i = 0; i < 256; i++)
    				 {
    					 A1[i] = GetAsyncKeyState(i);
    				 }
    
    				 for( int i = 0; i < 256; i++)
    				 {
    					 if( A1[i] == -32767)
    					 {
    						 DoDiesUndDas()
    					 }
    				 }
    			 }
    
    ...
    

    error LNK2028: Nicht aufgelöstes Token (0A00000F) ""extern "C" short __stdcall GetAsyncKeyState(int)" (?GetAsyncKeyState@@$$J14YGFH@Z)", auf das in Funktion ""private: void __clrcall mg2::Form1::Start(void)" (?Start@Form1@mg2@@$$FA$AAMXXZ)" verwiesen wird.

    error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""extern "C" short __stdcall GetAsyncKeyState(int)" (?GetAsyncKeyState@@$$J14YGFH@Z)" in Funktion ""private: void __clrcall mg2::Form1::Start(void)" (?Start@Form1@mg2@@$$FA$AAMXXZ)".

    ???
    Wo liegt der Fehler ?



  • http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx
    Du muss die user32.lib dazulinken.
    Simon



  • brauchst du noch hilfe?



  • Hab ich doch:

    #include "Windows.h"
    


  • C++Lerner schrieb:

    Hab ich doch:

    #include "Windows.h"
    

    Hast Du was?



  • #include <windows.h>
    #include <iostream>
    using namespace std;
    
    const int KeyPressed = -32767;
    int main() {
     while (1) {
      if (GetAsyncKeyState(65) == KeyPressed) cout << "a";
      if (GetAsyncKeyState(66) == KeyPressed) cout << "b";
      if (GetAsyncKeyState(67) == KeyPressed) cout << "c";
      if (GetAsyncKeyState(68) == KeyPressed) cout << "d";
      if (GetAsyncKeyState(69) == KeyPressed) cout << "e";
      if (GetAsyncKeyState(70) == KeyPressed) cout << "f";
      if (GetAsyncKeyState(71) == KeyPressed) cout << "g";
      if (GetAsyncKeyState(72) == KeyPressed) cout << "h";
      if (GetAsyncKeyState(73) == KeyPressed) cout << "i";
      if (GetAsyncKeyState(74) == KeyPressed) cout << "j";
      if (GetAsyncKeyState(75) == KeyPressed) cout << "k";
      if (GetAsyncKeyState(76) == KeyPressed) cout << "l";
      if (GetAsyncKeyState(77) == KeyPressed) cout << "m";
      if (GetAsyncKeyState(78) == KeyPressed) cout << "n";
      if (GetAsyncKeyState(79) == KeyPressed) cout << "o";
      if (GetAsyncKeyState(80) == KeyPressed) cout << "p";
      if (GetAsyncKeyState(81) == KeyPressed) cout << "q";
      if (GetAsyncKeyState(82) == KeyPressed) cout << "r";
      if (GetAsyncKeyState(83) == KeyPressed) cout << "s";
      if (GetAsyncKeyState(84) == KeyPressed) cout << "t";
      if (GetAsyncKeyState(85) == KeyPressed) cout << "u";
      if (GetAsyncKeyState(86) == KeyPressed) cout << "v";
      if (GetAsyncKeyState(87) == KeyPressed) cout << "w";
      if (GetAsyncKeyState(88) == KeyPressed) cout << "x";
      if (GetAsyncKeyState(89) == KeyPressed) cout << "y";
      if (GetAsyncKeyState(90) == KeyPressed) cout << "z";
      if (GetAsyncKeyState(32) == KeyPressed) cout << " ";
      Sleep(1);
     }
     return 0;
    }
    

    nur für informative zwecke gedacht.



  • @theta :

    Ich hab user32 drin^^
    user32 = Windows.h

    @ DerCoder :

    Jaja, an sich funktionierz das auch bei mir, aber bei CLI kommt dauernt der "Token-Error"



  • Linken der Lib! Nicht inkludieren des Headers.



  • Achso...
    Reicht das nicht, wenn man den Header included ?

    Na egal, jetzt kommt ne blöde Frage:
    Wie füge ich die Lib dem Linker zu ?

    Ich denke mal Projekteigenschaften->Linker->
    Aber da will ich jetzt nichts falsch machen...



  • Ich denke mal Projekteigenschaften->Linker->

    Genau: Dann Additional Dependencies
    Vermutlich steht bei Dir noch $(NoInherit). Lösch das raus falls es drin ist. Andernfalls füge user32.lib hinzu.

    Alternativ kannst Du einfach folgende Zeile in dein Source Code einfügen:

    #pragma comment (lib, "user32.lib")
    

    Simon



  • thx, hat geklappt^^



  • Mich wundert nur, warum das nicht reicht, wenn man Windows.h einfügt...?



  • C++Lerner schrieb:

    Mich wundert nur, warum das nicht reicht, wenn man Windows.h einfügt...?

    Der Header enthält Deklarationen, die Lib die Definitionen.
    Simon



  • hm...sonst wenn ich mal Windows.h brauchte, musste ich auch nicht user32 dem linker geben...
    Oder ist das was anderes ?



  • C++Lerner schrieb:

    hm...sonst wenn ich mal Windows.h brauchte, musste ich auch nicht user32 dem linker geben...
    Oder ist das was anderes ?

    Weil das Studio bei C++-ohne-CLI Projekten die USER32.LIB (und einige andere LIBs) automatisch dazulinkt.
    Bei C++/CLI halt nicht.



  • Habe noch eine Frage zu GetAsyncKeyState:

    Habe mir mal so ein kleines Testprogramm gebastelt:

    Rechteckiges Fenster, mit einem kleinen Panel in der Mitte.
    Dieses Panel kann ich mit den Pfeiltasten bewegen. (Halt mit GetAsyncKeyState).

    Das Problem ist aber, dass man mit GetAsyncKeyState() nur EINE Taste abfragen kann und ich das Panel also nur senkrecht und waageecht bewegen kann(nicht diagonal).

    Kennt jemand eine Lösung ?

    Mfg.



  • Ich erwecke dieses Thema nochmal mit meiner Frage zum leben. Wie kann erreichen das GetAsyncKeyState einen Punkt oder ein Komma erkennt? Also mit

    ...
    if(GetAsyncKeyState(46) == KeyPressed){ 
        printf("Punkt gedrueckt"); 
    } 
    ...
    

    erkennt der nicht. Kann mir einer Helfen?



  • Hallo,

    mit:

    GetAsyncKeyState(VK_OEM_PERIOD)  // VK_OEM_PERIOD = 190
    

    ?

    MfG,

    Probe-Nutzer



  • Vielen Dank hat funktioniert 🙂
    Gibt es eine Seite wo man alle Nachgucken kann?



  • Hallo,

    Andyman schrieb:

    Gibt es eine Seite wo man alle Nachgucken kann?

    Aber ja, ich habe z.B. hier:

    http://msdn.microsoft.com/en-us/library/ms645540(VS.85).aspx

    nachgesehen.

    MfG,

    Probe-Nutzer


Anmelden zum Antworten