Virtueller Zähler für GetAsyncKeyState oder doch einfacher?



  • hi,

    Ich hab ne Schleife die so aussieht:

    for(;;) {
    if(GetAsyncKeyState(VK_NUMPAD1) {
         doAnything();
    }
    
    Sleep(100);
    
    }
    

    Mein Problem ist jetzt, dass wen ich einaml Numpad1 drücke doAnything gleich 3-5 mal ausgeführt wird.
    Meine Idee ist es jetzt nen couter zu bauen der ab 1++ die schleife ignoriert und den counter nach 2min resettet aber das ist unnormal kompliziert.
    Hat jemand eine andere lösung?



  • Du solltest mal grob sagen, was Du eigentlich tun willst...
    Wenn die "doAnything" nur einmal aufgerufen werden sollte, dann musst Du dir merken, dass Du sie aufgerufen hast und dann warten, bis die Taste wieder losgelassen wurde und dann den merker zurücksetzen...

    Ganz abgesehen davon, bekommst Du hier kurze Tastendrücke nicht mit...
    I.d.R. macht man sowas mit Keyboard-Hooks...



  • If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
    [...]
    Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.



  • hmm hat wer dazu viell. ein Beispiel?



  • Also ok, ich werde ein Keyboard hook bauen wie hier:
    http://www.codeproject.com/dll/keyboardhook.asp

    (ich weiss auch schon ein kleines beispiel das mir hilft)

    achja zu for(;;) {
    if(GetAsyncKeyState(VK_NUMPAD1) == 1) { cout<<"bla"; }
    }

    Das hat nich funktioniert wegen "==1" also if(GetAsyncKeyState(VK_NUMPAD1)) { cout<<"bla"; }

    Was ein verherrender fehler ;/



  • Wenn du einzelne Bits abfragen willst, mußt du den Rückgabewert per &|^ zerlegen:

    if((getAsyncKeyState(VK_NUMPAD1)&1)!=0)...
    


  • Ok das funktioniert schon recht gut aber wenn ich nen keyboard hook baue der so funktioniert;

    LRESULT CALLBACK HookedWindowProcedure(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    switch(message)
    {
    case WM_DOWN:
    {
    wParam==VK_LEFT
    }
    break;
    }

    }

    kann ich dann die taste gedürckt halten oder ist das dann genauso wie numpad1&1?


Anmelden zum Antworten