Results 1 to 8 of 8

Thread: GetAsyncKeyState

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    GetAsyncKeyState

    How do I flush the GetAsyncKeyState buffer? It's really anoying when my program think that I've pressed Return just because I've selected OK in a messagebox...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    GetAsyncKeyState retrieves the physical state of your key, unlike GetKeyState you can't do anything about it, are you sure that you're not missing something, the virtual key for the left mouse button is 1 while return is 13
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    If I do like this:
    Code:
    MessageBox(NULL, "Test", "Test", MB_OK);
    if(GetAsyncKeyState(VK_RETURN))
    {
       MessageBox(NULL, "You've pressed Enter", "Enter", MB_OK);
    }
    The "You've pressed Enter" messagebox will appear right after the "Test" one, if i press Return to OK the "Test" box...

    [MSDN]
    If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState
    [/MSDN]

    I think that's the problem because I call GetAsyncKeyState in a another loop that gets run before this call...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Try this and you'll see what I mean. Run it once like it is and then you uncomment the MessageBox-line and you'll notice my problem... The second messagebox shouldn't appear "on its own"
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    void main()
    {
    	bool bRunning = true;
    	cout << "Press Return" << endl;
    	//MessageBox(NULL, "Test", "Test", MB_OK);
    	while(bRunning)
    	{
    		if(GetAsyncKeyState(VK_RETURN))
    		{
    			MessageBox(NULL, "You've pressed Return", "Return", MB_OK);
    			bRunning = false;
    		}
    	}
    	return;
    }
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    How about trying this

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    void main()
    {
      bool bRunning = true;
      cout << "Press Return" << endl;
      //MessageBox(NULL, "Test", "Test", MB_OK);
      while(bRunning)
      {
        if(GetAsyncKeyState(VK_RETURN) & 0x8000)
        {
          MessageBox(NULL, "You've pressed Return", "Return", MB_OK);
          bRunning = false;
        }
      }
      return;
    }

  6. #6
    Hyperactive Member Cmdr0Sunburn's Avatar
    Join Date
    May 2001
    Location
    g0t r00t?
    Posts
    461
    i use this and it should work...
    VB Code:
    1. #include <iostream>
    2. #include <windows.h>
    3. using namespace std;
    4. int regt;
    5. void main()
    6. {
    7.     bool bRunning = true;
    8.     cout << "Press Return" << endl;
    9.     //MessageBox(NULL, "Test", "Test", MB_OK);
    10.     while(bRunning)
    11.     {
    12. regt = GetAsyncKeyState(VK_RETURN)
    13.         if(regt)
    14.         {
    15.             MessageBox(NULL, "You've pressed Return", "Return", MB_OK);
    16.             bRunning = false;
    17.         }
    18.     }
    19.     return;
    20. }
    I know a lot oF Vb, expert in C++, and i think in assembly.
    MSVC++6.NET
    vb6
    masm
    Windowz Xp
    I find my self using this a lot in C++

    __asm {
    }

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    GetAsyncKeyState() does not work in a console.


    http://www.vbforums.com/showthread.p...tasynckeystate

    There is a bug with my attached code when 3 keys are pressed. 2 keys should be okay I think.

    http://www.vbforums.com/showthread.p...tasynckeystate

  8. #8

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, guys. I must've explained poorly...
    Run the code as it is and it DOES work.
    Uncomment (i.e. remove the //) the "MessageBox(NULL, "Test", "Test", MB_OK);" line and it doesn't work (like I want it) any more.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width